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