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