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