]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/tcp_input.c
Add UPDATING entries and bump version.
[FreeBSD/FreeBSD.git] / sys / netinet / tcp_input.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5  *      The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 2007-2008,2010
7  *      Swinburne University of Technology, Melbourne, Australia.
8  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
9  * Copyright (c) 2010 The FreeBSD Foundation
10  * Copyright (c) 2010-2011 Juniper Networks, Inc.
11  * All rights reserved.
12  *
13  * Portions of this software were developed at the Centre for Advanced Internet
14  * Architectures, Swinburne University of Technology, by Lawrence Stewart,
15  * James Healy and David Hayes, made possible in part by a grant from the Cisco
16  * University Research Program Fund at Community Foundation Silicon Valley.
17  *
18  * Portions of this software were developed at the Centre for Advanced
19  * Internet Architectures, Swinburne University of Technology, Melbourne,
20  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
21  *
22  * Portions of this software were developed by Robert N. M. Watson under
23  * contract to Juniper Networks, Inc.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the above copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  *    notice, this list of conditions and the following disclaimer in the
32  *    documentation and/or other materials provided with the distribution.
33  * 3. Neither the name of the University nor the names of its contributors
34  *    may be used to endorse or promote products derived from this software
35  *    without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  *      @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
50  */
51
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
54
55 #include "opt_inet.h"
56 #include "opt_inet6.h"
57 #include "opt_ipsec.h"
58 #include "opt_tcpdebug.h"
59
60 #include <sys/param.h>
61 #include <sys/kernel.h>
62 #ifdef TCP_HHOOK
63 #include <sys/hhook.h>
64 #endif
65 #include <sys/malloc.h>
66 #include <sys/mbuf.h>
67 #include <sys/proc.h>           /* for proc0 declaration */
68 #include <sys/protosw.h>
69 #include <sys/sdt.h>
70 #include <sys/signalvar.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/sysctl.h>
74 #include <sys/syslog.h>
75 #include <sys/systm.h>
76
77 #include <machine/cpu.h>        /* before tcp_seq.h, for tcp_random18() */
78
79 #include <vm/uma.h>
80
81 #include <net/if.h>
82 #include <net/if_var.h>
83 #include <net/route.h>
84 #include <net/vnet.h>
85
86 #define TCPSTATES               /* for logging */
87
88 #include <netinet/in.h>
89 #include <netinet/in_kdtrace.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/ip_icmp.h>    /* required for icmp_var.h */
94 #include <netinet/icmp_var.h>   /* for ICMP_BANDLIM */
95 #include <netinet/ip_var.h>
96 #include <netinet/ip_options.h>
97 #include <netinet/ip6.h>
98 #include <netinet/icmp6.h>
99 #include <netinet6/in6_pcb.h>
100 #include <netinet6/in6_var.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/nd6.h>
103 #include <netinet/tcp.h>
104 #include <netinet/tcp_fsm.h>
105 #include <netinet/tcp_log_buf.h>
106 #include <netinet/tcp_seq.h>
107 #include <netinet/tcp_timer.h>
108 #include <netinet/tcp_var.h>
109 #include <netinet6/tcp6_var.h>
110 #include <netinet/tcpip.h>
111 #include <netinet/cc/cc.h>
112 #include <netinet/tcp_fastopen.h>
113 #ifdef TCPPCAP
114 #include <netinet/tcp_pcap.h>
115 #endif
116 #include <netinet/tcp_syncache.h>
117 #ifdef TCPDEBUG
118 #include <netinet/tcp_debug.h>
119 #endif /* TCPDEBUG */
120 #ifdef TCP_OFFLOAD
121 #include <netinet/tcp_offload.h>
122 #endif
123
124 #include <netipsec/ipsec_support.h>
125
126 #include <machine/in_cksum.h>
127
128 #include <security/mac/mac_framework.h>
129
130 const int tcprexmtthresh = 3;
131
132 VNET_DEFINE(int, tcp_log_in_vain) = 0;
133 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
134     &VNET_NAME(tcp_log_in_vain), 0,
135     "Log all incoming TCP segments to closed ports");
136
137 VNET_DEFINE(int, blackhole) = 0;
138 #define V_blackhole             VNET(blackhole)
139 SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
140     &VNET_NAME(blackhole), 0,
141     "Do not send RST on segments to closed ports");
142
143 VNET_DEFINE(int, tcp_delack_enabled) = 1;
144 SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
145     &VNET_NAME(tcp_delack_enabled), 0,
146     "Delay ACK to try and piggyback it onto a data packet");
147
148 VNET_DEFINE(int, drop_synfin) = 0;
149 SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
150     &VNET_NAME(drop_synfin), 0,
151     "Drop TCP packets with SYN+FIN set");
152
153 VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0;
154 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW,
155     &VNET_NAME(tcp_do_rfc6675_pipe), 0,
156     "Use calculated pipe/in-flight bytes per RFC 6675");
157
158 VNET_DEFINE(int, tcp_do_rfc3042) = 1;
159 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
160     &VNET_NAME(tcp_do_rfc3042), 0,
161     "Enable RFC 3042 (Limited Transmit)");
162
163 VNET_DEFINE(int, tcp_do_rfc3390) = 1;
164 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
165     &VNET_NAME(tcp_do_rfc3390), 0,
166     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
167
168 VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
169 SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
170     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
171     "Slow-start flight size (initial congestion window) in number of segments");
172
173 VNET_DEFINE(int, tcp_do_rfc3465) = 1;
174 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
175     &VNET_NAME(tcp_do_rfc3465), 0,
176     "Enable RFC 3465 (Appropriate Byte Counting)");
177
178 VNET_DEFINE(int, tcp_abc_l_var) = 2;
179 SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
180     &VNET_NAME(tcp_abc_l_var), 2,
181     "Cap the max cwnd increment during slow-start to this number of segments");
182
183 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
184
185 VNET_DEFINE(int, tcp_do_ecn) = 2;
186 SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
187     &VNET_NAME(tcp_do_ecn), 0,
188     "TCP ECN support");
189
190 VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
191 SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW,
192     &VNET_NAME(tcp_ecn_maxretries), 0,
193     "Max retries before giving up on ECN");
194
195 VNET_DEFINE(int, tcp_insecure_syn) = 0;
196 SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
197     &VNET_NAME(tcp_insecure_syn), 0,
198     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
199
200 VNET_DEFINE(int, tcp_insecure_rst) = 0;
201 SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
202     &VNET_NAME(tcp_insecure_rst), 0,
203     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
204
205 VNET_DEFINE(int, tcp_recvspace) = 1024*64;
206 #define V_tcp_recvspace VNET(tcp_recvspace)
207 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
208     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
209
210 VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
211 SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
212     &VNET_NAME(tcp_do_autorcvbuf), 0,
213     "Enable automatic receive buffer sizing");
214
215 VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024;
216 SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW,
217     &VNET_NAME(tcp_autorcvbuf_inc), 0,
218     "Incrementor step size of automatic receive buffer");
219
220 VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
221 SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
222     &VNET_NAME(tcp_autorcvbuf_max), 0,
223     "Max size of automatic receive buffer");
224
225 VNET_DEFINE(struct inpcbhead, tcb);
226 #define tcb6    tcb  /* for KAME src sync over BSD*'s */
227 VNET_DEFINE(struct inpcbinfo, tcbinfo);
228
229 /*
230  * TCP statistics are stored in an array of counter(9)s, which size matches
231  * size of struct tcpstat.  TCP running connection count is a regular array.
232  */
233 VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
234 SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
235     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
236 VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
237 SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
238     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
239     "TCP connection counts by TCP state");
240
241 static void
242 tcp_vnet_init(const void *unused)
243 {
244
245         COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK);
246         VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK);
247 }
248 VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
249     tcp_vnet_init, NULL);
250
251 #ifdef VIMAGE
252 static void
253 tcp_vnet_uninit(const void *unused)
254 {
255
256         COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES);
257         VNET_PCPUSTAT_FREE(tcpstat);
258 }
259 VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
260     tcp_vnet_uninit, NULL);
261 #endif /* VIMAGE */
262
263 /*
264  * Kernel module interface for updating tcpstat.  The argument is an index
265  * into tcpstat treated as an array.
266  */
267 void
268 kmod_tcpstat_inc(int statnum)
269 {
270
271         counter_u64_add(VNET(tcpstat)[statnum], 1);
272 }
273
274 #ifdef TCP_HHOOK
275 /*
276  * Wrapper for the TCP established input helper hook.
277  */
278 void
279 hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
280 {
281         struct tcp_hhook_data hhook_data;
282
283         if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
284                 hhook_data.tp = tp;
285                 hhook_data.th = th;
286                 hhook_data.to = to;
287
288                 hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
289                     tp->osd);
290         }
291 }
292 #endif
293
294 /*
295  * CC wrapper hook functions
296  */
297 void
298 cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
299     uint16_t type)
300 {
301         INP_WLOCK_ASSERT(tp->t_inpcb);
302
303         tp->ccv->nsegs = nsegs;
304         tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
305         if (tp->snd_cwnd <= tp->snd_wnd)
306                 tp->ccv->flags |= CCF_CWND_LIMITED;
307         else
308                 tp->ccv->flags &= ~CCF_CWND_LIMITED;
309
310         if (type == CC_ACK) {
311                 if (tp->snd_cwnd > tp->snd_ssthresh) {
312                         tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
313                              nsegs * V_tcp_abc_l_var * tcp_maxseg(tp));
314                         if (tp->t_bytes_acked >= tp->snd_cwnd) {
315                                 tp->t_bytes_acked -= tp->snd_cwnd;
316                                 tp->ccv->flags |= CCF_ABC_SENTAWND;
317                         }
318                 } else {
319                                 tp->ccv->flags &= ~CCF_ABC_SENTAWND;
320                                 tp->t_bytes_acked = 0;
321                 }
322         }
323
324         if (CC_ALGO(tp)->ack_received != NULL) {
325                 /* XXXLAS: Find a way to live without this */
326                 tp->ccv->curack = th->th_ack;
327                 CC_ALGO(tp)->ack_received(tp->ccv, type);
328         }
329 }
330
331 void 
332 cc_conn_init(struct tcpcb *tp)
333 {
334         struct hc_metrics_lite metrics;
335         struct inpcb *inp = tp->t_inpcb;
336         u_int maxseg;
337         int rtt;
338
339         INP_WLOCK_ASSERT(tp->t_inpcb);
340
341         tcp_hc_get(&inp->inp_inc, &metrics);
342         maxseg = tcp_maxseg(tp);
343
344         if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
345                 tp->t_srtt = rtt;
346                 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
347                 TCPSTAT_INC(tcps_usedrtt);
348                 if (metrics.rmx_rttvar) {
349                         tp->t_rttvar = metrics.rmx_rttvar;
350                         TCPSTAT_INC(tcps_usedrttvar);
351                 } else {
352                         /* default variation is +- 1 rtt */
353                         tp->t_rttvar =
354                             tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
355                 }
356                 TCPT_RANGESET(tp->t_rxtcur,
357                     ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
358                     tp->t_rttmin, TCPTV_REXMTMAX);
359         }
360         if (metrics.rmx_ssthresh) {
361                 /*
362                  * There's some sort of gateway or interface
363                  * buffer limit on the path.  Use this to set
364                  * the slow start threshold, but set the
365                  * threshold to no less than 2*mss.
366                  */
367                 tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
368                 TCPSTAT_INC(tcps_usedssthresh);
369         }
370
371         /*
372          * Set the initial slow-start flight size.
373          *
374          * RFC5681 Section 3.1 specifies the default conservative values.
375          * RFC3390 specifies slightly more aggressive values.
376          * RFC6928 increases it to ten segments.
377          * Support for user specified value for initial flight size.
378          *
379          * If a SYN or SYN/ACK was lost and retransmitted, we have to
380          * reduce the initial CWND to one segment as congestion is likely
381          * requiring us to be cautious.
382          */
383         if (tp->snd_cwnd == 1)
384                 tp->snd_cwnd = maxseg;          /* SYN(-ACK) lost */
385         else if (V_tcp_initcwnd_segments)
386                 tp->snd_cwnd = min(V_tcp_initcwnd_segments * maxseg,
387                     max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
388         else if (V_tcp_do_rfc3390)
389                 tp->snd_cwnd = min(4 * maxseg, max(2 * maxseg, 4380));
390         else {
391                 /* Per RFC5681 Section 3.1 */
392                 if (maxseg > 2190)
393                         tp->snd_cwnd = 2 * maxseg;
394                 else if (maxseg > 1095)
395                         tp->snd_cwnd = 3 * maxseg;
396                 else
397                         tp->snd_cwnd = 4 * maxseg;
398         }
399
400         if (CC_ALGO(tp)->conn_init != NULL)
401                 CC_ALGO(tp)->conn_init(tp->ccv);
402 }
403
404 void inline
405 cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
406 {
407         u_int maxseg;
408
409         INP_WLOCK_ASSERT(tp->t_inpcb);
410
411         switch(type) {
412         case CC_NDUPACK:
413                 if (!IN_FASTRECOVERY(tp->t_flags)) {
414                         tp->snd_recover = tp->snd_max;
415                         if (tp->t_flags & TF_ECN_PERMIT)
416                                 tp->t_flags |= TF_ECN_SND_CWR;
417                 }
418                 break;
419         case CC_ECN:
420                 if (!IN_CONGRECOVERY(tp->t_flags) ||
421                     /*
422                      * Allow ECN reaction on ACK to CWR, if
423                      * that data segment was also CE marked.
424                      */
425                     SEQ_GEQ(th->th_ack, tp->snd_recover)) {
426                         EXIT_CONGRECOVERY(tp->t_flags);
427                         TCPSTAT_INC(tcps_ecn_rcwnd);
428                         tp->snd_recover = tp->snd_max + 1;
429                         if (tp->t_flags & TF_ECN_PERMIT)
430                                 tp->t_flags |= TF_ECN_SND_CWR;
431                 }
432                 break;
433         case CC_RTO:
434                 maxseg = tcp_maxseg(tp);
435                 tp->t_dupacks = 0;
436                 tp->t_bytes_acked = 0;
437                 EXIT_RECOVERY(tp->t_flags);
438                 tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 /
439                     maxseg) * maxseg;
440                 tp->snd_cwnd = maxseg;
441                 break;
442         case CC_RTO_ERR:
443                 TCPSTAT_INC(tcps_sndrexmitbad);
444                 /* RTO was unnecessary, so reset everything. */
445                 tp->snd_cwnd = tp->snd_cwnd_prev;
446                 tp->snd_ssthresh = tp->snd_ssthresh_prev;
447                 tp->snd_recover = tp->snd_recover_prev;
448                 if (tp->t_flags & TF_WASFRECOVERY)
449                         ENTER_FASTRECOVERY(tp->t_flags);
450                 if (tp->t_flags & TF_WASCRECOVERY)
451                         ENTER_CONGRECOVERY(tp->t_flags);
452                 tp->snd_nxt = tp->snd_max;
453                 tp->t_flags &= ~TF_PREVVALID;
454                 tp->t_badrxtwin = 0;
455                 break;
456         }
457
458         if (CC_ALGO(tp)->cong_signal != NULL) {
459                 if (th != NULL)
460                         tp->ccv->curack = th->th_ack;
461                 CC_ALGO(tp)->cong_signal(tp->ccv, type);
462         }
463 }
464
465 void inline
466 cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
467 {
468         INP_WLOCK_ASSERT(tp->t_inpcb);
469
470         /* XXXLAS: KASSERT that we're in recovery? */
471
472         if (CC_ALGO(tp)->post_recovery != NULL) {
473                 tp->ccv->curack = th->th_ack;
474                 CC_ALGO(tp)->post_recovery(tp->ccv);
475         }
476         /* XXXLAS: EXIT_RECOVERY ? */
477         tp->t_bytes_acked = 0;
478 }
479
480 /*
481  * Indicate whether this ack should be delayed.  We can delay the ack if
482  * following conditions are met:
483  *      - There is no delayed ack timer in progress.
484  *      - Our last ack wasn't a 0-sized window. We never want to delay
485  *        the ack that opens up a 0-sized window.
486  *      - LRO wasn't used for this segment. We make sure by checking that the
487  *        segment size is not larger than the MSS.
488  */
489 #define DELAY_ACK(tp, tlen)                                             \
490         ((!tcp_timer_active(tp, TT_DELACK) &&                           \
491             (tp->t_flags & TF_RXWIN0SENT) == 0) &&                      \
492             (tlen <= tp->t_maxseg) &&                                   \
493             (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
494
495 static void inline
496 cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
497 {
498         INP_WLOCK_ASSERT(tp->t_inpcb);
499
500         if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
501                 switch (iptos & IPTOS_ECN_MASK) {
502                 case IPTOS_ECN_CE:
503                         tp->ccv->flags |= CCF_IPHDR_CE;
504                         break;
505                 case IPTOS_ECN_ECT0:
506                         /* FALLTHROUGH */
507                 case IPTOS_ECN_ECT1:
508                         /* FALLTHROUGH */
509                 case IPTOS_ECN_NOTECT:
510                         tp->ccv->flags &= ~CCF_IPHDR_CE;
511                         break;
512                 }
513
514                 if (th->th_flags & TH_CWR)
515                         tp->ccv->flags |= CCF_TCPHDR_CWR;
516                 else
517                         tp->ccv->flags &= ~CCF_TCPHDR_CWR;
518
519                 if (tp->t_flags & TF_DELACK)
520                         tp->ccv->flags |= CCF_DELACK;
521                 else
522                         tp->ccv->flags &= ~CCF_DELACK;
523
524                 CC_ALGO(tp)->ecnpkt_handler(tp->ccv);
525
526                 if (tp->ccv->flags & CCF_ACKNOW)
527                         tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
528         }
529 }
530
531 /*
532  * TCP input handling is split into multiple parts:
533  *   tcp6_input is a thin wrapper around tcp_input for the extended
534  *      ip6_protox[] call format in ip6_input
535  *   tcp_input handles primary segment validation, inpcb lookup and
536  *      SYN processing on listen sockets
537  *   tcp_do_segment processes the ACK and text of the segment for
538  *      establishing, established and closing connections
539  */
540 #ifdef INET6
541 int
542 tcp6_input(struct mbuf **mp, int *offp, int proto)
543 {
544         struct mbuf *m;
545         struct in6_ifaddr *ia6;
546         struct ip6_hdr *ip6;
547
548         m = *mp;
549         if (m->m_len < *offp + sizeof(struct tcphdr)) {
550                 m = m_pullup(m, *offp + sizeof(struct tcphdr));
551                 if (m == NULL) {
552                         *mp = m;
553                         TCPSTAT_INC(tcps_rcvshort);
554                         return (IPPROTO_DONE);
555                 }
556         }
557
558         /*
559          * draft-itojun-ipv6-tcp-to-anycast
560          * better place to put this in?
561          */
562         ip6 = mtod(m, struct ip6_hdr *);
563         ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
564         if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
565
566                 ifa_free(&ia6->ia_ifa);
567                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
568                             (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
569                 *mp = NULL;
570                 return (IPPROTO_DONE);
571         }
572         if (ia6)
573                 ifa_free(&ia6->ia_ifa);
574
575         *mp = m;
576         return (tcp_input(mp, offp, proto));
577 }
578 #endif /* INET6 */
579
580 int
581 tcp_input(struct mbuf **mp, int *offp, int proto)
582 {
583         struct mbuf *m = *mp;
584         struct tcphdr *th = NULL;
585         struct ip *ip = NULL;
586         struct inpcb *inp = NULL;
587         struct tcpcb *tp = NULL;
588         struct socket *so = NULL;
589         u_char *optp = NULL;
590         int off0;
591         int optlen = 0;
592 #ifdef INET
593         int len;
594         uint8_t ipttl;
595 #endif
596         int tlen = 0, off;
597         int drop_hdrlen;
598         int thflags;
599         int rstreason = 0;      /* For badport_bandlim accounting purposes */
600         uint8_t iptos;
601         struct m_tag *fwd_tag = NULL;
602         struct epoch_tracker et;
603 #ifdef INET6
604         struct ip6_hdr *ip6 = NULL;
605         int isipv6;
606 #else
607         const void *ip6 = NULL;
608 #endif /* INET6 */
609         struct tcpopt to;               /* options in this segment */
610         char *s = NULL;                 /* address and port logging */
611         int ti_locked;
612 #ifdef TCPDEBUG
613         /*
614          * The size of tcp_saveipgen must be the size of the max ip header,
615          * now IPv6.
616          */
617         u_char tcp_saveipgen[IP6_HDR_LEN];
618         struct tcphdr tcp_savetcp;
619         short ostate = 0;
620 #endif
621
622 #ifdef INET6
623         isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
624 #endif
625
626         off0 = *offp;
627         m = *mp;
628         *mp = NULL;
629         to.to_flags = 0;
630         TCPSTAT_INC(tcps_rcvtotal);
631
632 #ifdef INET6
633         if (isipv6) {
634
635                 ip6 = mtod(m, struct ip6_hdr *);
636                 th = (struct tcphdr *)((caddr_t)ip6 + off0);
637                 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
638                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
639                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
640                                 th->th_sum = m->m_pkthdr.csum_data;
641                         else
642                                 th->th_sum = in6_cksum_pseudo(ip6, tlen,
643                                     IPPROTO_TCP, m->m_pkthdr.csum_data);
644                         th->th_sum ^= 0xffff;
645                 } else
646                         th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
647                 if (th->th_sum) {
648                         TCPSTAT_INC(tcps_rcvbadsum);
649                         goto drop;
650                 }
651
652                 /*
653                  * Be proactive about unspecified IPv6 address in source.
654                  * As we use all-zero to indicate unbounded/unconnected pcb,
655                  * unspecified IPv6 address can be used to confuse us.
656                  *
657                  * Note that packets with unspecified IPv6 destination is
658                  * already dropped in ip6_input.
659                  */
660                 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
661                         /* XXX stat */
662                         goto drop;
663                 }
664                 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
665         }
666 #endif
667 #if defined(INET) && defined(INET6)
668         else
669 #endif
670 #ifdef INET
671         {
672                 /*
673                  * Get IP and TCP header together in first mbuf.
674                  * Note: IP leaves IP header in first mbuf.
675                  */
676                 if (off0 > sizeof (struct ip)) {
677                         ip_stripoptions(m);
678                         off0 = sizeof(struct ip);
679                 }
680                 if (m->m_len < sizeof (struct tcpiphdr)) {
681                         if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
682                             == NULL) {
683                                 TCPSTAT_INC(tcps_rcvshort);
684                                 return (IPPROTO_DONE);
685                         }
686                 }
687                 ip = mtod(m, struct ip *);
688                 th = (struct tcphdr *)((caddr_t)ip + off0);
689                 tlen = ntohs(ip->ip_len) - off0;
690
691                 iptos = ip->ip_tos;
692                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
693                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
694                                 th->th_sum = m->m_pkthdr.csum_data;
695                         else
696                                 th->th_sum = in_pseudo(ip->ip_src.s_addr,
697                                     ip->ip_dst.s_addr,
698                                     htonl(m->m_pkthdr.csum_data + tlen +
699                                     IPPROTO_TCP));
700                         th->th_sum ^= 0xffff;
701                 } else {
702                         struct ipovly *ipov = (struct ipovly *)ip;
703
704                         /*
705                          * Checksum extended TCP header and data.
706                          */
707                         len = off0 + tlen;
708                         ipttl = ip->ip_ttl;
709                         bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
710                         ipov->ih_len = htons(tlen);
711                         th->th_sum = in_cksum(m, len);
712                         /* Reset length for SDT probes. */
713                         ip->ip_len = htons(len);
714                         /* Reset TOS bits */
715                         ip->ip_tos = iptos;
716                         /* Re-initialization for later version check */
717                         ip->ip_ttl = ipttl;
718                         ip->ip_v = IPVERSION;
719                         ip->ip_hl = off0 >> 2;
720                 }
721
722                 if (th->th_sum) {
723                         TCPSTAT_INC(tcps_rcvbadsum);
724                         goto drop;
725                 }
726         }
727 #endif /* INET */
728
729         /*
730          * Check that TCP offset makes sense,
731          * pull out TCP options and adjust length.              XXX
732          */
733         off = th->th_off << 2;
734         if (off < sizeof (struct tcphdr) || off > tlen) {
735                 TCPSTAT_INC(tcps_rcvbadoff);
736                 goto drop;
737         }
738         tlen -= off;    /* tlen is used instead of ti->ti_len */
739         if (off > sizeof (struct tcphdr)) {
740 #ifdef INET6
741                 if (isipv6) {
742                         if (m->m_len < off0 + off) {
743                                 m = m_pullup(m, off0 + off);
744                                 if (m == NULL) {
745                                         TCPSTAT_INC(tcps_rcvshort);
746                                         return (IPPROTO_DONE);
747                                 }
748                         }
749                         ip6 = mtod(m, struct ip6_hdr *);
750                         th = (struct tcphdr *)((caddr_t)ip6 + off0);
751                 }
752 #endif
753 #if defined(INET) && defined(INET6)
754                 else
755 #endif
756 #ifdef INET
757                 {
758                         if (m->m_len < sizeof(struct ip) + off) {
759                                 if ((m = m_pullup(m, sizeof (struct ip) + off))
760                                     == NULL) {
761                                         TCPSTAT_INC(tcps_rcvshort);
762                                         return (IPPROTO_DONE);
763                                 }
764                                 ip = mtod(m, struct ip *);
765                                 th = (struct tcphdr *)((caddr_t)ip + off0);
766                         }
767                 }
768 #endif
769                 optlen = off - sizeof (struct tcphdr);
770                 optp = (u_char *)(th + 1);
771         }
772         thflags = th->th_flags;
773
774         /*
775          * Convert TCP protocol specific fields to host format.
776          */
777         tcp_fields_to_host(th);
778
779         /*
780          * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
781          */
782         drop_hdrlen = off0 + off;
783
784         /*
785          * Locate pcb for segment; if we're likely to add or remove a
786          * connection then first acquire pcbinfo lock.  There are three cases
787          * where we might discover later we need a write lock despite the
788          * flags: ACKs moving a connection out of the syncache, ACKs for a
789          * connection in TIMEWAIT and SYNs not targeting a listening socket.
790          */
791         if ((thflags & (TH_FIN | TH_RST)) != 0) {
792                 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
793                 ti_locked = TI_RLOCKED;
794         } else
795                 ti_locked = TI_UNLOCKED;
796
797         /*
798          * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
799          */
800         if (
801 #ifdef INET6
802             (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
803 #ifdef INET
804             || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
805 #endif
806 #endif
807 #if defined(INET) && !defined(INET6)
808             (m->m_flags & M_IP_NEXTHOP)
809 #endif
810             )
811                 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
812
813 findpcb:
814 #ifdef INVARIANTS
815         if (ti_locked == TI_RLOCKED) {
816                 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
817         } else {
818                 INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
819         }
820 #endif
821 #ifdef INET6
822         if (isipv6 && fwd_tag != NULL) {
823                 struct sockaddr_in6 *next_hop6;
824
825                 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
826                 /*
827                  * Transparently forwarded. Pretend to be the destination.
828                  * Already got one like this?
829                  */
830                 inp = in6_pcblookup_mbuf(&V_tcbinfo,
831                     &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
832                     INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
833                 if (!inp) {
834                         /*
835                          * It's new.  Try to find the ambushing socket.
836                          * Because we've rewritten the destination address,
837                          * any hardware-generated hash is ignored.
838                          */
839                         inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
840                             th->th_sport, &next_hop6->sin6_addr,
841                             next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
842                             th->th_dport, INPLOOKUP_WILDCARD |
843                             INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
844                 }
845         } else if (isipv6) {
846                 inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
847                     th->th_sport, &ip6->ip6_dst, th->th_dport,
848                     INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
849                     m->m_pkthdr.rcvif, m);
850         }
851 #endif /* INET6 */
852 #if defined(INET6) && defined(INET)
853         else
854 #endif
855 #ifdef INET
856         if (fwd_tag != NULL) {
857                 struct sockaddr_in *next_hop;
858
859                 next_hop = (struct sockaddr_in *)(fwd_tag+1);
860                 /*
861                  * Transparently forwarded. Pretend to be the destination.
862                  * already got one like this?
863                  */
864                 inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
865                     ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
866                     m->m_pkthdr.rcvif, m);
867                 if (!inp) {
868                         /*
869                          * It's new.  Try to find the ambushing socket.
870                          * Because we've rewritten the destination address,
871                          * any hardware-generated hash is ignored.
872                          */
873                         inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
874                             th->th_sport, next_hop->sin_addr,
875                             next_hop->sin_port ? ntohs(next_hop->sin_port) :
876                             th->th_dport, INPLOOKUP_WILDCARD |
877                             INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
878                 }
879         } else
880                 inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
881                     th->th_sport, ip->ip_dst, th->th_dport,
882                     INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
883                     m->m_pkthdr.rcvif, m);
884 #endif /* INET */
885
886         /*
887          * If the INPCB does not exist then all data in the incoming
888          * segment is discarded and an appropriate RST is sent back.
889          * XXX MRT Send RST using which routing table?
890          */
891         if (inp == NULL) {
892                 /*
893                  * Log communication attempts to ports that are not
894                  * in use.
895                  */
896                 if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
897                     V_tcp_log_in_vain == 2) {
898                         if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
899                                 log(LOG_INFO, "%s; %s: Connection attempt "
900                                     "to closed port\n", s, __func__);
901                 }
902                 /*
903                  * When blackholing do not respond with a RST but
904                  * completely ignore the segment and drop it.
905                  */
906                 if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
907                     V_blackhole == 2)
908                         goto dropunlock;
909
910                 rstreason = BANDLIM_RST_CLOSEDPORT;
911                 goto dropwithreset;
912         }
913         INP_WLOCK_ASSERT(inp);
914         /*
915          * While waiting for inp lock during the lookup, another thread
916          * can have dropped the inpcb, in which case we need to loop back
917          * and try to find a new inpcb to deliver to.
918          */
919         if (inp->inp_flags & INP_DROPPED) {
920                 INP_WUNLOCK(inp);
921                 inp = NULL;
922                 goto findpcb;
923         }
924         if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
925             (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
926             ((inp->inp_socket == NULL) ||
927             (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) {
928                 inp->inp_flowid = m->m_pkthdr.flowid;
929                 inp->inp_flowtype = M_HASHTYPE_GET(m);
930         }
931 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
932 #ifdef INET6
933         if (isipv6 && IPSEC_ENABLED(ipv6) &&
934             IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
935                 goto dropunlock;
936         }
937 #ifdef INET
938         else
939 #endif
940 #endif /* INET6 */
941 #ifdef INET
942         if (IPSEC_ENABLED(ipv4) &&
943             IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
944                 goto dropunlock;
945         }
946 #endif /* INET */
947 #endif /* IPSEC */
948
949         /*
950          * Check the minimum TTL for socket.
951          */
952         if (inp->inp_ip_minttl != 0) {
953 #ifdef INET6
954                 if (isipv6) {
955                         if (inp->inp_ip_minttl > ip6->ip6_hlim)
956                                 goto dropunlock;
957                 } else
958 #endif
959                 if (inp->inp_ip_minttl > ip->ip_ttl)
960                         goto dropunlock;
961         }
962
963         /*
964          * A previous connection in TIMEWAIT state is supposed to catch stray
965          * or duplicate segments arriving late.  If this segment was a
966          * legitimate new connection attempt, the old INPCB gets removed and
967          * we can try again to find a listening socket.
968          *
969          * At this point, due to earlier optimism, we may hold only an inpcb
970          * lock, and not the inpcbinfo write lock.  If so, we need to try to
971          * acquire it, or if that fails, acquire a reference on the inpcb,
972          * drop all locks, acquire a global write lock, and then re-acquire
973          * the inpcb lock.  We may at that point discover that another thread
974          * has tried to free the inpcb, in which case we need to loop back
975          * and try to find a new inpcb to deliver to.
976          *
977          * XXXRW: It may be time to rethink timewait locking.
978          */
979         if (inp->inp_flags & INP_TIMEWAIT) {
980                 if (ti_locked == TI_UNLOCKED) {
981                         INP_INFO_RLOCK_ET(&V_tcbinfo, et);
982                         ti_locked = TI_RLOCKED;
983                 }
984                 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
985
986                 if (thflags & TH_SYN)
987                         tcp_dooptions(&to, optp, optlen, TO_SYN);
988                 /*
989                  * NB: tcp_twcheck unlocks the INP and frees the mbuf.
990                  */
991                 if (tcp_twcheck(inp, &to, th, m, tlen))
992                         goto findpcb;
993                 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
994                 return (IPPROTO_DONE);
995         }
996         /*
997          * The TCPCB may no longer exist if the connection is winding
998          * down or it is in the CLOSED state.  Either way we drop the
999          * segment and send an appropriate response.
1000          */
1001         tp = intotcpcb(inp);
1002         if (tp == NULL || tp->t_state == TCPS_CLOSED) {
1003                 rstreason = BANDLIM_RST_CLOSEDPORT;
1004                 goto dropwithreset;
1005         }
1006
1007 #ifdef TCP_OFFLOAD
1008         if (tp->t_flags & TF_TOE) {
1009                 tcp_offload_input(tp, m);
1010                 m = NULL;       /* consumed by the TOE driver */
1011                 goto dropunlock;
1012         }
1013 #endif
1014
1015         /*
1016          * We've identified a valid inpcb, but it could be that we need an
1017          * inpcbinfo write lock but don't hold it.  In this case, attempt to
1018          * acquire using the same strategy as the TIMEWAIT case above.  If we
1019          * relock, we have to jump back to 'relocked' as the connection might
1020          * now be in TIMEWAIT.
1021          */
1022 #ifdef INVARIANTS
1023         if ((thflags & (TH_FIN | TH_RST)) != 0)
1024                 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1025 #endif
1026         if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) ||
1027               (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) &&
1028                !IS_FASTOPEN(tp->t_flags)))) {
1029                 if (ti_locked == TI_UNLOCKED) {
1030                         INP_INFO_RLOCK_ET(&V_tcbinfo, et);
1031                         ti_locked = TI_RLOCKED;
1032                 }
1033                 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1034         }
1035
1036 #ifdef MAC
1037         INP_WLOCK_ASSERT(inp);
1038         if (mac_inpcb_check_deliver(inp, m))
1039                 goto dropunlock;
1040 #endif
1041         so = inp->inp_socket;
1042         KASSERT(so != NULL, ("%s: so == NULL", __func__));
1043 #ifdef TCPDEBUG
1044         if (so->so_options & SO_DEBUG) {
1045                 ostate = tp->t_state;
1046 #ifdef INET6
1047                 if (isipv6) {
1048                         bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1049                 } else
1050 #endif
1051                         bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1052                 tcp_savetcp = *th;
1053         }
1054 #endif /* TCPDEBUG */
1055         /*
1056          * When the socket is accepting connections (the INPCB is in LISTEN
1057          * state) we look into the SYN cache if this is a new connection
1058          * attempt or the completion of a previous one.
1059          */
1060         KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN),
1061             ("%s: so accepting but tp %p not listening", __func__, tp));
1062         if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) {
1063                 struct in_conninfo inc;
1064
1065                 bzero(&inc, sizeof(inc));
1066 #ifdef INET6
1067                 if (isipv6) {
1068                         inc.inc_flags |= INC_ISIPV6;
1069                         if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
1070                                 inc.inc_flags |= INC_IPV6MINMTU;
1071                         inc.inc6_faddr = ip6->ip6_src;
1072                         inc.inc6_laddr = ip6->ip6_dst;
1073                 } else
1074 #endif
1075                 {
1076                         inc.inc_faddr = ip->ip_src;
1077                         inc.inc_laddr = ip->ip_dst;
1078                 }
1079                 inc.inc_fport = th->th_sport;
1080                 inc.inc_lport = th->th_dport;
1081                 inc.inc_fibnum = so->so_fibnum;
1082
1083                 /*
1084                  * Check for an existing connection attempt in syncache if
1085                  * the flag is only ACK.  A successful lookup creates a new
1086                  * socket appended to the listen queue in SYN_RECEIVED state.
1087                  */
1088                 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1089
1090                         INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1091                         /*
1092                          * Parse the TCP options here because
1093                          * syncookies need access to the reflected
1094                          * timestamp.
1095                          */
1096                         tcp_dooptions(&to, optp, optlen, 0);
1097                         /*
1098                          * NB: syncache_expand() doesn't unlock
1099                          * inp and tcpinfo locks.
1100                          */
1101                         rstreason = syncache_expand(&inc, &to, th, &so, m);
1102                         if (rstreason < 0) {
1103                                 /*
1104                                  * A failing TCP MD5 signature comparison
1105                                  * must result in the segment being dropped
1106                                  * and must not produce any response back
1107                                  * to the sender.
1108                                  */
1109                                 goto dropunlock;
1110                         } else if (rstreason == 0) {
1111                                 /*
1112                                  * No syncache entry or ACK was not
1113                                  * for our SYN/ACK.  Send a RST.
1114                                  * NB: syncache did its own logging
1115                                  * of the failure cause.
1116                                  */
1117                                 rstreason = BANDLIM_RST_OPENPORT;
1118                                 goto dropwithreset;
1119                         }
1120 tfo_socket_result:
1121                         if (so == NULL) {
1122                                 /*
1123                                  * We completed the 3-way handshake
1124                                  * but could not allocate a socket
1125                                  * either due to memory shortage,
1126                                  * listen queue length limits or
1127                                  * global socket limits.  Send RST
1128                                  * or wait and have the remote end
1129                                  * retransmit the ACK for another
1130                                  * try.
1131                                  */
1132                                 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1133                                         log(LOG_DEBUG, "%s; %s: Listen socket: "
1134                                             "Socket allocation failed due to "
1135                                             "limits or memory shortage, %s\n",
1136                                             s, __func__,
1137                                             V_tcp_sc_rst_sock_fail ?
1138                                             "sending RST" : "try again");
1139                                 if (V_tcp_sc_rst_sock_fail) {
1140                                         rstreason = BANDLIM_UNLIMITED;
1141                                         goto dropwithreset;
1142                                 } else
1143                                         goto dropunlock;
1144                         }
1145                         /*
1146                          * Socket is created in state SYN_RECEIVED.
1147                          * Unlock the listen socket, lock the newly
1148                          * created socket and update the tp variable.
1149                          */
1150                         INP_WUNLOCK(inp);       /* listen socket */
1151                         inp = sotoinpcb(so);
1152                         /*
1153                          * New connection inpcb is already locked by
1154                          * syncache_expand().
1155                          */
1156                         INP_WLOCK_ASSERT(inp);
1157                         tp = intotcpcb(inp);
1158                         KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
1159                             ("%s: ", __func__));
1160                         /*
1161                          * Process the segment and the data it
1162                          * contains.  tcp_do_segment() consumes
1163                          * the mbuf chain and unlocks the inpcb.
1164                          */
1165                         TCP_PROBE5(receive, NULL, tp, m, tp, th);
1166                         tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
1167                             iptos);
1168                         if (ti_locked == TI_RLOCKED)
1169                                 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1170                         return (IPPROTO_DONE);
1171                 }
1172                 /*
1173                  * Segment flag validation for new connection attempts:
1174                  *
1175                  * Our (SYN|ACK) response was rejected.
1176                  * Check with syncache and remove entry to prevent
1177                  * retransmits.
1178                  *
1179                  * NB: syncache_chkrst does its own logging of failure
1180                  * causes.
1181                  */
1182                 if (thflags & TH_RST) {
1183                         syncache_chkrst(&inc, th, m);
1184                         goto dropunlock;
1185                 }
1186                 /*
1187                  * We can't do anything without SYN.
1188                  */
1189                 if ((thflags & TH_SYN) == 0) {
1190                         if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1191                                 log(LOG_DEBUG, "%s; %s: Listen socket: "
1192                                     "SYN is missing, segment ignored\n",
1193                                     s, __func__);
1194                         TCPSTAT_INC(tcps_badsyn);
1195                         goto dropunlock;
1196                 }
1197                 /*
1198                  * (SYN|ACK) is bogus on a listen socket.
1199                  */
1200                 if (thflags & TH_ACK) {
1201                         if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1202                                 log(LOG_DEBUG, "%s; %s: Listen socket: "
1203                                     "SYN|ACK invalid, segment rejected\n",
1204                                     s, __func__);
1205                         syncache_badack(&inc);  /* XXX: Not needed! */
1206                         TCPSTAT_INC(tcps_badsyn);
1207                         rstreason = BANDLIM_RST_OPENPORT;
1208                         goto dropwithreset;
1209                 }
1210                 /*
1211                  * If the drop_synfin option is enabled, drop all
1212                  * segments with both the SYN and FIN bits set.
1213                  * This prevents e.g. nmap from identifying the
1214                  * TCP/IP stack.
1215                  * XXX: Poor reasoning.  nmap has other methods
1216                  * and is constantly refining its stack detection
1217                  * strategies.
1218                  * XXX: This is a violation of the TCP specification
1219                  * and was used by RFC1644.
1220                  */
1221                 if ((thflags & TH_FIN) && V_drop_synfin) {
1222                         if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1223                                 log(LOG_DEBUG, "%s; %s: Listen socket: "
1224                                     "SYN|FIN segment ignored (based on "
1225                                     "sysctl setting)\n", s, __func__);
1226                         TCPSTAT_INC(tcps_badsyn);
1227                         goto dropunlock;
1228                 }
1229                 /*
1230                  * Segment's flags are (SYN) or (SYN|FIN).
1231                  *
1232                  * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1233                  * as they do not affect the state of the TCP FSM.
1234                  * The data pointed to by TH_URG and th_urp is ignored.
1235                  */
1236                 KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1237                     ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1238                 KASSERT(thflags & (TH_SYN),
1239                     ("%s: Listen socket: TH_SYN not set", __func__));
1240 #ifdef INET6
1241                 /*
1242                  * If deprecated address is forbidden,
1243                  * we do not accept SYN to deprecated interface
1244                  * address to prevent any new inbound connection from
1245                  * getting established.
1246                  * When we do not accept SYN, we send a TCP RST,
1247                  * with deprecated source address (instead of dropping
1248                  * it).  We compromise it as it is much better for peer
1249                  * to send a RST, and RST will be the final packet
1250                  * for the exchange.
1251                  *
1252                  * If we do not forbid deprecated addresses, we accept
1253                  * the SYN packet.  RFC2462 does not suggest dropping
1254                  * SYN in this case.
1255                  * If we decipher RFC2462 5.5.4, it says like this:
1256                  * 1. use of deprecated addr with existing
1257                  *    communication is okay - "SHOULD continue to be
1258                  *    used"
1259                  * 2. use of it with new communication:
1260                  *   (2a) "SHOULD NOT be used if alternate address
1261                  *        with sufficient scope is available"
1262                  *   (2b) nothing mentioned otherwise.
1263                  * Here we fall into (2b) case as we have no choice in
1264                  * our source address selection - we must obey the peer.
1265                  *
1266                  * The wording in RFC2462 is confusing, and there are
1267                  * multiple description text for deprecated address
1268                  * handling - worse, they are not exactly the same.
1269                  * I believe 5.5.4 is the best one, so we follow 5.5.4.
1270                  */
1271                 if (isipv6 && !V_ip6_use_deprecated) {
1272                         struct in6_ifaddr *ia6;
1273
1274                         ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
1275                         if (ia6 != NULL &&
1276                             (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
1277                                 ifa_free(&ia6->ia_ifa);
1278                                 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1279                                     log(LOG_DEBUG, "%s; %s: Listen socket: "
1280                                         "Connection attempt to deprecated "
1281                                         "IPv6 address rejected\n",
1282                                         s, __func__);
1283                                 rstreason = BANDLIM_RST_OPENPORT;
1284                                 goto dropwithreset;
1285                         }
1286                         if (ia6)
1287                                 ifa_free(&ia6->ia_ifa);
1288                 }
1289 #endif /* INET6 */
1290                 /*
1291                  * Basic sanity checks on incoming SYN requests:
1292                  *   Don't respond if the destination is a link layer
1293                  *      broadcast according to RFC1122 4.2.3.10, p. 104.
1294                  *   If it is from this socket it must be forged.
1295                  *   Don't respond if the source or destination is a
1296                  *      global or subnet broad- or multicast address.
1297                  *   Note that it is quite possible to receive unicast
1298                  *      link-layer packets with a broadcast IP address. Use
1299                  *      in_broadcast() to find them.
1300                  */
1301                 if (m->m_flags & (M_BCAST|M_MCAST)) {
1302                         if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1303                             log(LOG_DEBUG, "%s; %s: Listen socket: "
1304                                 "Connection attempt from broad- or multicast "
1305                                 "link layer address ignored\n", s, __func__);
1306                         goto dropunlock;
1307                 }
1308 #ifdef INET6
1309                 if (isipv6) {
1310                         if (th->th_dport == th->th_sport &&
1311                             IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1312                                 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1313                                     log(LOG_DEBUG, "%s; %s: Listen socket: "
1314                                         "Connection attempt to/from self "
1315                                         "ignored\n", s, __func__);
1316                                 goto dropunlock;
1317                         }
1318                         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1319                             IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1320                                 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1321                                     log(LOG_DEBUG, "%s; %s: Listen socket: "
1322                                         "Connection attempt from/to multicast "
1323                                         "address ignored\n", s, __func__);
1324                                 goto dropunlock;
1325                         }
1326                 }
1327 #endif
1328 #if defined(INET) && defined(INET6)
1329                 else
1330 #endif
1331 #ifdef INET
1332                 {
1333                         if (th->th_dport == th->th_sport &&
1334                             ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1335                                 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1336                                     log(LOG_DEBUG, "%s; %s: Listen socket: "
1337                                         "Connection attempt from/to self "
1338                                         "ignored\n", s, __func__);
1339                                 goto dropunlock;
1340                         }
1341                         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1342                             IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
1343                             ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1344                             in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1345                                 if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1346                                     log(LOG_DEBUG, "%s; %s: Listen socket: "
1347                                         "Connection attempt from/to broad- "
1348                                         "or multicast address ignored\n",
1349                                         s, __func__);
1350                                 goto dropunlock;
1351                         }
1352                 }
1353 #endif
1354                 /*
1355                  * SYN appears to be valid.  Create compressed TCP state
1356                  * for syncache.
1357                  */
1358 #ifdef TCPDEBUG
1359                 if (so->so_options & SO_DEBUG)
1360                         tcp_trace(TA_INPUT, ostate, tp,
1361                             (void *)tcp_saveipgen, &tcp_savetcp, 0);
1362 #endif
1363                 TCP_PROBE3(debug__input, tp, th, m);
1364                 tcp_dooptions(&to, optp, optlen, TO_SYN);
1365                 if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL, iptos))
1366                         goto tfo_socket_result;
1367
1368                 /*
1369                  * Entry added to syncache and mbuf consumed.
1370                  * Only the listen socket is unlocked by syncache_add().
1371                  */
1372                 if (ti_locked == TI_RLOCKED) {
1373                         INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1374                         ti_locked = TI_UNLOCKED;
1375                 }
1376                 INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
1377                 return (IPPROTO_DONE);
1378         } else if (tp->t_state == TCPS_LISTEN) {
1379                 /*
1380                  * When a listen socket is torn down the SO_ACCEPTCONN
1381                  * flag is removed first while connections are drained
1382                  * from the accept queue in a unlock/lock cycle of the
1383                  * ACCEPT_LOCK, opening a race condition allowing a SYN
1384                  * attempt go through unhandled.
1385                  */
1386                 goto dropunlock;
1387         }
1388 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1389         if (tp->t_flags & TF_SIGNATURE) {
1390                 tcp_dooptions(&to, optp, optlen, thflags);
1391                 if ((to.to_flags & TOF_SIGNATURE) == 0) {
1392                         TCPSTAT_INC(tcps_sig_err_nosigopt);
1393                         goto dropunlock;
1394                 }
1395                 if (!TCPMD5_ENABLED() ||
1396                     TCPMD5_INPUT(m, th, to.to_signature) != 0)
1397                         goto dropunlock;
1398         }
1399 #endif
1400         TCP_PROBE5(receive, NULL, tp, m, tp, th);
1401
1402         /*
1403          * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
1404          * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
1405          * the inpcb, and unlocks pcbinfo.
1406          */
1407         tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos);
1408         if (ti_locked == TI_RLOCKED)
1409                 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1410         return (IPPROTO_DONE);
1411
1412 dropwithreset:
1413         TCP_PROBE5(receive, NULL, tp, m, tp, th);
1414
1415         if (ti_locked == TI_RLOCKED) {
1416                 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1417                 ti_locked = TI_UNLOCKED;
1418         }
1419 #ifdef INVARIANTS
1420         else {
1421                 KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1422                     "ti_locked: %d", __func__, ti_locked));
1423                 INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
1424         }
1425 #endif
1426
1427         if (inp != NULL) {
1428                 tcp_dropwithreset(m, th, tp, tlen, rstreason);
1429                 INP_WUNLOCK(inp);
1430         } else
1431                 tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1432         m = NULL;       /* mbuf chain got consumed. */
1433         goto drop;
1434
1435 dropunlock:
1436         if (m != NULL)
1437                 TCP_PROBE5(receive, NULL, tp, m, tp, th);
1438
1439         if (ti_locked == TI_RLOCKED) {
1440                 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1441                 ti_locked = TI_UNLOCKED;
1442         }
1443 #ifdef INVARIANTS
1444         else {
1445                 KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1446                     "ti_locked: %d", __func__, ti_locked));
1447                 INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
1448         }
1449 #endif
1450
1451         if (inp != NULL)
1452                 INP_WUNLOCK(inp);
1453
1454 drop:
1455         INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
1456         if (s != NULL)
1457                 free(s, M_TCPLOG);
1458         if (m != NULL)
1459                 m_freem(m);
1460         return (IPPROTO_DONE);
1461 }
1462
1463 /*
1464  * Automatic sizing of receive socket buffer.  Often the send
1465  * buffer size is not optimally adjusted to the actual network
1466  * conditions at hand (delay bandwidth product).  Setting the
1467  * buffer size too small limits throughput on links with high
1468  * bandwidth and high delay (eg. trans-continental/oceanic links).
1469  *
1470  * On the receive side the socket buffer memory is only rarely
1471  * used to any significant extent.  This allows us to be much
1472  * more aggressive in scaling the receive socket buffer.  For
1473  * the case that the buffer space is actually used to a large
1474  * extent and we run out of kernel memory we can simply drop
1475  * the new segments; TCP on the sender will just retransmit it
1476  * later.  Setting the buffer size too big may only consume too
1477  * much kernel memory if the application doesn't read() from
1478  * the socket or packet loss or reordering makes use of the
1479  * reassembly queue.
1480  *
1481  * The criteria to step up the receive buffer one notch are:
1482  *  1. Application has not set receive buffer size with
1483  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1484  *  2. the number of bytes received during the time it takes
1485  *     one timestamp to be reflected back to us (the RTT);
1486  *  3. received bytes per RTT is within seven eighth of the
1487  *     current socket buffer size;
1488  *  4. receive buffer size has not hit maximal automatic size;
1489  *
1490  * This algorithm does one step per RTT at most and only if
1491  * we receive a bulk stream w/o packet losses or reorderings.
1492  * Shrinking the buffer during idle times is not necessary as
1493  * it doesn't consume any memory when idle.
1494  *
1495  * TODO: Only step up if the application is actually serving
1496  * the buffer to better manage the socket buffer resources.
1497  */
1498 int
1499 tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1500     struct tcpcb *tp, int tlen)
1501 {
1502         int newsize = 0;
1503
1504         if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1505             tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1506             TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1507             (tp->t_srtt >> TCP_RTT_SHIFT)) {
1508                 if (tp->rfbuf_cnt > (so->so_rcv.sb_hiwat / 8 * 7) &&
1509                     so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1510                         newsize = min(so->so_rcv.sb_hiwat +
1511                             V_tcp_autorcvbuf_inc, V_tcp_autorcvbuf_max);
1512                 }
1513                 TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1514
1515                 /* Start over with next RTT. */
1516                 tp->rfbuf_ts = 0;
1517                 tp->rfbuf_cnt = 0;
1518         } else {
1519                 tp->rfbuf_cnt += tlen;  /* add up */
1520         }
1521         return (newsize);
1522 }
1523
1524 void
1525 tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1526     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos)
1527 {
1528         int thflags, acked, ourfinisacked, needoutput = 0, sack_changed;
1529         int rstreason, todrop, win, incforsyn = 0;
1530         uint32_t tiwin;
1531         uint16_t nsegs;
1532         char *s;
1533         struct in_conninfo *inc;
1534         struct mbuf *mfree;
1535         struct tcpopt to;
1536         int tfo_syn;
1537         
1538 #ifdef TCPDEBUG
1539         /*
1540          * The size of tcp_saveipgen must be the size of the max ip header,
1541          * now IPv6.
1542          */
1543         u_char tcp_saveipgen[IP6_HDR_LEN];
1544         struct tcphdr tcp_savetcp;
1545         short ostate = 0;
1546 #endif
1547         thflags = th->th_flags;
1548         inc = &tp->t_inpcb->inp_inc;
1549         tp->sackhint.last_sack_ack = 0;
1550         sack_changed = 0;
1551         nsegs = max(1, m->m_pkthdr.lro_nsegs);
1552         /*
1553          * If this is either a state-changing packet or current state isn't
1554          * established, we require a write lock on tcbinfo.  Otherwise, we
1555          * allow the tcbinfo to be in either alocked or unlocked, as the
1556          * caller may have unnecessarily acquired a write lock due to a race.
1557          */
1558         if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1559             tp->t_state != TCPS_ESTABLISHED) {
1560                 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1561         }
1562         INP_WLOCK_ASSERT(tp->t_inpcb);
1563         KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1564             __func__));
1565         KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1566             __func__));
1567
1568 #ifdef TCPPCAP
1569         /* Save segment, if requested. */
1570         tcp_pcap_add(th, m, &(tp->t_inpkts));
1571 #endif
1572         TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
1573             tlen, NULL, true);
1574
1575         if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1576                 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1577                         log(LOG_DEBUG, "%s; %s: "
1578                             "SYN|FIN segment ignored (based on "
1579                             "sysctl setting)\n", s, __func__);
1580                         free(s, M_TCPLOG);
1581                 }
1582                 goto drop;
1583         }
1584
1585         /*
1586          * If a segment with the ACK-bit set arrives in the SYN-SENT state
1587          * check SEQ.ACK first.
1588          */
1589         if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1590             (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1591                 rstreason = BANDLIM_UNLIMITED;
1592                 goto dropwithreset;
1593         }
1594
1595         /*
1596          * Segment received on connection.
1597          * Reset idle time and keep-alive timer.
1598          * XXX: This should be done after segment
1599          * validation to ignore broken/spoofed segs.
1600          */
1601         tp->t_rcvtime = ticks;
1602
1603         /*
1604          * Scale up the window into a 32-bit value.
1605          * For the SYN_SENT state the scale is zero.
1606          */
1607         tiwin = th->th_win << tp->snd_scale;
1608
1609         /*
1610          * TCP ECN processing.
1611          */
1612         if (tp->t_flags & TF_ECN_PERMIT) {
1613                 if (thflags & TH_CWR)
1614                         tp->t_flags &= ~TF_ECN_SND_ECE;
1615                 switch (iptos & IPTOS_ECN_MASK) {
1616                 case IPTOS_ECN_CE:
1617                         tp->t_flags |= TF_ECN_SND_ECE;
1618                         TCPSTAT_INC(tcps_ecn_ce);
1619                         break;
1620                 case IPTOS_ECN_ECT0:
1621                         TCPSTAT_INC(tcps_ecn_ect0);
1622                         break;
1623                 case IPTOS_ECN_ECT1:
1624                         TCPSTAT_INC(tcps_ecn_ect1);
1625                         break;
1626                 }
1627
1628                 /* Process a packet differently from RFC3168. */
1629                 cc_ecnpkt_handler(tp, th, iptos);
1630
1631                 /* Congestion experienced. */
1632                 if (thflags & TH_ECE) {
1633                         cc_cong_signal(tp, th, CC_ECN);
1634                 }
1635         }
1636
1637         /*
1638          * Parse options on any incoming segment.
1639          */
1640         tcp_dooptions(&to, (u_char *)(th + 1),
1641             (th->th_off << 2) - sizeof(struct tcphdr),
1642             (thflags & TH_SYN) ? TO_SYN : 0);
1643
1644 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1645         if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1646             (to.to_flags & TOF_SIGNATURE) == 0) {
1647                 TCPSTAT_INC(tcps_sig_err_sigopt);
1648                 /* XXX: should drop? */
1649         }
1650 #endif
1651         /*
1652          * If echoed timestamp is later than the current time,
1653          * fall back to non RFC1323 RTT calculation.  Normalize
1654          * timestamp if syncookies were used when this connection
1655          * was established.
1656          */
1657         if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1658                 to.to_tsecr -= tp->ts_offset;
1659                 if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1660                         to.to_tsecr = 0;
1661                 else if (tp->t_flags & TF_PREVVALID &&
1662                          tp->t_badrxtwin != 0 && SEQ_LT(to.to_tsecr, tp->t_badrxtwin))
1663                         cc_cong_signal(tp, th, CC_RTO_ERR);
1664         }
1665         /*
1666          * Process options only when we get SYN/ACK back. The SYN case
1667          * for incoming connections is handled in tcp_syncache.
1668          * According to RFC1323 the window field in a SYN (i.e., a <SYN>
1669          * or <SYN,ACK>) segment itself is never scaled.
1670          * XXX this is traditional behavior, may need to be cleaned up.
1671          */
1672         if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1673                 if ((to.to_flags & TOF_SCALE) &&
1674                     (tp->t_flags & TF_REQ_SCALE)) {
1675                         tp->t_flags |= TF_RCVD_SCALE;
1676                         tp->snd_scale = to.to_wscale;
1677                 } else
1678                         tp->t_flags &= ~TF_REQ_SCALE;
1679                 /*
1680                  * Initial send window.  It will be updated with
1681                  * the next incoming segment to the scaled value.
1682                  */
1683                 tp->snd_wnd = th->th_win;
1684                 if ((to.to_flags & TOF_TS) &&
1685                     (tp->t_flags & TF_REQ_TSTMP)) {
1686                         tp->t_flags |= TF_RCVD_TSTMP;
1687                         tp->ts_recent = to.to_tsval;
1688                         tp->ts_recent_age = tcp_ts_getticks();
1689                 } else
1690                         tp->t_flags &= ~TF_REQ_TSTMP;
1691                 if (to.to_flags & TOF_MSS)
1692                         tcp_mss(tp, to.to_mss);
1693                 if ((tp->t_flags & TF_SACK_PERMIT) &&
1694                     (to.to_flags & TOF_SACKPERM) == 0)
1695                         tp->t_flags &= ~TF_SACK_PERMIT;
1696                 if (IS_FASTOPEN(tp->t_flags)) {
1697                         if (to.to_flags & TOF_FASTOPEN) {
1698                                 uint16_t mss;
1699
1700                                 if (to.to_flags & TOF_MSS)
1701                                         mss = to.to_mss;
1702                                 else
1703                                         if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
1704                                                 mss = TCP6_MSS;
1705                                         else
1706                                                 mss = TCP_MSS;
1707                                 tcp_fastopen_update_cache(tp, mss,
1708                                     to.to_tfo_len, to.to_tfo_cookie);
1709                         } else
1710                                 tcp_fastopen_disable_path(tp);
1711                 }
1712         }
1713
1714         /*
1715          * If timestamps were negotiated during SYN/ACK they should
1716          * appear on every segment during this session and vice versa.
1717          */
1718         if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
1719                 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1720                         log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1721                             "no action\n", s, __func__);
1722                         free(s, M_TCPLOG);
1723                 }
1724         }
1725         if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
1726                 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1727                         log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
1728                             "no action\n", s, __func__);
1729                         free(s, M_TCPLOG);
1730                 }
1731         }
1732
1733         /*
1734          * Header prediction: check for the two common cases
1735          * of a uni-directional data xfer.  If the packet has
1736          * no control flags, is in-sequence, the window didn't
1737          * change and we're not retransmitting, it's a
1738          * candidate.  If the length is zero and the ack moved
1739          * forward, we're the sender side of the xfer.  Just
1740          * free the data acked & wake any higher level process
1741          * that was blocked waiting for space.  If the length
1742          * is non-zero and the ack didn't move, we're the
1743          * receiver side.  If we're getting packets in-order
1744          * (the reassembly queue is empty), add the data to
1745          * the socket buffer and note that we need a delayed ack.
1746          * Make sure that the hidden state-flags are also off.
1747          * Since we check for TCPS_ESTABLISHED first, it can only
1748          * be TH_NEEDSYN.
1749          */
1750         if (tp->t_state == TCPS_ESTABLISHED &&
1751             th->th_seq == tp->rcv_nxt &&
1752             (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1753             tp->snd_nxt == tp->snd_max &&
1754             tiwin && tiwin == tp->snd_wnd && 
1755             ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1756             SEGQ_EMPTY(tp) &&
1757             ((to.to_flags & TOF_TS) == 0 ||
1758              TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1759
1760                 /*
1761                  * If last ACK falls within this segment's sequence numbers,
1762                  * record the timestamp.
1763                  * NOTE that the test is modified according to the latest
1764                  * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1765                  */
1766                 if ((to.to_flags & TOF_TS) != 0 &&
1767                     SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1768                         tp->ts_recent_age = tcp_ts_getticks();
1769                         tp->ts_recent = to.to_tsval;
1770                 }
1771
1772                 if (tlen == 0) {
1773                         if (SEQ_GT(th->th_ack, tp->snd_una) &&
1774                             SEQ_LEQ(th->th_ack, tp->snd_max) &&
1775                             !IN_RECOVERY(tp->t_flags) &&
1776                             (to.to_flags & TOF_SACK) == 0 &&
1777                             TAILQ_EMPTY(&tp->snd_holes)) {
1778                                 /*
1779                                  * This is a pure ack for outstanding data.
1780                                  */
1781                                 TCPSTAT_INC(tcps_predack);
1782
1783                                 /*
1784                                  * "bad retransmit" recovery without timestamps.
1785                                  */
1786                                 if ((to.to_flags & TOF_TS) == 0 &&
1787                                     tp->t_rxtshift == 1 &&
1788                                     tp->t_flags & TF_PREVVALID &&
1789                                     (int)(ticks - tp->t_badrxtwin) < 0) {
1790                                         cc_cong_signal(tp, th, CC_RTO_ERR);
1791                                 }
1792
1793                                 /*
1794                                  * Recalculate the transmit timer / rtt.
1795                                  *
1796                                  * Some boxes send broken timestamp replies
1797                                  * during the SYN+ACK phase, ignore
1798                                  * timestamps of 0 or we could calculate a
1799                                  * huge RTT and blow up the retransmit timer.
1800                                  */
1801                                 if ((to.to_flags & TOF_TS) != 0 &&
1802                                     to.to_tsecr) {
1803                                         uint32_t t;
1804
1805                                         t = tcp_ts_getticks() - to.to_tsecr;
1806                                         if (!tp->t_rttlow || tp->t_rttlow > t)
1807                                                 tp->t_rttlow = t;
1808                                         tcp_xmit_timer(tp,
1809                                             TCP_TS_TO_TICKS(t) + 1);
1810                                 } else if (tp->t_rtttime &&
1811                                     SEQ_GT(th->th_ack, tp->t_rtseq)) {
1812                                         if (!tp->t_rttlow ||
1813                                             tp->t_rttlow > ticks - tp->t_rtttime)
1814                                                 tp->t_rttlow = ticks - tp->t_rtttime;
1815                                         tcp_xmit_timer(tp,
1816                                                         ticks - tp->t_rtttime);
1817                                 }
1818                                 acked = BYTES_THIS_ACK(tp, th);
1819
1820 #ifdef TCP_HHOOK
1821                                 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
1822                                 hhook_run_tcp_est_in(tp, th, &to);
1823 #endif
1824
1825                                 TCPSTAT_ADD(tcps_rcvackpack, nsegs);
1826                                 TCPSTAT_ADD(tcps_rcvackbyte, acked);
1827                                 sbdrop(&so->so_snd, acked);
1828                                 if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
1829                                     SEQ_LEQ(th->th_ack, tp->snd_recover))
1830                                         tp->snd_recover = th->th_ack - 1;
1831                                 
1832                                 /*
1833                                  * Let the congestion control algorithm update
1834                                  * congestion control related information. This
1835                                  * typically means increasing the congestion
1836                                  * window.
1837                                  */
1838                                 cc_ack_received(tp, th, nsegs, CC_ACK);
1839
1840                                 tp->snd_una = th->th_ack;
1841                                 /*
1842                                  * Pull snd_wl2 up to prevent seq wrap relative
1843                                  * to th_ack.
1844                                  */
1845                                 tp->snd_wl2 = th->th_ack;
1846                                 tp->t_dupacks = 0;
1847                                 m_freem(m);
1848
1849                                 /*
1850                                  * If all outstanding data are acked, stop
1851                                  * retransmit timer, otherwise restart timer
1852                                  * using current (possibly backed-off) value.
1853                                  * If process is waiting for space,
1854                                  * wakeup/selwakeup/signal.  If data
1855                                  * are ready to send, let tcp_output
1856                                  * decide between more output or persist.
1857                                  */
1858 #ifdef TCPDEBUG
1859                                 if (so->so_options & SO_DEBUG)
1860                                         tcp_trace(TA_INPUT, ostate, tp,
1861                                             (void *)tcp_saveipgen,
1862                                             &tcp_savetcp, 0);
1863 #endif
1864                                 TCP_PROBE3(debug__input, tp, th, m);
1865                                 if (tp->snd_una == tp->snd_max)
1866                                         tcp_timer_activate(tp, TT_REXMT, 0);
1867                                 else if (!tcp_timer_active(tp, TT_PERSIST))
1868                                         tcp_timer_activate(tp, TT_REXMT,
1869                                                       tp->t_rxtcur);
1870                                 sowwakeup(so);
1871                                 if (sbavail(&so->so_snd))
1872                                         (void) tp->t_fb->tfb_tcp_output(tp);
1873                                 goto check_delack;
1874                         }
1875                 } else if (th->th_ack == tp->snd_una &&
1876                     tlen <= sbspace(&so->so_rcv)) {
1877                         int newsize = 0;        /* automatic sockbuf scaling */
1878
1879                         /*
1880                          * This is a pure, in-sequence data packet with
1881                          * nothing on the reassembly queue and we have enough
1882                          * buffer space to take it.
1883                          */
1884                         /* Clean receiver SACK report if present */
1885                         if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
1886                                 tcp_clean_sackreport(tp);
1887                         TCPSTAT_INC(tcps_preddat);
1888                         tp->rcv_nxt += tlen;
1889                         /*
1890                          * Pull snd_wl1 up to prevent seq wrap relative to
1891                          * th_seq.
1892                          */
1893                         tp->snd_wl1 = th->th_seq;
1894                         /*
1895                          * Pull rcv_up up to prevent seq wrap relative to
1896                          * rcv_nxt.
1897                          */
1898                         tp->rcv_up = tp->rcv_nxt;
1899                         TCPSTAT_ADD(tcps_rcvpack, nsegs);
1900                         TCPSTAT_ADD(tcps_rcvbyte, tlen);
1901 #ifdef TCPDEBUG
1902                         if (so->so_options & SO_DEBUG)
1903                                 tcp_trace(TA_INPUT, ostate, tp,
1904                                     (void *)tcp_saveipgen, &tcp_savetcp, 0);
1905 #endif
1906                         TCP_PROBE3(debug__input, tp, th, m);
1907
1908                         newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
1909
1910                         /* Add data to socket buffer. */
1911                         SOCKBUF_LOCK(&so->so_rcv);
1912                         if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1913                                 m_freem(m);
1914                         } else {
1915                                 /*
1916                                  * Set new socket buffer size.
1917                                  * Give up when limit is reached.
1918                                  */
1919                                 if (newsize)
1920                                         if (!sbreserve_locked(&so->so_rcv,
1921                                             newsize, so, NULL))
1922                                                 so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1923                                 m_adj(m, drop_hdrlen);  /* delayed header drop */
1924                                 sbappendstream_locked(&so->so_rcv, m, 0);
1925                         }
1926                         /* NB: sorwakeup_locked() does an implicit unlock. */
1927                         sorwakeup_locked(so);
1928                         if (DELAY_ACK(tp, tlen)) {
1929                                 tp->t_flags |= TF_DELACK;
1930                         } else {
1931                                 tp->t_flags |= TF_ACKNOW;
1932                                 tp->t_fb->tfb_tcp_output(tp);
1933                         }
1934                         goto check_delack;
1935                 }
1936         }
1937
1938         /*
1939          * Calculate amount of space in receive window,
1940          * and then do TCP input processing.
1941          * Receive window is amount of space in rcv queue,
1942          * but not less than advertised window.
1943          */
1944         win = sbspace(&so->so_rcv);
1945         if (win < 0)
1946                 win = 0;
1947         tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1948
1949         switch (tp->t_state) {
1950
1951         /*
1952          * If the state is SYN_RECEIVED:
1953          *      if seg contains an ACK, but not for our SYN/ACK, send a RST.
1954          */
1955         case TCPS_SYN_RECEIVED:
1956                 if ((thflags & TH_ACK) &&
1957                     (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1958                      SEQ_GT(th->th_ack, tp->snd_max))) {
1959                                 rstreason = BANDLIM_RST_OPENPORT;
1960                                 goto dropwithreset;
1961                 }
1962                 if (IS_FASTOPEN(tp->t_flags)) {
1963                         /*
1964                          * When a TFO connection is in SYN_RECEIVED, the
1965                          * only valid packets are the initial SYN, a
1966                          * retransmit/copy of the initial SYN (possibly with
1967                          * a subset of the original data), a valid ACK, a
1968                          * FIN, or a RST.
1969                          */
1970                         if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1971                                 rstreason = BANDLIM_RST_OPENPORT;
1972                                 goto dropwithreset;
1973                         } else if (thflags & TH_SYN) {
1974                                 /* non-initial SYN is ignored */
1975                                 if ((tcp_timer_active(tp, TT_DELACK) || 
1976                                      tcp_timer_active(tp, TT_REXMT)))
1977                                         goto drop;
1978                         } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
1979                                 goto drop;
1980                         }
1981                 }
1982                 break;
1983
1984         /*
1985          * If the state is SYN_SENT:
1986          *      if seg contains a RST with valid ACK (SEQ.ACK has already
1987          *          been verified), then drop the connection.
1988          *      if seg contains a RST without an ACK, drop the seg.
1989          *      if seg does not contain SYN, then drop the seg.
1990          * Otherwise this is an acceptable SYN segment
1991          *      initialize tp->rcv_nxt and tp->irs
1992          *      if seg contains ack then advance tp->snd_una
1993          *      if seg contains an ECE and ECN support is enabled, the stream
1994          *          is ECN capable.
1995          *      if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1996          *      arrange for segment to be acked (eventually)
1997          *      continue processing rest of data/controls, beginning with URG
1998          */
1999         case TCPS_SYN_SENT:
2000                 if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
2001                         TCP_PROBE5(connect__refused, NULL, tp,
2002                             m, tp, th);
2003                         tp = tcp_drop(tp, ECONNREFUSED);
2004                 }
2005                 if (thflags & TH_RST)
2006                         goto drop;
2007                 if (!(thflags & TH_SYN))
2008                         goto drop;
2009
2010                 tp->irs = th->th_seq;
2011                 tcp_rcvseqinit(tp);
2012                 if (thflags & TH_ACK) {
2013                         int tfo_partial_ack = 0;
2014
2015                         TCPSTAT_INC(tcps_connects);
2016                         soisconnected(so);
2017 #ifdef MAC
2018                         mac_socketpeer_set_from_mbuf(m, so);
2019 #endif
2020                         /* Do window scaling on this connection? */
2021                         if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2022                                 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2023                                 tp->rcv_scale = tp->request_r_scale;
2024                         }
2025                         tp->rcv_adv += min(tp->rcv_wnd,
2026                             TCP_MAXWIN << tp->rcv_scale);
2027                         tp->snd_una++;          /* SYN is acked */
2028                         /*
2029                          * If not all the data that was sent in the TFO SYN
2030                          * has been acked, resend the remainder right away.
2031                          */
2032                         if (IS_FASTOPEN(tp->t_flags) &&
2033                             (tp->snd_una != tp->snd_max)) {
2034                                 tp->snd_nxt = th->th_ack;
2035                                 tfo_partial_ack = 1;
2036                         }
2037                         /*
2038                          * If there's data, delay ACK; if there's also a FIN
2039                          * ACKNOW will be turned on later.
2040                          */
2041                         if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
2042                                 tcp_timer_activate(tp, TT_DELACK,
2043                                     tcp_delacktime);
2044                         else
2045                                 tp->t_flags |= TF_ACKNOW;
2046
2047                         if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) &&
2048                             V_tcp_do_ecn) {
2049                                 tp->t_flags |= TF_ECN_PERMIT;
2050                                 TCPSTAT_INC(tcps_ecn_shs);
2051                         }
2052                         
2053                         /*
2054                          * Received <SYN,ACK> in SYN_SENT[*] state.
2055                          * Transitions:
2056                          *      SYN_SENT  --> ESTABLISHED
2057                          *      SYN_SENT* --> FIN_WAIT_1
2058                          */
2059                         tp->t_starttime = ticks;
2060                         if (tp->t_flags & TF_NEEDFIN) {
2061                                 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2062                                 tp->t_flags &= ~TF_NEEDFIN;
2063                                 thflags &= ~TH_SYN;
2064                         } else {
2065                                 tcp_state_change(tp, TCPS_ESTABLISHED);
2066                                 TCP_PROBE5(connect__established, NULL, tp,
2067                                     m, tp, th);
2068                                 cc_conn_init(tp);
2069                                 tcp_timer_activate(tp, TT_KEEP,
2070                                     TP_KEEPIDLE(tp));
2071                         }
2072                 } else {
2073                         /*
2074                          * Received initial SYN in SYN-SENT[*] state =>
2075                          * simultaneous open.
2076                          * If it succeeds, connection is * half-synchronized.
2077                          * Otherwise, do 3-way handshake:
2078                          *        SYN-SENT -> SYN-RECEIVED
2079                          *        SYN-SENT* -> SYN-RECEIVED*
2080                          */
2081                         tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
2082                         tcp_timer_activate(tp, TT_REXMT, 0);
2083                         tcp_state_change(tp, TCPS_SYN_RECEIVED);
2084                 }
2085
2086                 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2087                 INP_WLOCK_ASSERT(tp->t_inpcb);
2088
2089                 /*
2090                  * Advance th->th_seq to correspond to first data byte.
2091                  * If data, trim to stay within window,
2092                  * dropping FIN if necessary.
2093                  */
2094                 th->th_seq++;
2095                 if (tlen > tp->rcv_wnd) {
2096                         todrop = tlen - tp->rcv_wnd;
2097                         m_adj(m, -todrop);
2098                         tlen = tp->rcv_wnd;
2099                         thflags &= ~TH_FIN;
2100                         TCPSTAT_INC(tcps_rcvpackafterwin);
2101                         TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2102                 }
2103                 tp->snd_wl1 = th->th_seq - 1;
2104                 tp->rcv_up = th->th_seq;
2105                 /*
2106                  * Client side of transaction: already sent SYN and data.
2107                  * If the remote host used T/TCP to validate the SYN,
2108                  * our data will be ACK'd; if so, enter normal data segment
2109                  * processing in the middle of step 5, ack processing.
2110                  * Otherwise, goto step 6.
2111                  */
2112                 if (thflags & TH_ACK)
2113                         goto process_ACK;
2114
2115                 goto step6;
2116
2117         /*
2118          * If the state is LAST_ACK or CLOSING or TIME_WAIT:
2119          *      do normal processing.
2120          *
2121          * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
2122          */
2123         case TCPS_LAST_ACK:
2124         case TCPS_CLOSING:
2125                 break;  /* continue normal processing */
2126         }
2127
2128         /*
2129          * States other than LISTEN or SYN_SENT.
2130          * First check the RST flag and sequence number since reset segments
2131          * are exempt from the timestamp and connection count tests.  This
2132          * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
2133          * below which allowed reset segments in half the sequence space
2134          * to fall though and be processed (which gives forged reset
2135          * segments with a random sequence number a 50 percent chance of
2136          * killing a connection).
2137          * Then check timestamp, if present.
2138          * Then check the connection count, if present.
2139          * Then check that at least some bytes of segment are within
2140          * receive window.  If segment begins before rcv_nxt,
2141          * drop leading data (and SYN); if nothing left, just ack.
2142          */
2143         if (thflags & TH_RST) {
2144                 /*
2145                  * RFC5961 Section 3.2
2146                  *
2147                  * - RST drops connection only if SEG.SEQ == RCV.NXT.
2148                  * - If RST is in window, we send challenge ACK.
2149                  *
2150                  * Note: to take into account delayed ACKs, we should
2151                  *   test against last_ack_sent instead of rcv_nxt.
2152                  * Note 2: we handle special case of closed window, not
2153                  *   covered by the RFC.
2154                  */
2155                 if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
2156                     SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
2157                     (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
2158
2159                         INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2160                         KASSERT(tp->t_state != TCPS_SYN_SENT,
2161                             ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
2162                             __func__, th, tp));
2163
2164                         if (V_tcp_insecure_rst ||
2165                             tp->last_ack_sent == th->th_seq) {
2166                                 TCPSTAT_INC(tcps_drops);
2167                                 /* Drop the connection. */
2168                                 switch (tp->t_state) {
2169                                 case TCPS_SYN_RECEIVED:
2170                                         so->so_error = ECONNREFUSED;
2171                                         goto close;
2172                                 case TCPS_ESTABLISHED:
2173                                 case TCPS_FIN_WAIT_1:
2174                                 case TCPS_FIN_WAIT_2:
2175                                 case TCPS_CLOSE_WAIT:
2176                                 case TCPS_CLOSING:
2177                                 case TCPS_LAST_ACK:
2178                                         so->so_error = ECONNRESET;
2179                                 close:
2180                                         /* FALLTHROUGH */
2181                                 default:
2182                                         tp = tcp_close(tp);
2183                                 }
2184                         } else {
2185                                 TCPSTAT_INC(tcps_badrst);
2186                                 /* Send challenge ACK. */
2187                                 tcp_respond(tp, mtod(m, void *), th, m,
2188                                     tp->rcv_nxt, tp->snd_nxt, TH_ACK);
2189                                 tp->last_ack_sent = tp->rcv_nxt;
2190                                 m = NULL;
2191                         }
2192                 }
2193                 goto drop;
2194         }
2195
2196         /*
2197          * RFC5961 Section 4.2
2198          * Send challenge ACK for any SYN in synchronized state.
2199          */
2200         if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2201             tp->t_state != TCPS_SYN_RECEIVED) {
2202                 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2203
2204                 TCPSTAT_INC(tcps_badsyn);
2205                 if (V_tcp_insecure_syn &&
2206                     SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
2207                     SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
2208                         tp = tcp_drop(tp, ECONNRESET);
2209                         rstreason = BANDLIM_UNLIMITED;
2210                 } else {
2211                         /* Send challenge ACK. */
2212                         tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
2213                             tp->snd_nxt, TH_ACK);
2214                         tp->last_ack_sent = tp->rcv_nxt;
2215                         m = NULL;
2216                 }
2217                 goto drop;
2218         }
2219
2220         /*
2221          * RFC 1323 PAWS: If we have a timestamp reply on this segment
2222          * and it's less than ts_recent, drop it.
2223          */
2224         if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
2225             TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2226
2227                 /* Check to see if ts_recent is over 24 days old.  */
2228                 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2229                         /*
2230                          * Invalidate ts_recent.  If this segment updates
2231                          * ts_recent, the age will be reset later and ts_recent
2232                          * will get a valid value.  If it does not, setting
2233                          * ts_recent to zero will at least satisfy the
2234                          * requirement that zero be placed in the timestamp
2235                          * echo reply when ts_recent isn't valid.  The
2236                          * age isn't reset until we get a valid ts_recent
2237                          * because we don't want out-of-order segments to be
2238                          * dropped when ts_recent is old.
2239                          */
2240                         tp->ts_recent = 0;
2241                 } else {
2242                         TCPSTAT_INC(tcps_rcvduppack);
2243                         TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
2244                         TCPSTAT_INC(tcps_pawsdrop);
2245                         if (tlen)
2246                                 goto dropafterack;
2247                         goto drop;
2248                 }
2249         }
2250
2251         /*
2252          * In the SYN-RECEIVED state, validate that the packet belongs to
2253          * this connection before trimming the data to fit the receive
2254          * window.  Check the sequence number versus IRS since we know
2255          * the sequence numbers haven't wrapped.  This is a partial fix
2256          * for the "LAND" DoS attack.
2257          */
2258         if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2259                 rstreason = BANDLIM_RST_OPENPORT;
2260                 goto dropwithreset;
2261         }
2262
2263         todrop = tp->rcv_nxt - th->th_seq;
2264         if (todrop > 0) {
2265                 if (thflags & TH_SYN) {
2266                         thflags &= ~TH_SYN;
2267                         th->th_seq++;
2268                         if (th->th_urp > 1)
2269                                 th->th_urp--;
2270                         else
2271                                 thflags &= ~TH_URG;
2272                         todrop--;
2273                 }
2274                 /*
2275                  * Following if statement from Stevens, vol. 2, p. 960.
2276                  */
2277                 if (todrop > tlen
2278                     || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2279                         /*
2280                          * Any valid FIN must be to the left of the window.
2281                          * At this point the FIN must be a duplicate or out
2282                          * of sequence; drop it.
2283                          */
2284                         thflags &= ~TH_FIN;
2285
2286                         /*
2287                          * Send an ACK to resynchronize and drop any data.
2288                          * But keep on processing for RST or ACK.
2289                          */
2290                         tp->t_flags |= TF_ACKNOW;
2291                         todrop = tlen;
2292                         TCPSTAT_INC(tcps_rcvduppack);
2293                         TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2294                 } else {
2295                         TCPSTAT_INC(tcps_rcvpartduppack);
2296                         TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2297                 }
2298                 /*
2299                  * DSACK - add SACK block for dropped range
2300                  */
2301                 if (tp->t_flags & TF_SACK_PERMIT) {
2302                         tcp_update_sack_list(tp, th->th_seq,
2303                             th->th_seq + todrop);
2304                         /*
2305                          * ACK now, as the next in-sequence segment
2306                          * will clear the DSACK block again
2307                          */
2308                         tp->t_flags |= TF_ACKNOW;
2309                 }
2310                 drop_hdrlen += todrop;  /* drop from the top afterwards */
2311                 th->th_seq += todrop;
2312                 tlen -= todrop;
2313                 if (th->th_urp > todrop)
2314                         th->th_urp -= todrop;
2315                 else {
2316                         thflags &= ~TH_URG;
2317                         th->th_urp = 0;
2318                 }
2319         }
2320
2321         /*
2322          * If new data are received on a connection after the
2323          * user processes are gone, then RST the other end.
2324          */
2325         if ((so->so_state & SS_NOFDREF) &&
2326             tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2327                 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2328
2329                 if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
2330                         log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
2331                             "after socket was closed, "
2332                             "sending RST and removing tcpcb\n",
2333                             s, __func__, tcpstates[tp->t_state], tlen);
2334                         free(s, M_TCPLOG);
2335                 }
2336                 tp = tcp_close(tp);
2337                 TCPSTAT_INC(tcps_rcvafterclose);
2338                 rstreason = BANDLIM_UNLIMITED;
2339                 goto dropwithreset;
2340         }
2341
2342         /*
2343          * If segment ends after window, drop trailing data
2344          * (and PUSH and FIN); if nothing left, just ACK.
2345          */
2346         todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2347         if (todrop > 0) {
2348                 TCPSTAT_INC(tcps_rcvpackafterwin);
2349                 if (todrop >= tlen) {
2350                         TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2351                         /*
2352                          * If window is closed can only take segments at
2353                          * window edge, and have to drop data and PUSH from
2354                          * incoming segments.  Continue processing, but
2355                          * remember to ack.  Otherwise, drop segment
2356                          * and ack.
2357                          */
2358                         if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2359                                 tp->t_flags |= TF_ACKNOW;
2360                                 TCPSTAT_INC(tcps_rcvwinprobe);
2361                         } else
2362                                 goto dropafterack;
2363                 } else
2364                         TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2365                 m_adj(m, -todrop);
2366                 tlen -= todrop;
2367                 thflags &= ~(TH_PUSH|TH_FIN);
2368         }
2369
2370         /*
2371          * If last ACK falls within this segment's sequence numbers,
2372          * record its timestamp.
2373          * NOTE: 
2374          * 1) That the test incorporates suggestions from the latest
2375          *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2376          * 2) That updating only on newer timestamps interferes with
2377          *    our earlier PAWS tests, so this check should be solely
2378          *    predicated on the sequence space of this segment.
2379          * 3) That we modify the segment boundary check to be 
2380          *        Last.ACK.Sent <= SEG.SEQ + SEG.Len  
2381          *    instead of RFC1323's
2382          *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2383          *    This modified check allows us to overcome RFC1323's
2384          *    limitations as described in Stevens TCP/IP Illustrated
2385          *    Vol. 2 p.869. In such cases, we can still calculate the
2386          *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2387          */
2388         if ((to.to_flags & TOF_TS) != 0 &&
2389             SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2390             SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2391                 ((thflags & (TH_SYN|TH_FIN)) != 0))) {
2392                 tp->ts_recent_age = tcp_ts_getticks();
2393                 tp->ts_recent = to.to_tsval;
2394         }
2395
2396         /*
2397          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2398          * flag is on (half-synchronized state), then queue data for
2399          * later processing; else drop segment and return.
2400          */
2401         if ((thflags & TH_ACK) == 0) {
2402                 if (tp->t_state == TCPS_SYN_RECEIVED ||
2403                     (tp->t_flags & TF_NEEDSYN)) {
2404                         if (tp->t_state == TCPS_SYN_RECEIVED &&
2405                             IS_FASTOPEN(tp->t_flags)) {
2406                                 tp->snd_wnd = tiwin;
2407                                 cc_conn_init(tp);
2408                         }
2409                         goto step6;
2410                 } else if (tp->t_flags & TF_ACKNOW)
2411                         goto dropafterack;
2412                 else
2413                         goto drop;
2414         }
2415
2416         /*
2417          * Ack processing.
2418          */
2419         switch (tp->t_state) {
2420
2421         /*
2422          * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2423          * ESTABLISHED state and continue processing.
2424          * The ACK was checked above.
2425          */
2426         case TCPS_SYN_RECEIVED:
2427
2428                 TCPSTAT_INC(tcps_connects);
2429                 soisconnected(so);
2430                 /* Do window scaling? */
2431                 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2432                         (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2433                         tp->rcv_scale = tp->request_r_scale;
2434                 }
2435                 tp->snd_wnd = tiwin;
2436                 /*
2437                  * Make transitions:
2438                  *      SYN-RECEIVED  -> ESTABLISHED
2439                  *      SYN-RECEIVED* -> FIN-WAIT-1
2440                  */
2441                 tp->t_starttime = ticks;
2442                 if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
2443                         tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2444                         tp->t_tfo_pending = NULL;
2445                 }
2446                 if (tp->t_flags & TF_NEEDFIN) {
2447                         tcp_state_change(tp, TCPS_FIN_WAIT_1);
2448                         tp->t_flags &= ~TF_NEEDFIN;
2449                 } else {
2450                         tcp_state_change(tp, TCPS_ESTABLISHED);
2451                         TCP_PROBE5(accept__established, NULL, tp,
2452                             m, tp, th);
2453                         /*
2454                          * TFO connections call cc_conn_init() during SYN
2455                          * processing.  Calling it again here for such
2456                          * connections is not harmless as it would undo the
2457                          * snd_cwnd reduction that occurs when a TFO SYN|ACK
2458                          * is retransmitted.
2459                          */
2460                         if (!IS_FASTOPEN(tp->t_flags))
2461                                 cc_conn_init(tp);
2462                         tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
2463                 }
2464                 /*
2465                  * Account for the ACK of our SYN prior to
2466                  * regular ACK processing below, except for
2467                  * simultaneous SYN, which is handled later.
2468                  */
2469                 if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
2470                         incforsyn = 1;
2471                 /*
2472                  * If segment contains data or ACK, will call tcp_reass()
2473                  * later; if not, do so now to pass queued data to user.
2474                  */
2475                 if (tlen == 0 && (thflags & TH_FIN) == 0)
2476                         (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
2477                             (struct mbuf *)0);
2478                 tp->snd_wl1 = th->th_seq - 1;
2479                 /* FALLTHROUGH */
2480
2481         /*
2482          * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2483          * ACKs.  If the ack is in the range
2484          *      tp->snd_una < th->th_ack <= tp->snd_max
2485          * then advance tp->snd_una to th->th_ack and drop
2486          * data from the retransmission queue.  If this ACK reflects
2487          * more up to date window information we update our window information.
2488          */
2489         case TCPS_ESTABLISHED:
2490         case TCPS_FIN_WAIT_1:
2491         case TCPS_FIN_WAIT_2:
2492         case TCPS_CLOSE_WAIT:
2493         case TCPS_CLOSING:
2494         case TCPS_LAST_ACK:
2495                 if (SEQ_GT(th->th_ack, tp->snd_max)) {
2496                         TCPSTAT_INC(tcps_rcvacktoomuch);
2497                         goto dropafterack;
2498                 }
2499                 if ((tp->t_flags & TF_SACK_PERMIT) &&
2500                     ((to.to_flags & TOF_SACK) ||
2501                      !TAILQ_EMPTY(&tp->snd_holes)))
2502                         sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
2503                 else
2504                         /*
2505                          * Reset the value so that previous (valid) value
2506                          * from the last ack with SACK doesn't get used.
2507                          */
2508                         tp->sackhint.sacked_bytes = 0;
2509
2510 #ifdef TCP_HHOOK
2511                 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
2512                 hhook_run_tcp_est_in(tp, th, &to);
2513 #endif
2514
2515                 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
2516                         u_int maxseg;
2517
2518                         maxseg = tcp_maxseg(tp);
2519                         if (tlen == 0 &&
2520                             (tiwin == tp->snd_wnd ||
2521                             (tp->t_flags & TF_SACK_PERMIT))) {
2522                                 /*
2523                                  * If this is the first time we've seen a
2524                                  * FIN from the remote, this is not a
2525                                  * duplicate and it needs to be processed
2526                                  * normally.  This happens during a
2527                                  * simultaneous close.
2528                                  */
2529                                 if ((thflags & TH_FIN) &&
2530                                     (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
2531                                         tp->t_dupacks = 0;
2532                                         break;
2533                                 }
2534                                 TCPSTAT_INC(tcps_rcvdupack);
2535                                 /*
2536                                  * If we have outstanding data (other than
2537                                  * a window probe), this is a completely
2538                                  * duplicate ack (ie, window info didn't
2539                                  * change and FIN isn't set),
2540                                  * the ack is the biggest we've
2541                                  * seen and we've seen exactly our rexmt
2542                                  * threshold of them, assume a packet
2543                                  * has been dropped and retransmit it.
2544                                  * Kludge snd_nxt & the congestion
2545                                  * window so we send only this one
2546                                  * packet.
2547                                  *
2548                                  * We know we're losing at the current
2549                                  * window size so do congestion avoidance
2550                                  * (set ssthresh to half the current window
2551                                  * and pull our congestion window back to
2552                                  * the new ssthresh).
2553                                  *
2554                                  * Dup acks mean that packets have left the
2555                                  * network (they're now cached at the receiver)
2556                                  * so bump cwnd by the amount in the receiver
2557                                  * to keep a constant cwnd packets in the
2558                                  * network.
2559                                  *
2560                                  * When using TCP ECN, notify the peer that
2561                                  * we reduced the cwnd.
2562                                  */
2563                                 /*
2564                                  * Following 2 kinds of acks should not affect
2565                                  * dupack counting:
2566                                  * 1) Old acks
2567                                  * 2) Acks with SACK but without any new SACK
2568                                  * information in them. These could result from
2569                                  * any anomaly in the network like a switch
2570                                  * duplicating packets or a possible DoS attack.
2571                                  */
2572                                 if (th->th_ack != tp->snd_una ||
2573                                     ((tp->t_flags & TF_SACK_PERMIT) &&
2574                                     !sack_changed))
2575                                         break;
2576                                 else if (!tcp_timer_active(tp, TT_REXMT))
2577                                         tp->t_dupacks = 0;
2578                                 else if (++tp->t_dupacks > tcprexmtthresh ||
2579                                      IN_FASTRECOVERY(tp->t_flags)) {
2580                                         cc_ack_received(tp, th, nsegs,
2581                                             CC_DUPACK);
2582                                         if ((tp->t_flags & TF_SACK_PERMIT) &&
2583                                             IN_FASTRECOVERY(tp->t_flags)) {
2584                                                 int awnd;
2585                                                 
2586                                                 /*
2587                                                  * Compute the amount of data in flight first.
2588                                                  * We can inject new data into the pipe iff 
2589                                                  * we have less than 1/2 the original window's
2590                                                  * worth of data in flight.
2591                                                  */
2592                                                 if (V_tcp_do_rfc6675_pipe)
2593                                                         awnd = tcp_compute_pipe(tp);
2594                                                 else
2595                                                         awnd = (tp->snd_nxt - tp->snd_fack) +
2596                                                                 tp->sackhint.sack_bytes_rexmit;
2597
2598                                                 if (awnd < tp->snd_ssthresh) {
2599                                                         tp->snd_cwnd += maxseg;
2600                                                         if (tp->snd_cwnd > tp->snd_ssthresh)
2601                                                                 tp->snd_cwnd = tp->snd_ssthresh;
2602                                                 }
2603                                         } else
2604                                                 tp->snd_cwnd += maxseg;
2605                                         (void) tp->t_fb->tfb_tcp_output(tp);
2606                                         goto drop;
2607                                 } else if (tp->t_dupacks == tcprexmtthresh) {
2608                                         tcp_seq onxt = tp->snd_nxt;
2609
2610                                         /*
2611                                          * If we're doing sack, check to
2612                                          * see if we're already in sack
2613                                          * recovery. If we're not doing sack,
2614                                          * check to see if we're in newreno
2615                                          * recovery.
2616                                          */
2617                                         if (tp->t_flags & TF_SACK_PERMIT) {
2618                                                 if (IN_FASTRECOVERY(tp->t_flags)) {
2619                                                         tp->t_dupacks = 0;
2620                                                         break;
2621                                                 }
2622                                         } else {
2623                                                 if (SEQ_LEQ(th->th_ack,
2624                                                     tp->snd_recover)) {
2625                                                         tp->t_dupacks = 0;
2626                                                         break;
2627                                                 }
2628                                         }
2629                                         /* Congestion signal before ack. */
2630                                         cc_cong_signal(tp, th, CC_NDUPACK);
2631                                         cc_ack_received(tp, th, nsegs,
2632                                             CC_DUPACK);
2633                                         tcp_timer_activate(tp, TT_REXMT, 0);
2634                                         tp->t_rtttime = 0;
2635                                         if (tp->t_flags & TF_SACK_PERMIT) {
2636                                                 TCPSTAT_INC(
2637                                                     tcps_sack_recovery_episode);
2638                                                 tp->sack_newdata = tp->snd_nxt;
2639                                                 tp->snd_cwnd = maxseg;
2640                                                 (void) tp->t_fb->tfb_tcp_output(tp);
2641                                                 goto drop;
2642                                         }
2643                                         tp->snd_nxt = th->th_ack;
2644                                         tp->snd_cwnd = maxseg;
2645                                         (void) tp->t_fb->tfb_tcp_output(tp);
2646                                         KASSERT(tp->snd_limited <= 2,
2647                                             ("%s: tp->snd_limited too big",
2648                                             __func__));
2649                                         tp->snd_cwnd = tp->snd_ssthresh +
2650                                              maxseg *
2651                                              (tp->t_dupacks - tp->snd_limited);
2652                                         if (SEQ_GT(onxt, tp->snd_nxt))
2653                                                 tp->snd_nxt = onxt;
2654                                         goto drop;
2655                                 } else if (V_tcp_do_rfc3042) {
2656                                         /*
2657                                          * Process first and second duplicate
2658                                          * ACKs. Each indicates a segment
2659                                          * leaving the network, creating room
2660                                          * for more. Make sure we can send a
2661                                          * packet on reception of each duplicate
2662                                          * ACK by increasing snd_cwnd by one
2663                                          * segment. Restore the original
2664                                          * snd_cwnd after packet transmission.
2665                                          */
2666                                         cc_ack_received(tp, th, nsegs,
2667                                             CC_DUPACK);
2668                                         uint32_t oldcwnd = tp->snd_cwnd;
2669                                         tcp_seq oldsndmax = tp->snd_max;
2670                                         u_int sent;
2671                                         int avail;
2672
2673                                         KASSERT(tp->t_dupacks == 1 ||
2674                                             tp->t_dupacks == 2,
2675                                             ("%s: dupacks not 1 or 2",
2676                                             __func__));
2677                                         if (tp->t_dupacks == 1)
2678                                                 tp->snd_limited = 0;
2679                                         tp->snd_cwnd =
2680                                             (tp->snd_nxt - tp->snd_una) +
2681                                             (tp->t_dupacks - tp->snd_limited) *
2682                                             maxseg;
2683                                         /*
2684                                          * Only call tcp_output when there
2685                                          * is new data available to be sent.
2686                                          * Otherwise we would send pure ACKs.
2687                                          */
2688                                         SOCKBUF_LOCK(&so->so_snd);
2689                                         avail = sbavail(&so->so_snd) -
2690                                             (tp->snd_nxt - tp->snd_una);
2691                                         SOCKBUF_UNLOCK(&so->so_snd);
2692                                         if (avail > 0)
2693                                                 (void) tp->t_fb->tfb_tcp_output(tp);
2694                                         sent = tp->snd_max - oldsndmax;
2695                                         if (sent > maxseg) {
2696                                                 KASSERT((tp->t_dupacks == 2 &&
2697                                                     tp->snd_limited == 0) ||
2698                                                    (sent == maxseg + 1 &&
2699                                                     tp->t_flags & TF_SENTFIN),
2700                                                     ("%s: sent too much",
2701                                                     __func__));
2702                                                 tp->snd_limited = 2;
2703                                         } else if (sent > 0)
2704                                                 ++tp->snd_limited;
2705                                         tp->snd_cwnd = oldcwnd;
2706                                         goto drop;
2707                                 }
2708                         }
2709                         break;
2710                 } else {
2711                         /*
2712                          * This ack is advancing the left edge, reset the
2713                          * counter.
2714                          */
2715                         tp->t_dupacks = 0;
2716                         /*
2717                          * If this ack also has new SACK info, increment the
2718                          * counter as per rfc6675. The variable
2719                          * sack_changed tracks all changes to the SACK
2720                          * scoreboard, including when partial ACKs without
2721                          * SACK options are received, and clear the scoreboard
2722                          * from the left side. Such partial ACKs should not be
2723                          * counted as dupacks here.
2724                          */
2725                         if ((tp->t_flags & TF_SACK_PERMIT) &&
2726                             (to.to_flags & TOF_SACK) &&
2727                             sack_changed)
2728                                 tp->t_dupacks++;
2729                 }
2730
2731                 KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2732                     ("%s: th_ack <= snd_una", __func__));
2733
2734                 /*
2735                  * If the congestion window was inflated to account
2736                  * for the other side's cached packets, retract it.
2737                  */
2738                 if (IN_FASTRECOVERY(tp->t_flags)) {
2739                         if (SEQ_LT(th->th_ack, tp->snd_recover)) {
2740                                 if (tp->t_flags & TF_SACK_PERMIT)
2741                                         tcp_sack_partialack(tp, th);
2742                                 else
2743                                         tcp_newreno_partial_ack(tp, th);
2744                         } else
2745                                 cc_post_recovery(tp, th);
2746                 }
2747                 /*
2748                  * If we reach this point, ACK is not a duplicate,
2749                  *     i.e., it ACKs something we sent.
2750                  */
2751                 if (tp->t_flags & TF_NEEDSYN) {
2752                         /*
2753                          * T/TCP: Connection was half-synchronized, and our
2754                          * SYN has been ACK'd (so connection is now fully
2755                          * synchronized).  Go to non-starred state,
2756                          * increment snd_una for ACK of SYN, and check if
2757                          * we can do window scaling.
2758                          */
2759                         tp->t_flags &= ~TF_NEEDSYN;
2760                         tp->snd_una++;
2761                         /* Do window scaling? */
2762                         if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2763                                 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2764                                 tp->rcv_scale = tp->request_r_scale;
2765                                 /* Send window already scaled. */
2766                         }
2767                 }
2768
2769 process_ACK:
2770                 INP_WLOCK_ASSERT(tp->t_inpcb);
2771
2772                 /*
2773                  * Adjust for the SYN bit in sequence space,
2774                  * but don't account for it in cwnd calculations.
2775                  * This is for the SYN_RECEIVED, non-simultaneous
2776                  * SYN case. SYN_SENT and simultaneous SYN are
2777                  * treated elsewhere.
2778                  */
2779                 if (incforsyn)
2780                         tp->snd_una++;
2781                 acked = BYTES_THIS_ACK(tp, th);
2782                 KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2783                     "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2784                     tp->snd_una, th->th_ack, tp, m));
2785                 TCPSTAT_ADD(tcps_rcvackpack, nsegs);
2786                 TCPSTAT_ADD(tcps_rcvackbyte, acked);
2787
2788                 /*
2789                  * If we just performed our first retransmit, and the ACK
2790                  * arrives within our recovery window, then it was a mistake
2791                  * to do the retransmit in the first place.  Recover our
2792                  * original cwnd and ssthresh, and proceed to transmit where
2793                  * we left off.
2794                  */
2795                 if (tp->t_rxtshift == 1 &&
2796                     tp->t_flags & TF_PREVVALID &&
2797                     tp->t_badrxtwin &&
2798                     SEQ_LT(to.to_tsecr, tp->t_badrxtwin))
2799                         cc_cong_signal(tp, th, CC_RTO_ERR);
2800
2801                 /*
2802                  * If we have a timestamp reply, update smoothed
2803                  * round trip time.  If no timestamp is present but
2804                  * transmit timer is running and timed sequence
2805                  * number was acked, update smoothed round trip time.
2806                  * Since we now have an rtt measurement, cancel the
2807                  * timer backoff (cf., Phil Karn's retransmit alg.).
2808                  * Recompute the initial retransmit timer.
2809                  *
2810                  * Some boxes send broken timestamp replies
2811                  * during the SYN+ACK phase, ignore
2812                  * timestamps of 0 or we could calculate a
2813                  * huge RTT and blow up the retransmit timer.
2814                  */
2815                 if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
2816                         uint32_t t;
2817
2818                         t = tcp_ts_getticks() - to.to_tsecr;
2819                         if (!tp->t_rttlow || tp->t_rttlow > t)
2820                                 tp->t_rttlow = t;
2821                         tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2822                 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2823                         if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2824                                 tp->t_rttlow = ticks - tp->t_rtttime;
2825                         tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2826                 }
2827
2828                 /*
2829                  * If all outstanding data is acked, stop retransmit
2830                  * timer and remember to restart (more output or persist).
2831                  * If there is more data to be acked, restart retransmit
2832                  * timer, using current (possibly backed-off) value.
2833                  */
2834                 if (th->th_ack == tp->snd_max) {
2835                         tcp_timer_activate(tp, TT_REXMT, 0);
2836                         needoutput = 1;
2837                 } else if (!tcp_timer_active(tp, TT_PERSIST))
2838                         tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2839
2840                 /*
2841                  * If no data (only SYN) was ACK'd,
2842                  *    skip rest of ACK processing.
2843                  */
2844                 if (acked == 0)
2845                         goto step6;
2846
2847                 /*
2848                  * Let the congestion control algorithm update congestion
2849                  * control related information. This typically means increasing
2850                  * the congestion window.
2851                  */
2852                 cc_ack_received(tp, th, nsegs, CC_ACK);
2853
2854                 SOCKBUF_LOCK(&so->so_snd);
2855                 if (acked > sbavail(&so->so_snd)) {
2856                         if (tp->snd_wnd >= sbavail(&so->so_snd))
2857                                 tp->snd_wnd -= sbavail(&so->so_snd);
2858                         else
2859                                 tp->snd_wnd = 0;
2860                         mfree = sbcut_locked(&so->so_snd,
2861                             (int)sbavail(&so->so_snd));
2862                         ourfinisacked = 1;
2863                 } else {
2864                         mfree = sbcut_locked(&so->so_snd, acked);
2865                         if (tp->snd_wnd >= (uint32_t) acked)
2866                                 tp->snd_wnd -= acked;
2867                         else
2868                                 tp->snd_wnd = 0;
2869                         ourfinisacked = 0;
2870                 }
2871                 /* NB: sowwakeup_locked() does an implicit unlock. */
2872                 sowwakeup_locked(so);
2873                 m_freem(mfree);
2874                 /* Detect una wraparound. */
2875                 if (!IN_RECOVERY(tp->t_flags) &&
2876                     SEQ_GT(tp->snd_una, tp->snd_recover) &&
2877                     SEQ_LEQ(th->th_ack, tp->snd_recover))
2878                         tp->snd_recover = th->th_ack - 1;
2879                 /* XXXLAS: Can this be moved up into cc_post_recovery? */
2880                 if (IN_RECOVERY(tp->t_flags) &&
2881                     SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2882                         EXIT_RECOVERY(tp->t_flags);
2883                 }
2884                 tp->snd_una = th->th_ack;
2885                 if (tp->t_flags & TF_SACK_PERMIT) {
2886                         if (SEQ_GT(tp->snd_una, tp->snd_recover))
2887                                 tp->snd_recover = tp->snd_una;
2888                 }
2889                 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2890                         tp->snd_nxt = tp->snd_una;
2891
2892                 switch (tp->t_state) {
2893
2894                 /*
2895                  * In FIN_WAIT_1 STATE in addition to the processing
2896                  * for the ESTABLISHED state if our FIN is now acknowledged
2897                  * then enter FIN_WAIT_2.
2898                  */
2899                 case TCPS_FIN_WAIT_1:
2900                         if (ourfinisacked) {
2901                                 /*
2902                                  * If we can't receive any more
2903                                  * data, then closing user can proceed.
2904                                  * Starting the timer is contrary to the
2905                                  * specification, but if we don't get a FIN
2906                                  * we'll hang forever.
2907                                  *
2908                                  * XXXjl:
2909                                  * we should release the tp also, and use a
2910                                  * compressed state.
2911                                  */
2912                                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2913                                         soisdisconnected(so);
2914                                         tcp_timer_activate(tp, TT_2MSL,
2915                                             (tcp_fast_finwait2_recycle ?
2916                                             tcp_finwait2_timeout :
2917                                             TP_MAXIDLE(tp)));
2918                                 }
2919                                 tcp_state_change(tp, TCPS_FIN_WAIT_2);
2920                         }
2921                         break;
2922
2923                 /*
2924                  * In CLOSING STATE in addition to the processing for
2925                  * the ESTABLISHED state if the ACK acknowledges our FIN
2926                  * then enter the TIME-WAIT state, otherwise ignore
2927                  * the segment.
2928                  */
2929                 case TCPS_CLOSING:
2930                         if (ourfinisacked) {
2931                                 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2932                                 tcp_twstart(tp);
2933                                 m_freem(m);
2934                                 return;
2935                         }
2936                         break;
2937
2938                 /*
2939                  * In LAST_ACK, we may still be waiting for data to drain
2940                  * and/or to be acked, as well as for the ack of our FIN.
2941                  * If our FIN is now acknowledged, delete the TCB,
2942                  * enter the closed state and return.
2943                  */
2944                 case TCPS_LAST_ACK:
2945                         if (ourfinisacked) {
2946                                 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2947                                 tp = tcp_close(tp);
2948                                 goto drop;
2949                         }
2950                         break;
2951                 }
2952         }
2953
2954 step6:
2955         INP_WLOCK_ASSERT(tp->t_inpcb);
2956
2957         /*
2958          * Update window information.
2959          * Don't look at window if no ACK: TAC's send garbage on first SYN.
2960          */
2961         if ((thflags & TH_ACK) &&
2962             (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2963             (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2964              (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2965                 /* keep track of pure window updates */
2966                 if (tlen == 0 &&
2967                     tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2968                         TCPSTAT_INC(tcps_rcvwinupd);
2969                 tp->snd_wnd = tiwin;
2970                 tp->snd_wl1 = th->th_seq;
2971                 tp->snd_wl2 = th->th_ack;
2972                 if (tp->snd_wnd > tp->max_sndwnd)
2973                         tp->max_sndwnd = tp->snd_wnd;
2974                 needoutput = 1;
2975         }
2976
2977         /*
2978          * Process segments with URG.
2979          */
2980         if ((thflags & TH_URG) && th->th_urp &&
2981             TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2982                 /*
2983                  * This is a kludge, but if we receive and accept
2984                  * random urgent pointers, we'll crash in
2985                  * soreceive.  It's hard to imagine someone
2986                  * actually wanting to send this much urgent data.
2987                  */
2988                 SOCKBUF_LOCK(&so->so_rcv);
2989                 if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
2990                         th->th_urp = 0;                 /* XXX */
2991                         thflags &= ~TH_URG;             /* XXX */
2992                         SOCKBUF_UNLOCK(&so->so_rcv);    /* XXX */
2993                         goto dodata;                    /* XXX */
2994                 }
2995                 /*
2996                  * If this segment advances the known urgent pointer,
2997                  * then mark the data stream.  This should not happen
2998                  * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2999                  * a FIN has been received from the remote side.
3000                  * In these states we ignore the URG.
3001                  *
3002                  * According to RFC961 (Assigned Protocols),
3003                  * the urgent pointer points to the last octet
3004                  * of urgent data.  We continue, however,
3005                  * to consider it to indicate the first octet
3006                  * of data past the urgent section as the original
3007                  * spec states (in one of two places).
3008                  */
3009                 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
3010                         tp->rcv_up = th->th_seq + th->th_urp;
3011                         so->so_oobmark = sbavail(&so->so_rcv) +
3012                             (tp->rcv_up - tp->rcv_nxt) - 1;
3013                         if (so->so_oobmark == 0)
3014                                 so->so_rcv.sb_state |= SBS_RCVATMARK;
3015                         sohasoutofband(so);
3016                         tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
3017                 }
3018                 SOCKBUF_UNLOCK(&so->so_rcv);
3019                 /*
3020                  * Remove out of band data so doesn't get presented to user.
3021                  * This can happen independent of advancing the URG pointer,
3022                  * but if two URG's are pending at once, some out-of-band
3023                  * data may creep in... ick.
3024                  */
3025                 if (th->th_urp <= (uint32_t)tlen &&
3026                     !(so->so_options & SO_OOBINLINE)) {
3027                         /* hdr drop is delayed */
3028                         tcp_pulloutofband(so, th, m, drop_hdrlen);
3029                 }
3030         } else {
3031                 /*
3032                  * If no out of band data is expected,
3033                  * pull receive urgent pointer along
3034                  * with the receive window.
3035                  */
3036                 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3037                         tp->rcv_up = tp->rcv_nxt;
3038         }
3039 dodata:                                                 /* XXX */
3040         INP_WLOCK_ASSERT(tp->t_inpcb);
3041
3042         /*
3043          * Process the segment text, merging it into the TCP sequencing queue,
3044          * and arranging for acknowledgment of receipt if necessary.
3045          * This process logically involves adjusting tp->rcv_wnd as data
3046          * is presented to the user (this happens in tcp_usrreq.c,
3047          * case PRU_RCVD).  If a FIN has already been received on this
3048          * connection then we just ignore the text.
3049          */
3050         tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
3051                    IS_FASTOPEN(tp->t_flags));
3052         if ((tlen || (thflags & TH_FIN) || tfo_syn) &&
3053             TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3054                 tcp_seq save_start = th->th_seq;
3055                 tcp_seq save_rnxt  = tp->rcv_nxt;
3056                 int     save_tlen  = tlen;
3057                 m_adj(m, drop_hdrlen);  /* delayed header drop */
3058                 /*
3059                  * Insert segment which includes th into TCP reassembly queue
3060                  * with control block tp.  Set thflags to whether reassembly now
3061                  * includes a segment with FIN.  This handles the common case
3062                  * inline (segment is the next to be received on an established
3063                  * connection, and the queue is empty), avoiding linkage into
3064                  * and removal from the queue and repetition of various
3065                  * conversions.
3066                  * Set DELACK for segments received in order, but ack
3067                  * immediately when segments are out of order (so
3068                  * fast retransmit can work).
3069                  */
3070                 if (th->th_seq == tp->rcv_nxt &&
3071                     SEGQ_EMPTY(tp) &&
3072                     (TCPS_HAVEESTABLISHED(tp->t_state) ||
3073                      tfo_syn)) {
3074                         if (DELAY_ACK(tp, tlen) || tfo_syn)
3075                                 tp->t_flags |= TF_DELACK;
3076                         else
3077                                 tp->t_flags |= TF_ACKNOW;
3078                         tp->rcv_nxt += tlen;
3079                         thflags = th->th_flags & TH_FIN;
3080                         TCPSTAT_INC(tcps_rcvpack);
3081                         TCPSTAT_ADD(tcps_rcvbyte, tlen);
3082                         SOCKBUF_LOCK(&so->so_rcv);
3083                         if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3084                                 m_freem(m);
3085                         else
3086                                 sbappendstream_locked(&so->so_rcv, m, 0);
3087                         /* NB: sorwakeup_locked() does an implicit unlock. */
3088                         sorwakeup_locked(so);
3089                 } else {
3090                         /*
3091                          * XXX: Due to the header drop above "th" is
3092                          * theoretically invalid by now.  Fortunately
3093                          * m_adj() doesn't actually frees any mbufs
3094                          * when trimming from the head.
3095                          */
3096                         tcp_seq temp = save_start;
3097                         thflags = tcp_reass(tp, th, &temp, &tlen, m);
3098                         tp->t_flags |= TF_ACKNOW;
3099                 }
3100                 if ((tp->t_flags & TF_SACK_PERMIT) && (save_tlen > 0)) {
3101                         if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
3102                                 /*
3103                                  * DSACK actually handled in the fastpath
3104                                  * above.
3105                                  */
3106                                 tcp_update_sack_list(tp, save_start,
3107                                     save_start + save_tlen);
3108                         } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
3109                                 if ((tp->rcv_numsacks >= 1) &&
3110                                     (tp->sackblks[0].end == save_start)) {
3111                                         /*
3112                                          * Partial overlap, recorded at todrop
3113                                          * above.
3114                                          */
3115                                         tcp_update_sack_list(tp,
3116                                             tp->sackblks[0].start,
3117                                             tp->sackblks[0].end);
3118                                 } else {
3119                                         tcp_update_dsack_list(tp, save_start,
3120                                             save_start + save_tlen);
3121                                 }
3122                         } else if (tlen >= save_tlen) {
3123                                 /* Update of sackblks. */
3124                                 tcp_update_dsack_list(tp, save_start,
3125                                     save_start + save_tlen);
3126                         } else if (tlen > 0) {
3127                                 tcp_update_dsack_list(tp, save_start,
3128                                     save_start + tlen);
3129                         }
3130                 }
3131 #if 0
3132                 /*
3133                  * Note the amount of data that peer has sent into
3134                  * our window, in order to estimate the sender's
3135                  * buffer size.
3136                  * XXX: Unused.
3137                  */
3138                 if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3139                         len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3140                 else
3141                         len = so->so_rcv.sb_hiwat;
3142 #endif
3143         } else {
3144                 m_freem(m);
3145                 thflags &= ~TH_FIN;
3146         }
3147
3148         /*
3149          * If FIN is received ACK the FIN and let the user know
3150          * that the connection is closing.
3151          */
3152         if (thflags & TH_FIN) {
3153                 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3154                         socantrcvmore(so);
3155                         /*
3156                          * If connection is half-synchronized
3157                          * (ie NEEDSYN flag on) then delay ACK,
3158                          * so it may be piggybacked when SYN is sent.
3159                          * Otherwise, since we received a FIN then no
3160                          * more input can be expected, send ACK now.
3161                          */
3162                         if (tp->t_flags & TF_NEEDSYN)
3163                                 tp->t_flags |= TF_DELACK;
3164                         else
3165                                 tp->t_flags |= TF_ACKNOW;
3166                         tp->rcv_nxt++;
3167                 }
3168                 switch (tp->t_state) {
3169
3170                 /*
3171                  * In SYN_RECEIVED and ESTABLISHED STATES
3172                  * enter the CLOSE_WAIT state.
3173                  */
3174                 case TCPS_SYN_RECEIVED:
3175                         tp->t_starttime = ticks;
3176                         /* FALLTHROUGH */
3177                 case TCPS_ESTABLISHED:
3178                         tcp_state_change(tp, TCPS_CLOSE_WAIT);
3179                         break;
3180
3181                 /*
3182                  * If still in FIN_WAIT_1 STATE FIN has not been acked so
3183                  * enter the CLOSING state.
3184                  */
3185                 case TCPS_FIN_WAIT_1:
3186                         tcp_state_change(tp, TCPS_CLOSING);
3187                         break;
3188
3189                 /*
3190                  * In FIN_WAIT_2 state enter the TIME_WAIT state,
3191                  * starting the time-wait timer, turning off the other
3192                  * standard timers.
3193                  */
3194                 case TCPS_FIN_WAIT_2:
3195                         INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
3196
3197                         tcp_twstart(tp);
3198                         return;
3199                 }
3200         }
3201 #ifdef TCPDEBUG
3202         if (so->so_options & SO_DEBUG)
3203                 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3204                           &tcp_savetcp, 0);
3205 #endif
3206         TCP_PROBE3(debug__input, tp, th, m);
3207
3208         /*
3209          * Return any desired output.
3210          */
3211         if (needoutput || (tp->t_flags & TF_ACKNOW))
3212                 (void) tp->t_fb->tfb_tcp_output(tp);
3213
3214 check_delack:
3215         INP_WLOCK_ASSERT(tp->t_inpcb);
3216
3217         if (tp->t_flags & TF_DELACK) {
3218                 tp->t_flags &= ~TF_DELACK;
3219                 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
3220         }
3221         INP_WUNLOCK(tp->t_inpcb);
3222         return;
3223
3224 dropafterack:
3225         /*
3226          * Generate an ACK dropping incoming segment if it occupies
3227          * sequence space, where the ACK reflects our state.
3228          *
3229          * We can now skip the test for the RST flag since all
3230          * paths to this code happen after packets containing
3231          * RST have been dropped.
3232          *
3233          * In the SYN-RECEIVED state, don't send an ACK unless the
3234          * segment we received passes the SYN-RECEIVED ACK test.
3235          * If it fails send a RST.  This breaks the loop in the
3236          * "LAND" DoS attack, and also prevents an ACK storm
3237          * between two listening ports that have been sent forged
3238          * SYN segments, each with the source address of the other.
3239          */
3240         if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3241             (SEQ_GT(tp->snd_una, th->th_ack) ||
3242              SEQ_GT(th->th_ack, tp->snd_max)) ) {
3243                 rstreason = BANDLIM_RST_OPENPORT;
3244                 goto dropwithreset;
3245         }
3246 #ifdef TCPDEBUG
3247         if (so->so_options & SO_DEBUG)
3248                 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3249                           &tcp_savetcp, 0);
3250 #endif
3251         TCP_PROBE3(debug__input, tp, th, m);
3252         tp->t_flags |= TF_ACKNOW;
3253         (void) tp->t_fb->tfb_tcp_output(tp);
3254         INP_WUNLOCK(tp->t_inpcb);
3255         m_freem(m);
3256         return;
3257
3258 dropwithreset:
3259         if (tp != NULL) {
3260                 tcp_dropwithreset(m, th, tp, tlen, rstreason);
3261                 INP_WUNLOCK(tp->t_inpcb);
3262         } else
3263                 tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3264         return;
3265
3266 drop:
3267         /*
3268          * Drop space held by incoming segment and return.
3269          */
3270 #ifdef TCPDEBUG
3271         if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3272                 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3273                           &tcp_savetcp, 0);
3274 #endif
3275         TCP_PROBE3(debug__input, tp, th, m);
3276         if (tp != NULL)
3277                 INP_WUNLOCK(tp->t_inpcb);
3278         m_freem(m);
3279 }
3280
3281 /*
3282  * Issue RST and make ACK acceptable to originator of segment.
3283  * The mbuf must still include the original packet header.
3284  * tp may be NULL.
3285  */
3286 void
3287 tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3288     int tlen, int rstreason)
3289 {
3290 #ifdef INET
3291         struct ip *ip;
3292 #endif
3293 #ifdef INET6
3294         struct ip6_hdr *ip6;
3295 #endif
3296
3297         if (tp != NULL) {
3298                 INP_WLOCK_ASSERT(tp->t_inpcb);
3299         }
3300
3301         /* Don't bother if destination was broadcast/multicast. */
3302         if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3303                 goto drop;
3304 #ifdef INET6
3305         if (mtod(m, struct ip *)->ip_v == 6) {
3306                 ip6 = mtod(m, struct ip6_hdr *);
3307                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3308                     IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3309                         goto drop;
3310                 /* IPv6 anycast check is done at tcp6_input() */
3311         }
3312 #endif
3313 #if defined(INET) && defined(INET6)
3314         else
3315 #endif
3316 #ifdef INET
3317         {
3318                 ip = mtod(m, struct ip *);
3319                 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3320                     IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3321                     ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3322                     in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3323                         goto drop;
3324         }
3325 #endif
3326
3327         /* Perform bandwidth limiting. */
3328         if (badport_bandlim(rstreason) < 0)
3329                 goto drop;
3330
3331         /* tcp_respond consumes the mbuf chain. */
3332         if (th->th_flags & TH_ACK) {
3333                 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3334                     th->th_ack, TH_RST);
3335         } else {
3336                 if (th->th_flags & TH_SYN)
3337                         tlen++;
3338                 if (th->th_flags & TH_FIN)
3339                         tlen++;
3340                 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3341                     (tcp_seq)0, TH_RST|TH_ACK);
3342         }
3343         return;
3344 drop:
3345         m_freem(m);
3346 }
3347
3348 /*
3349  * Parse TCP options and place in tcpopt.
3350  */
3351 void
3352 tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3353 {
3354         int opt, optlen;
3355
3356         to->to_flags = 0;
3357         for (; cnt > 0; cnt -= optlen, cp += optlen) {
3358                 opt = cp[0];
3359                 if (opt == TCPOPT_EOL)
3360                         break;
3361                 if (opt == TCPOPT_NOP)
3362                         optlen = 1;
3363                 else {
3364                         if (cnt < 2)
3365                                 break;
3366                         optlen = cp[1];
3367                         if (optlen < 2 || optlen > cnt)
3368                                 break;
3369                 }
3370                 switch (opt) {
3371                 case TCPOPT_MAXSEG:
3372                         if (optlen != TCPOLEN_MAXSEG)
3373                                 continue;
3374                         if (!(flags & TO_SYN))
3375                                 continue;
3376                         to->to_flags |= TOF_MSS;
3377                         bcopy((char *)cp + 2,
3378                             (char *)&to->to_mss, sizeof(to->to_mss));
3379                         to->to_mss = ntohs(to->to_mss);
3380                         break;
3381                 case TCPOPT_WINDOW:
3382                         if (optlen != TCPOLEN_WINDOW)
3383                                 continue;
3384                         if (!(flags & TO_SYN))
3385                                 continue;
3386                         to->to_flags |= TOF_SCALE;
3387                         to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3388                         break;
3389                 case TCPOPT_TIMESTAMP:
3390                         if (optlen != TCPOLEN_TIMESTAMP)
3391                                 continue;
3392                         to->to_flags |= TOF_TS;
3393                         bcopy((char *)cp + 2,
3394                             (char *)&to->to_tsval, sizeof(to->to_tsval));
3395                         to->to_tsval = ntohl(to->to_tsval);
3396                         bcopy((char *)cp + 6,
3397                             (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3398                         to->to_tsecr = ntohl(to->to_tsecr);
3399                         break;
3400                 case TCPOPT_SIGNATURE:
3401                         /*
3402                          * In order to reply to a host which has set the
3403                          * TCP_SIGNATURE option in its initial SYN, we have
3404                          * to record the fact that the option was observed
3405                          * here for the syncache code to perform the correct
3406                          * response.
3407                          */
3408                         if (optlen != TCPOLEN_SIGNATURE)
3409                                 continue;
3410                         to->to_flags |= TOF_SIGNATURE;
3411                         to->to_signature = cp + 2;
3412                         break;
3413                 case TCPOPT_SACK_PERMITTED:
3414                         if (optlen != TCPOLEN_SACK_PERMITTED)
3415                                 continue;
3416                         if (!(flags & TO_SYN))
3417                                 continue;
3418                         if (!V_tcp_do_sack)
3419                                 continue;
3420                         to->to_flags |= TOF_SACKPERM;
3421                         break;
3422                 case TCPOPT_SACK:
3423                         if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
3424                                 continue;
3425                         if (flags & TO_SYN)
3426                                 continue;
3427                         to->to_flags |= TOF_SACK;
3428                         to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
3429                         to->to_sacks = cp + 2;
3430                         TCPSTAT_INC(tcps_sack_rcv_blocks);
3431                         break;
3432                 case TCPOPT_FAST_OPEN:
3433                         /*
3434                          * Cookie length validation is performed by the
3435                          * server side cookie checking code or the client
3436                          * side cookie cache update code.
3437                          */
3438                         if (!(flags & TO_SYN))
3439                                 continue;
3440                         if (!V_tcp_fastopen_client_enable &&
3441                             !V_tcp_fastopen_server_enable)
3442                                 continue;
3443                         to->to_flags |= TOF_FASTOPEN;
3444                         to->to_tfo_len = optlen - 2;
3445                         to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3446                         break;
3447                 default:
3448                         continue;
3449                 }
3450         }
3451 }
3452
3453 /*
3454  * Pull out of band byte out of a segment so
3455  * it doesn't appear in the user's data queue.
3456  * It is still reflected in the segment length for
3457  * sequencing purposes.
3458  */
3459 void
3460 tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3461     int off)
3462 {
3463         int cnt = off + th->th_urp - 1;
3464
3465         while (cnt >= 0) {
3466                 if (m->m_len > cnt) {
3467                         char *cp = mtod(m, caddr_t) + cnt;
3468                         struct tcpcb *tp = sototcpcb(so);
3469
3470                         INP_WLOCK_ASSERT(tp->t_inpcb);
3471
3472                         tp->t_iobc = *cp;
3473                         tp->t_oobflags |= TCPOOB_HAVEDATA;
3474                         bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3475                         m->m_len--;
3476                         if (m->m_flags & M_PKTHDR)
3477                                 m->m_pkthdr.len--;
3478                         return;
3479                 }
3480                 cnt -= m->m_len;
3481                 m = m->m_next;
3482                 if (m == NULL)
3483                         break;
3484         }
3485         panic("tcp_pulloutofband");
3486 }
3487
3488 /*
3489  * Collect new round-trip time estimate
3490  * and update averages and current timeout.
3491  */
3492 void
3493 tcp_xmit_timer(struct tcpcb *tp, int rtt)
3494 {
3495         int delta;
3496
3497         INP_WLOCK_ASSERT(tp->t_inpcb);
3498
3499         TCPSTAT_INC(tcps_rttupdated);
3500         tp->t_rttupdated++;
3501         if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3502                 /*
3503                  * srtt is stored as fixed point with 5 bits after the
3504                  * binary point (i.e., scaled by 8).  The following magic
3505                  * is equivalent to the smoothing algorithm in rfc793 with
3506                  * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3507                  * point).  Adjust rtt to origin 0.
3508                  */
3509                 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3510                         - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3511
3512                 if ((tp->t_srtt += delta) <= 0)
3513                         tp->t_srtt = 1;
3514
3515                 /*
3516                  * We accumulate a smoothed rtt variance (actually, a
3517                  * smoothed mean difference), then set the retransmit
3518                  * timer to smoothed rtt + 4 times the smoothed variance.
3519                  * rttvar is stored as fixed point with 4 bits after the
3520                  * binary point (scaled by 16).  The following is
3521                  * equivalent to rfc793 smoothing with an alpha of .75
3522                  * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3523                  * rfc793's wired-in beta.
3524                  */
3525                 if (delta < 0)
3526                         delta = -delta;
3527                 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3528                 if ((tp->t_rttvar += delta) <= 0)
3529                         tp->t_rttvar = 1;
3530                 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
3531                     tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3532         } else {
3533                 /*
3534                  * No rtt measurement yet - use the unsmoothed rtt.
3535                  * Set the variance to half the rtt (so our first
3536                  * retransmit happens at 3*rtt).
3537                  */
3538                 tp->t_srtt = rtt << TCP_RTT_SHIFT;
3539                 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
3540                 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3541         }
3542         tp->t_rtttime = 0;
3543         tp->t_rxtshift = 0;
3544
3545         /*
3546          * the retransmit should happen at rtt + 4 * rttvar.
3547          * Because of the way we do the smoothing, srtt and rttvar
3548          * will each average +1/2 tick of bias.  When we compute
3549          * the retransmit timer, we want 1/2 tick of rounding and
3550          * 1 extra tick because of +-1/2 tick uncertainty in the
3551          * firing of the timer.  The bias will give us exactly the
3552          * 1.5 tick we need.  But, because the bias is
3553          * statistical, we have to test that we don't drop below
3554          * the minimum feasible timer (which is 2 ticks).
3555          */
3556         TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
3557                       max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3558
3559         /*
3560          * We received an ack for a packet that wasn't retransmitted;
3561          * it is probably safe to discard any error indications we've
3562          * received recently.  This isn't quite right, but close enough
3563          * for now (a route might have failed after we sent a segment,
3564          * and the return path might not be symmetrical).
3565          */
3566         tp->t_softerror = 0;
3567 }
3568
3569 /*
3570  * Determine a reasonable value for maxseg size.
3571  * If the route is known, check route for mtu.
3572  * If none, use an mss that can be handled on the outgoing interface
3573  * without forcing IP to fragment.  If no route is found, route has no mtu,
3574  * or the destination isn't local, use a default, hopefully conservative
3575  * size (usually 512 or the default IP max size, but no more than the mtu
3576  * of the interface), as we can't discover anything about intervening
3577  * gateways or networks.  We also initialize the congestion/slow start
3578  * window to be a single segment if the destination isn't local.
3579  * While looking at the routing entry, we also initialize other path-dependent
3580  * parameters from pre-set or cached values in the routing entry.
3581  *
3582  * NOTE that resulting t_maxseg doesn't include space for TCP options or
3583  * IP options, e.g. IPSEC data, since length of this data may vary, and
3584  * thus it is calculated for every segment separately in tcp_output().
3585  *
3586  * NOTE that this routine is only called when we process an incoming
3587  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3588  * settings are handled in tcp_mssopt().
3589  */
3590 void
3591 tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
3592     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3593 {
3594         int mss = 0;
3595         uint32_t maxmtu = 0;
3596         struct inpcb *inp = tp->t_inpcb;
3597         struct hc_metrics_lite metrics;
3598 #ifdef INET6
3599         int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3600         size_t min_protoh = isipv6 ?
3601                             sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3602                             sizeof (struct tcpiphdr);
3603 #else
3604         const size_t min_protoh = sizeof(struct tcpiphdr);
3605 #endif
3606
3607         INP_WLOCK_ASSERT(tp->t_inpcb);
3608
3609         if (mtuoffer != -1) {
3610                 KASSERT(offer == -1, ("%s: conflict", __func__));
3611                 offer = mtuoffer - min_protoh;
3612         }
3613
3614         /* Initialize. */
3615 #ifdef INET6
3616         if (isipv6) {
3617                 maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
3618                 tp->t_maxseg = V_tcp_v6mssdflt;
3619         }
3620 #endif
3621 #if defined(INET) && defined(INET6)
3622         else
3623 #endif
3624 #ifdef INET
3625         {
3626                 maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
3627                 tp->t_maxseg = V_tcp_mssdflt;
3628         }
3629 #endif
3630
3631         /*
3632          * No route to sender, stay with default mss and return.
3633          */
3634         if (maxmtu == 0) {
3635                 /*
3636                  * In case we return early we need to initialize metrics
3637                  * to a defined state as tcp_hc_get() would do for us
3638                  * if there was no cache hit.
3639                  */
3640                 if (metricptr != NULL)
3641                         bzero(metricptr, sizeof(struct hc_metrics_lite));
3642                 return;
3643         }
3644
3645         /* What have we got? */
3646         switch (offer) {
3647                 case 0:
3648                         /*
3649                          * Offer == 0 means that there was no MSS on the SYN
3650                          * segment, in this case we use tcp_mssdflt as
3651                          * already assigned to t_maxseg above.
3652                          */
3653                         offer = tp->t_maxseg;
3654                         break;
3655
3656                 case -1:
3657                         /*
3658                          * Offer == -1 means that we didn't receive SYN yet.
3659                          */
3660                         /* FALLTHROUGH */
3661
3662                 default:
3663                         /*
3664                          * Prevent DoS attack with too small MSS. Round up
3665                          * to at least minmss.
3666                          */
3667                         offer = max(offer, V_tcp_minmss);
3668         }
3669
3670         /*
3671          * rmx information is now retrieved from tcp_hostcache.
3672          */
3673         tcp_hc_get(&inp->inp_inc, &metrics);
3674         if (metricptr != NULL)
3675                 bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
3676
3677         /*
3678          * If there's a discovered mtu in tcp hostcache, use it.
3679          * Else, use the link mtu.
3680          */
3681         if (metrics.rmx_mtu)
3682                 mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3683         else {
3684 #ifdef INET6
3685                 if (isipv6) {
3686                         mss = maxmtu - min_protoh;
3687                         if (!V_path_mtu_discovery &&
3688                             !in6_localaddr(&inp->in6p_faddr))
3689                                 mss = min(mss, V_tcp_v6mssdflt);
3690                 }
3691 #endif
3692 #if defined(INET) && defined(INET6)
3693                 else
3694 #endif
3695 #ifdef INET
3696                 {
3697                         mss = maxmtu - min_protoh;
3698                         if (!V_path_mtu_discovery &&
3699                             !in_localaddr(inp->inp_faddr))
3700                                 mss = min(mss, V_tcp_mssdflt);
3701                 }
3702 #endif
3703                 /*
3704                  * XXX - The above conditional (mss = maxmtu - min_protoh)
3705                  * probably violates the TCP spec.
3706                  * The problem is that, since we don't know the
3707                  * other end's MSS, we are supposed to use a conservative
3708                  * default.  But, if we do that, then MTU discovery will
3709                  * never actually take place, because the conservative
3710                  * default is much less than the MTUs typically seen
3711                  * on the Internet today.  For the moment, we'll sweep
3712                  * this under the carpet.
3713                  *
3714                  * The conservative default might not actually be a problem
3715                  * if the only case this occurs is when sending an initial
3716                  * SYN with options and data to a host we've never talked
3717                  * to before.  Then, they will reply with an MSS value which
3718                  * will get recorded and the new parameters should get
3719                  * recomputed.  For Further Study.
3720                  */
3721         }
3722         mss = min(mss, offer);
3723
3724         /*
3725          * Sanity check: make sure that maxseg will be large
3726          * enough to allow some data on segments even if the
3727          * all the option space is used (40bytes).  Otherwise
3728          * funny things may happen in tcp_output.
3729          *
3730          * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3731          */
3732         mss = max(mss, 64);
3733
3734         tp->t_maxseg = mss;
3735 }
3736
3737 void
3738 tcp_mss(struct tcpcb *tp, int offer)
3739 {
3740         int mss;
3741         uint32_t bufsize;
3742         struct inpcb *inp;
3743         struct socket *so;
3744         struct hc_metrics_lite metrics;
3745         struct tcp_ifcap cap;
3746
3747         KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
3748
3749         bzero(&cap, sizeof(cap));
3750         tcp_mss_update(tp, offer, -1, &metrics, &cap);
3751
3752         mss = tp->t_maxseg;
3753         inp = tp->t_inpcb;
3754
3755         /*
3756          * If there's a pipesize, change the socket buffer to that size,
3757          * don't change if sb_hiwat is different than default (then it
3758          * has been changed on purpose with setsockopt).
3759          * Make the socket buffers an integral number of mss units;
3760          * if the mss is larger than the socket buffer, decrease the mss.
3761          */
3762         so = inp->inp_socket;
3763         SOCKBUF_LOCK(&so->so_snd);
3764         if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
3765                 bufsize = metrics.rmx_sendpipe;
3766         else
3767                 bufsize = so->so_snd.sb_hiwat;
3768         if (bufsize < mss)
3769                 mss = bufsize;
3770         else {
3771                 bufsize = roundup(bufsize, mss);
3772                 if (bufsize > sb_max)
3773                         bufsize = sb_max;
3774                 if (bufsize > so->so_snd.sb_hiwat)
3775                         (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3776         }
3777         SOCKBUF_UNLOCK(&so->so_snd);
3778         /*
3779          * Sanity check: make sure that maxseg will be large
3780          * enough to allow some data on segments even if the
3781          * all the option space is used (40bytes).  Otherwise
3782          * funny things may happen in tcp_output.
3783          *
3784          * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3785          */
3786         tp->t_maxseg = max(mss, 64);
3787
3788         SOCKBUF_LOCK(&so->so_rcv);
3789         if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
3790                 bufsize = metrics.rmx_recvpipe;
3791         else
3792                 bufsize = so->so_rcv.sb_hiwat;
3793         if (bufsize > mss) {
3794                 bufsize = roundup(bufsize, mss);
3795                 if (bufsize > sb_max)
3796                         bufsize = sb_max;
3797                 if (bufsize > so->so_rcv.sb_hiwat)
3798                         (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3799         }
3800         SOCKBUF_UNLOCK(&so->so_rcv);
3801
3802         /* Check the interface for TSO capabilities. */
3803         if (cap.ifcap & CSUM_TSO) {
3804                 tp->t_flags |= TF_TSO;
3805                 tp->t_tsomax = cap.tsomax;
3806                 tp->t_tsomaxsegcount = cap.tsomaxsegcount;
3807                 tp->t_tsomaxsegsize = cap.tsomaxsegsize;
3808         }
3809 }
3810
3811 /*
3812  * Determine the MSS option to send on an outgoing SYN.
3813  */
3814 int
3815 tcp_mssopt(struct in_conninfo *inc)
3816 {
3817         int mss = 0;
3818         uint32_t thcmtu = 0;
3819         uint32_t maxmtu = 0;
3820         size_t min_protoh;
3821
3822         KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3823
3824 #ifdef INET6
3825         if (inc->inc_flags & INC_ISIPV6) {
3826                 mss = V_tcp_v6mssdflt;
3827                 maxmtu = tcp_maxmtu6(inc, NULL);
3828                 min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3829         }
3830 #endif
3831 #if defined(INET) && defined(INET6)
3832         else
3833 #endif
3834 #ifdef INET
3835         {
3836                 mss = V_tcp_mssdflt;
3837                 maxmtu = tcp_maxmtu(inc, NULL);
3838                 min_protoh = sizeof(struct tcpiphdr);
3839         }
3840 #endif
3841 #if defined(INET6) || defined(INET)
3842         thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
3843 #endif
3844
3845         if (maxmtu && thcmtu)
3846                 mss = min(maxmtu, thcmtu) - min_protoh;
3847         else if (maxmtu || thcmtu)
3848                 mss = max(maxmtu, thcmtu) - min_protoh;
3849
3850         return (mss);
3851 }
3852
3853
3854 /*
3855  * On a partial ack arrives, force the retransmission of the
3856  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3857  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3858  * be started again.
3859  */
3860 void
3861 tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
3862 {
3863         tcp_seq onxt = tp->snd_nxt;
3864         uint32_t ocwnd = tp->snd_cwnd;
3865         u_int maxseg = tcp_maxseg(tp);
3866
3867         INP_WLOCK_ASSERT(tp->t_inpcb);
3868
3869         tcp_timer_activate(tp, TT_REXMT, 0);
3870         tp->t_rtttime = 0;
3871         tp->snd_nxt = th->th_ack;
3872         /*
3873          * Set snd_cwnd to one segment beyond acknowledged offset.
3874          * (tp->snd_una has not yet been updated when this function is called.)
3875          */
3876         tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
3877         tp->t_flags |= TF_ACKNOW;
3878         (void) tp->t_fb->tfb_tcp_output(tp);
3879         tp->snd_cwnd = ocwnd;
3880         if (SEQ_GT(onxt, tp->snd_nxt))
3881                 tp->snd_nxt = onxt;
3882         /*
3883          * Partial window deflation.  Relies on fact that tp->snd_una
3884          * not updated yet.
3885          */
3886         if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3887                 tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3888         else
3889                 tp->snd_cwnd = 0;
3890         tp->snd_cwnd += maxseg;
3891 }
3892
3893 int
3894 tcp_compute_pipe(struct tcpcb *tp)
3895 {
3896         return (tp->snd_max - tp->snd_una +
3897                 tp->sackhint.sack_bytes_rexmit -
3898                 tp->sackhint.sacked_bytes);
3899 }