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