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