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