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