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