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