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