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