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