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