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