tcp_input.c revision 220560
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 of Technology, by Lawrence Stewart, 12 * James Healy and David Hayes, made possible in part by a grant from the Cisco 13 * University 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: head/sys/netinet/tcp_input.c 220560 2011-04-12 08:13:18Z lstewart $"); 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 114const int tcprexmtthresh = 3; 115 116VNET_DEFINE(struct tcpstat, tcpstat); 117SYSCTL_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 121int tcp_log_in_vain = 0; 122SYSCTL_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 126VNET_DEFINE(int, blackhole) = 0; 127#define V_blackhole VNET(blackhole) 128SYSCTL_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 132VNET_DEFINE(int, tcp_delack_enabled) = 1; 133SYSCTL_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 137VNET_DEFINE(int, drop_synfin) = 0; 138#define V_drop_synfin VNET(drop_synfin) 139SYSCTL_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 143VNET_DEFINE(int, tcp_do_rfc3042) = 1; 144#define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 145SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 146 &VNET_NAME(tcp_do_rfc3042), 0, 147 "Enable RFC 3042 (Limited Transmit)"); 148 149VNET_DEFINE(int, tcp_do_rfc3390) = 1; 150SYSCTL_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 154VNET_DEFINE(int, tcp_do_rfc3465) = 1; 155SYSCTL_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 159VNET_DEFINE(int, tcp_abc_l_var) = 2; 160SYSCTL_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 164SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 165 166VNET_DEFINE(int, tcp_do_ecn) = 0; 167SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_RW, 168 &VNET_NAME(tcp_do_ecn), 0, 169 "TCP ECN support"); 170 171VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 172SYSCTL_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 176VNET_DEFINE(int, tcp_insecure_rst) = 0; 177#define V_tcp_insecure_rst VNET(tcp_insecure_rst) 178SYSCTL_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 182VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 183#define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 184SYSCTL_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 188VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 189#define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 190SYSCTL_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 194VNET_DEFINE(int, tcp_autorcvbuf_max) = 256*1024; 195#define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 196SYSCTL_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 200int tcp_read_locking = 1; 201SYSCTL_INT(_net_inet_tcp, OID_AUTO, read_locking, CTLFLAG_RW, 202 &tcp_read_locking, 0, "Enable read locking strategy"); 203 204VNET_DEFINE(struct inpcbhead, tcb); 205#define tcb6 tcb /* for KAME src sync over BSD*'s */ 206VNET_DEFINE(struct inpcbinfo, tcbinfo); 207 208static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 209static void tcp_do_segment(struct mbuf *, struct tcphdr *, 210 struct socket *, struct tcpcb *, int, int, uint8_t, 211 int); 212static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 213 struct tcpcb *, int, int); 214static void tcp_pulloutofband(struct socket *, 215 struct tcphdr *, struct mbuf *, int); 216static void tcp_xmit_timer(struct tcpcb *, int); 217static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 218static void inline cc_ack_received(struct tcpcb *tp, struct tcphdr *th, 219 uint16_t type); 220static void inline cc_conn_init(struct tcpcb *tp); 221static void inline cc_post_recovery(struct tcpcb *tp, struct tcphdr *th); 222static 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 */ 232void 233kmod_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 */ 242static void inline 243hhook_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 */ 260static void inline 261cc_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 292static void inline 293cc_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 377void inline 378cc_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 428static void inline 429cc_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) \ 446do { \ 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 478int 479tcp6_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 506void 507tcp_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 706findpcb: 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 */ 842relocked: 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 1237dropwithreset: 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 1254dropunlock: 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 1266drop: 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 1274static void 1275tcp_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 tp->sackhint.last_sack_ack = 0; 1295 1296 /* 1297 * If this is either a state-changing packet or current state isn't 1298 * established, we require a write lock on tcbinfo. Otherwise, we 1299 * allow either a read lock or a write lock, as we may have acquired 1300 * a write lock due to a race. 1301 * 1302 * Require a global write lock for SYN/FIN/RST segments or 1303 * non-established connections; otherwise accept either a read or 1304 * write lock, as we may have conservatively acquired a write lock in 1305 * certain cases in tcp_input() (is this still true?). Currently we 1306 * will never enter with no lock, so we try to drop it quickly in the 1307 * common pure ack/pure data cases. 1308 */ 1309 if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1310 tp->t_state != TCPS_ESTABLISHED) { 1311 KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1312 "SYN/FIN/RST/!EST", __func__, ti_locked)); 1313 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1314 } else { 1315#ifdef INVARIANTS 1316 if (ti_locked == TI_RLOCKED) 1317 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1318 else if (ti_locked == TI_WLOCKED) 1319 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1320 else 1321 panic("%s: ti_locked %d for EST", __func__, 1322 ti_locked); 1323#endif 1324 } 1325 INP_WLOCK_ASSERT(tp->t_inpcb); 1326 KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1327 __func__)); 1328 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1329 __func__)); 1330 1331 /* 1332 * Segment received on connection. 1333 * Reset idle time and keep-alive timer. 1334 * XXX: This should be done after segment 1335 * validation to ignore broken/spoofed segs. 1336 */ 1337 tp->t_rcvtime = ticks; 1338 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1339 tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 1340 1341 /* 1342 * Unscale the window into a 32-bit value. 1343 * For the SYN_SENT state the scale is zero. 1344 */ 1345 tiwin = th->th_win << tp->snd_scale; 1346 1347 /* 1348 * TCP ECN processing. 1349 */ 1350 if (tp->t_flags & TF_ECN_PERMIT) { 1351 if (thflags & TH_CWR) 1352 tp->t_flags &= ~TF_ECN_SND_ECE; 1353 switch (iptos & IPTOS_ECN_MASK) { 1354 case IPTOS_ECN_CE: 1355 tp->t_flags |= TF_ECN_SND_ECE; 1356 TCPSTAT_INC(tcps_ecn_ce); 1357 break; 1358 case IPTOS_ECN_ECT0: 1359 TCPSTAT_INC(tcps_ecn_ect0); 1360 break; 1361 case IPTOS_ECN_ECT1: 1362 TCPSTAT_INC(tcps_ecn_ect1); 1363 break; 1364 } 1365 /* Congestion experienced. */ 1366 if (thflags & TH_ECE) { 1367 cc_cong_signal(tp, th, CC_ECN); 1368 } 1369 } 1370 1371 /* 1372 * Parse options on any incoming segment. 1373 */ 1374 tcp_dooptions(&to, (u_char *)(th + 1), 1375 (th->th_off << 2) - sizeof(struct tcphdr), 1376 (thflags & TH_SYN) ? TO_SYN : 0); 1377 1378 /* 1379 * If echoed timestamp is later than the current time, 1380 * fall back to non RFC1323 RTT calculation. Normalize 1381 * timestamp if syncookies were used when this connection 1382 * was established. 1383 */ 1384 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1385 to.to_tsecr -= tp->ts_offset; 1386 if (TSTMP_GT(to.to_tsecr, ticks)) 1387 to.to_tsecr = 0; 1388 } 1389 1390 /* 1391 * Process options only when we get SYN/ACK back. The SYN case 1392 * for incoming connections is handled in tcp_syncache. 1393 * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1394 * or <SYN,ACK>) segment itself is never scaled. 1395 * XXX this is traditional behavior, may need to be cleaned up. 1396 */ 1397 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1398 if ((to.to_flags & TOF_SCALE) && 1399 (tp->t_flags & TF_REQ_SCALE)) { 1400 tp->t_flags |= TF_RCVD_SCALE; 1401 tp->snd_scale = to.to_wscale; 1402 } 1403 /* 1404 * Initial send window. It will be updated with 1405 * the next incoming segment to the scaled value. 1406 */ 1407 tp->snd_wnd = th->th_win; 1408 if (to.to_flags & TOF_TS) { 1409 tp->t_flags |= TF_RCVD_TSTMP; 1410 tp->ts_recent = to.to_tsval; 1411 tp->ts_recent_age = ticks; 1412 } 1413 if (to.to_flags & TOF_MSS) 1414 tcp_mss(tp, to.to_mss); 1415 if ((tp->t_flags & TF_SACK_PERMIT) && 1416 (to.to_flags & TOF_SACKPERM) == 0) 1417 tp->t_flags &= ~TF_SACK_PERMIT; 1418 } 1419 1420 /* 1421 * Header prediction: check for the two common cases 1422 * of a uni-directional data xfer. If the packet has 1423 * no control flags, is in-sequence, the window didn't 1424 * change and we're not retransmitting, it's a 1425 * candidate. If the length is zero and the ack moved 1426 * forward, we're the sender side of the xfer. Just 1427 * free the data acked & wake any higher level process 1428 * that was blocked waiting for space. If the length 1429 * is non-zero and the ack didn't move, we're the 1430 * receiver side. If we're getting packets in-order 1431 * (the reassembly queue is empty), add the data to 1432 * the socket buffer and note that we need a delayed ack. 1433 * Make sure that the hidden state-flags are also off. 1434 * Since we check for TCPS_ESTABLISHED first, it can only 1435 * be TH_NEEDSYN. 1436 */ 1437 if (tp->t_state == TCPS_ESTABLISHED && 1438 th->th_seq == tp->rcv_nxt && 1439 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1440 tp->snd_nxt == tp->snd_max && 1441 tiwin && tiwin == tp->snd_wnd && 1442 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1443 LIST_EMPTY(&tp->t_segq) && 1444 ((to.to_flags & TOF_TS) == 0 || 1445 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1446 1447 /* 1448 * If last ACK falls within this segment's sequence numbers, 1449 * record the timestamp. 1450 * NOTE that the test is modified according to the latest 1451 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1452 */ 1453 if ((to.to_flags & TOF_TS) != 0 && 1454 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1455 tp->ts_recent_age = ticks; 1456 tp->ts_recent = to.to_tsval; 1457 } 1458 1459 if (tlen == 0) { 1460 if (SEQ_GT(th->th_ack, tp->snd_una) && 1461 SEQ_LEQ(th->th_ack, tp->snd_max) && 1462 !IN_RECOVERY(tp->t_flags) && 1463 (to.to_flags & TOF_SACK) == 0 && 1464 TAILQ_EMPTY(&tp->snd_holes)) { 1465 /* 1466 * This is a pure ack for outstanding data. 1467 */ 1468 if (ti_locked == TI_RLOCKED) 1469 INP_INFO_RUNLOCK(&V_tcbinfo); 1470 else if (ti_locked == TI_WLOCKED) 1471 INP_INFO_WUNLOCK(&V_tcbinfo); 1472 else 1473 panic("%s: ti_locked %d on pure ACK", 1474 __func__, ti_locked); 1475 ti_locked = TI_UNLOCKED; 1476 1477 TCPSTAT_INC(tcps_predack); 1478 1479 /* 1480 * "bad retransmit" recovery. 1481 */ 1482 if (tp->t_rxtshift == 1 && 1483 (int)(ticks - tp->t_badrxtwin) < 0) { 1484 cc_cong_signal(tp, th, CC_RTO_ERR); 1485 } 1486 1487 /* 1488 * Recalculate the transmit timer / rtt. 1489 * 1490 * Some boxes send broken timestamp replies 1491 * during the SYN+ACK phase, ignore 1492 * timestamps of 0 or we could calculate a 1493 * huge RTT and blow up the retransmit timer. 1494 */ 1495 if ((to.to_flags & TOF_TS) != 0 && 1496 to.to_tsecr) { 1497 if (!tp->t_rttlow || 1498 tp->t_rttlow > ticks - to.to_tsecr) 1499 tp->t_rttlow = ticks - to.to_tsecr; 1500 tcp_xmit_timer(tp, 1501 ticks - to.to_tsecr + 1); 1502 } else if (tp->t_rtttime && 1503 SEQ_GT(th->th_ack, tp->t_rtseq)) { 1504 if (!tp->t_rttlow || 1505 tp->t_rttlow > ticks - tp->t_rtttime) 1506 tp->t_rttlow = ticks - tp->t_rtttime; 1507 tcp_xmit_timer(tp, 1508 ticks - tp->t_rtttime); 1509 } 1510 acked = BYTES_THIS_ACK(tp, th); 1511 1512 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 1513 hhook_run_tcp_est_in(tp, th, &to); 1514 1515 TCPSTAT_INC(tcps_rcvackpack); 1516 TCPSTAT_ADD(tcps_rcvackbyte, acked); 1517 sbdrop(&so->so_snd, acked); 1518 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 1519 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1520 tp->snd_recover = th->th_ack - 1; 1521 1522 /* 1523 * Let the congestion control algorithm update 1524 * congestion control related information. This 1525 * typically means increasing the congestion 1526 * window. 1527 */ 1528 cc_ack_received(tp, th, CC_ACK); 1529 1530 tp->snd_una = th->th_ack; 1531 /* 1532 * Pull snd_wl2 up to prevent seq wrap relative 1533 * to th_ack. 1534 */ 1535 tp->snd_wl2 = th->th_ack; 1536 tp->t_dupacks = 0; 1537 m_freem(m); 1538 ND6_HINT(tp); /* Some progress has been made. */ 1539 1540 /* 1541 * If all outstanding data are acked, stop 1542 * retransmit timer, otherwise restart timer 1543 * using current (possibly backed-off) value. 1544 * If process is waiting for space, 1545 * wakeup/selwakeup/signal. If data 1546 * are ready to send, let tcp_output 1547 * decide between more output or persist. 1548 */ 1549#ifdef TCPDEBUG 1550 if (so->so_options & SO_DEBUG) 1551 tcp_trace(TA_INPUT, ostate, tp, 1552 (void *)tcp_saveipgen, 1553 &tcp_savetcp, 0); 1554#endif 1555 if (tp->snd_una == tp->snd_max) 1556 tcp_timer_activate(tp, TT_REXMT, 0); 1557 else if (!tcp_timer_active(tp, TT_PERSIST)) 1558 tcp_timer_activate(tp, TT_REXMT, 1559 tp->t_rxtcur); 1560 sowwakeup(so); 1561 if (so->so_snd.sb_cc) 1562 (void) tcp_output(tp); 1563 goto check_delack; 1564 } 1565 } else if (th->th_ack == tp->snd_una && 1566 tlen <= sbspace(&so->so_rcv)) { 1567 int newsize = 0; /* automatic sockbuf scaling */ 1568 1569 /* 1570 * This is a pure, in-sequence data packet with 1571 * nothing on the reassembly queue and we have enough 1572 * buffer space to take it. 1573 */ 1574 if (ti_locked == TI_RLOCKED) 1575 INP_INFO_RUNLOCK(&V_tcbinfo); 1576 else if (ti_locked == TI_WLOCKED) 1577 INP_INFO_WUNLOCK(&V_tcbinfo); 1578 else 1579 panic("%s: ti_locked %d on pure data " 1580 "segment", __func__, ti_locked); 1581 ti_locked = TI_UNLOCKED; 1582 1583 /* Clean receiver SACK report if present */ 1584 if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 1585 tcp_clean_sackreport(tp); 1586 TCPSTAT_INC(tcps_preddat); 1587 tp->rcv_nxt += tlen; 1588 /* 1589 * Pull snd_wl1 up to prevent seq wrap relative to 1590 * th_seq. 1591 */ 1592 tp->snd_wl1 = th->th_seq; 1593 /* 1594 * Pull rcv_up up to prevent seq wrap relative to 1595 * rcv_nxt. 1596 */ 1597 tp->rcv_up = tp->rcv_nxt; 1598 TCPSTAT_INC(tcps_rcvpack); 1599 TCPSTAT_ADD(tcps_rcvbyte, tlen); 1600 ND6_HINT(tp); /* Some progress has been made */ 1601#ifdef TCPDEBUG 1602 if (so->so_options & SO_DEBUG) 1603 tcp_trace(TA_INPUT, ostate, tp, 1604 (void *)tcp_saveipgen, &tcp_savetcp, 0); 1605#endif 1606 /* 1607 * Automatic sizing of receive socket buffer. Often the send 1608 * buffer size is not optimally adjusted to the actual network 1609 * conditions at hand (delay bandwidth product). Setting the 1610 * buffer size too small limits throughput on links with high 1611 * bandwidth and high delay (eg. trans-continental/oceanic links). 1612 * 1613 * On the receive side the socket buffer memory is only rarely 1614 * used to any significant extent. This allows us to be much 1615 * more aggressive in scaling the receive socket buffer. For 1616 * the case that the buffer space is actually used to a large 1617 * extent and we run out of kernel memory we can simply drop 1618 * the new segments; TCP on the sender will just retransmit it 1619 * later. Setting the buffer size too big may only consume too 1620 * much kernel memory if the application doesn't read() from 1621 * the socket or packet loss or reordering makes use of the 1622 * reassembly queue. 1623 * 1624 * The criteria to step up the receive buffer one notch are: 1625 * 1. the number of bytes received during the time it takes 1626 * one timestamp to be reflected back to us (the RTT); 1627 * 2. received bytes per RTT is within seven eighth of the 1628 * current socket buffer size; 1629 * 3. receive buffer size has not hit maximal automatic size; 1630 * 1631 * This algorithm does one step per RTT at most and only if 1632 * we receive a bulk stream w/o packet losses or reorderings. 1633 * Shrinking the buffer during idle times is not necessary as 1634 * it doesn't consume any memory when idle. 1635 * 1636 * TODO: Only step up if the application is actually serving 1637 * the buffer to better manage the socket buffer resources. 1638 */ 1639 if (V_tcp_do_autorcvbuf && 1640 to.to_tsecr && 1641 (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 1642 if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) && 1643 to.to_tsecr - tp->rfbuf_ts < hz) { 1644 if (tp->rfbuf_cnt > 1645 (so->so_rcv.sb_hiwat / 8 * 7) && 1646 so->so_rcv.sb_hiwat < 1647 V_tcp_autorcvbuf_max) { 1648 newsize = 1649 min(so->so_rcv.sb_hiwat + 1650 V_tcp_autorcvbuf_inc, 1651 V_tcp_autorcvbuf_max); 1652 } 1653 /* Start over with next RTT. */ 1654 tp->rfbuf_ts = 0; 1655 tp->rfbuf_cnt = 0; 1656 } else 1657 tp->rfbuf_cnt += tlen; /* add up */ 1658 } 1659 1660 /* Add data to socket buffer. */ 1661 SOCKBUF_LOCK(&so->so_rcv); 1662 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1663 m_freem(m); 1664 } else { 1665 /* 1666 * Set new socket buffer size. 1667 * Give up when limit is reached. 1668 */ 1669 if (newsize) 1670 if (!sbreserve_locked(&so->so_rcv, 1671 newsize, so, NULL)) 1672 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1673 m_adj(m, drop_hdrlen); /* delayed header drop */ 1674 sbappendstream_locked(&so->so_rcv, m); 1675 } 1676 /* NB: sorwakeup_locked() does an implicit unlock. */ 1677 sorwakeup_locked(so); 1678 if (DELAY_ACK(tp)) { 1679 tp->t_flags |= TF_DELACK; 1680 } else { 1681 tp->t_flags |= TF_ACKNOW; 1682 tcp_output(tp); 1683 } 1684 goto check_delack; 1685 } 1686 } 1687 1688 /* 1689 * Calculate amount of space in receive window, 1690 * and then do TCP input processing. 1691 * Receive window is amount of space in rcv queue, 1692 * but not less than advertised window. 1693 */ 1694 win = sbspace(&so->so_rcv); 1695 if (win < 0) 1696 win = 0; 1697 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1698 1699 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 1700 tp->rfbuf_ts = 0; 1701 tp->rfbuf_cnt = 0; 1702 1703 switch (tp->t_state) { 1704 1705 /* 1706 * If the state is SYN_RECEIVED: 1707 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1708 */ 1709 case TCPS_SYN_RECEIVED: 1710 if ((thflags & TH_ACK) && 1711 (SEQ_LEQ(th->th_ack, tp->snd_una) || 1712 SEQ_GT(th->th_ack, tp->snd_max))) { 1713 rstreason = BANDLIM_RST_OPENPORT; 1714 goto dropwithreset; 1715 } 1716 break; 1717 1718 /* 1719 * If the state is SYN_SENT: 1720 * if seg contains an ACK, but not for our SYN, drop the input. 1721 * if seg contains a RST, then drop the connection. 1722 * if seg does not contain SYN, then drop it. 1723 * Otherwise this is an acceptable SYN segment 1724 * initialize tp->rcv_nxt and tp->irs 1725 * if seg contains ack then advance tp->snd_una 1726 * if seg contains an ECE and ECN support is enabled, the stream 1727 * is ECN capable. 1728 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1729 * arrange for segment to be acked (eventually) 1730 * continue processing rest of data/controls, beginning with URG 1731 */ 1732 case TCPS_SYN_SENT: 1733 if ((thflags & TH_ACK) && 1734 (SEQ_LEQ(th->th_ack, tp->iss) || 1735 SEQ_GT(th->th_ack, tp->snd_max))) { 1736 rstreason = BANDLIM_UNLIMITED; 1737 goto dropwithreset; 1738 } 1739 if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1740 tp = tcp_drop(tp, ECONNREFUSED); 1741 if (thflags & TH_RST) 1742 goto drop; 1743 if (!(thflags & TH_SYN)) 1744 goto drop; 1745 1746 tp->irs = th->th_seq; 1747 tcp_rcvseqinit(tp); 1748 if (thflags & TH_ACK) { 1749 TCPSTAT_INC(tcps_connects); 1750 soisconnected(so); 1751#ifdef MAC 1752 mac_socketpeer_set_from_mbuf(m, so); 1753#endif 1754 /* Do window scaling on this connection? */ 1755 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1756 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1757 tp->rcv_scale = tp->request_r_scale; 1758 } 1759 tp->rcv_adv += imin(tp->rcv_wnd, 1760 TCP_MAXWIN << tp->rcv_scale); 1761 tp->snd_una++; /* SYN is acked */ 1762 /* 1763 * If there's data, delay ACK; if there's also a FIN 1764 * ACKNOW will be turned on later. 1765 */ 1766 if (DELAY_ACK(tp) && tlen != 0) 1767 tcp_timer_activate(tp, TT_DELACK, 1768 tcp_delacktime); 1769 else 1770 tp->t_flags |= TF_ACKNOW; 1771 1772 if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1773 tp->t_flags |= TF_ECN_PERMIT; 1774 TCPSTAT_INC(tcps_ecn_shs); 1775 } 1776 1777 /* 1778 * Received <SYN,ACK> in SYN_SENT[*] state. 1779 * Transitions: 1780 * SYN_SENT --> ESTABLISHED 1781 * SYN_SENT* --> FIN_WAIT_1 1782 */ 1783 tp->t_starttime = ticks; 1784 if (tp->t_flags & TF_NEEDFIN) { 1785 tp->t_state = TCPS_FIN_WAIT_1; 1786 tp->t_flags &= ~TF_NEEDFIN; 1787 thflags &= ~TH_SYN; 1788 } else { 1789 tp->t_state = TCPS_ESTABLISHED; 1790 cc_conn_init(tp); 1791 tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 1792 } 1793 } else { 1794 /* 1795 * Received initial SYN in SYN-SENT[*] state => 1796 * simultaneous open. If segment contains CC option 1797 * and there is a cached CC, apply TAO test. 1798 * If it succeeds, connection is * half-synchronized. 1799 * Otherwise, do 3-way handshake: 1800 * SYN-SENT -> SYN-RECEIVED 1801 * SYN-SENT* -> SYN-RECEIVED* 1802 * If there was no CC option, clear cached CC value. 1803 */ 1804 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1805 tcp_timer_activate(tp, TT_REXMT, 0); 1806 tp->t_state = TCPS_SYN_RECEIVED; 1807 } 1808 1809 KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 1810 "ti_locked %d", __func__, ti_locked)); 1811 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1812 INP_WLOCK_ASSERT(tp->t_inpcb); 1813 1814 /* 1815 * Advance th->th_seq to correspond to first data byte. 1816 * If data, trim to stay within window, 1817 * dropping FIN if necessary. 1818 */ 1819 th->th_seq++; 1820 if (tlen > tp->rcv_wnd) { 1821 todrop = tlen - tp->rcv_wnd; 1822 m_adj(m, -todrop); 1823 tlen = tp->rcv_wnd; 1824 thflags &= ~TH_FIN; 1825 TCPSTAT_INC(tcps_rcvpackafterwin); 1826 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1827 } 1828 tp->snd_wl1 = th->th_seq - 1; 1829 tp->rcv_up = th->th_seq; 1830 /* 1831 * Client side of transaction: already sent SYN and data. 1832 * If the remote host used T/TCP to validate the SYN, 1833 * our data will be ACK'd; if so, enter normal data segment 1834 * processing in the middle of step 5, ack processing. 1835 * Otherwise, goto step 6. 1836 */ 1837 if (thflags & TH_ACK) 1838 goto process_ACK; 1839 1840 goto step6; 1841 1842 /* 1843 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1844 * do normal processing. 1845 * 1846 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1847 */ 1848 case TCPS_LAST_ACK: 1849 case TCPS_CLOSING: 1850 break; /* continue normal processing */ 1851 } 1852 1853 /* 1854 * States other than LISTEN or SYN_SENT. 1855 * First check the RST flag and sequence number since reset segments 1856 * are exempt from the timestamp and connection count tests. This 1857 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 1858 * below which allowed reset segments in half the sequence space 1859 * to fall though and be processed (which gives forged reset 1860 * segments with a random sequence number a 50 percent chance of 1861 * killing a connection). 1862 * Then check timestamp, if present. 1863 * Then check the connection count, if present. 1864 * Then check that at least some bytes of segment are within 1865 * receive window. If segment begins before rcv_nxt, 1866 * drop leading data (and SYN); if nothing left, just ack. 1867 * 1868 * 1869 * If the RST bit is set, check the sequence number to see 1870 * if this is a valid reset segment. 1871 * RFC 793 page 37: 1872 * In all states except SYN-SENT, all reset (RST) segments 1873 * are validated by checking their SEQ-fields. A reset is 1874 * valid if its sequence number is in the window. 1875 * Note: this does not take into account delayed ACKs, so 1876 * we should test against last_ack_sent instead of rcv_nxt. 1877 * The sequence number in the reset segment is normally an 1878 * echo of our outgoing acknowlegement numbers, but some hosts 1879 * send a reset with the sequence number at the rightmost edge 1880 * of our receive window, and we have to handle this case. 1881 * Note 2: Paul Watson's paper "Slipping in the Window" has shown 1882 * that brute force RST attacks are possible. To combat this, 1883 * we use a much stricter check while in the ESTABLISHED state, 1884 * only accepting RSTs where the sequence number is equal to 1885 * last_ack_sent. In all other states (the states in which a 1886 * RST is more likely), the more permissive check is used. 1887 * If we have multiple segments in flight, the initial reset 1888 * segment sequence numbers will be to the left of last_ack_sent, 1889 * but they will eventually catch up. 1890 * In any case, it never made sense to trim reset segments to 1891 * fit the receive window since RFC 1122 says: 1892 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 1893 * 1894 * A TCP SHOULD allow a received RST segment to include data. 1895 * 1896 * DISCUSSION 1897 * It has been suggested that a RST segment could contain 1898 * ASCII text that encoded and explained the cause of the 1899 * RST. No standard has yet been established for such 1900 * data. 1901 * 1902 * If the reset segment passes the sequence number test examine 1903 * the state: 1904 * SYN_RECEIVED STATE: 1905 * If passive open, return to LISTEN state. 1906 * If active open, inform user that connection was refused. 1907 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 1908 * Inform user that connection was reset, and close tcb. 1909 * CLOSING, LAST_ACK STATES: 1910 * Close the tcb. 1911 * TIME_WAIT STATE: 1912 * Drop the segment - see Stevens, vol. 2, p. 964 and 1913 * RFC 1337. 1914 */ 1915 if (thflags & TH_RST) { 1916 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 1917 SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 1918 switch (tp->t_state) { 1919 1920 case TCPS_SYN_RECEIVED: 1921 so->so_error = ECONNREFUSED; 1922 goto close; 1923 1924 case TCPS_ESTABLISHED: 1925 if (V_tcp_insecure_rst == 0 && 1926 !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 1927 SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 1928 !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 1929 SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 1930 TCPSTAT_INC(tcps_badrst); 1931 goto drop; 1932 } 1933 /* FALLTHROUGH */ 1934 case TCPS_FIN_WAIT_1: 1935 case TCPS_FIN_WAIT_2: 1936 case TCPS_CLOSE_WAIT: 1937 so->so_error = ECONNRESET; 1938 close: 1939 KASSERT(ti_locked == TI_WLOCKED, 1940 ("tcp_do_segment: TH_RST 1 ti_locked %d", 1941 ti_locked)); 1942 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1943 1944 tp->t_state = TCPS_CLOSED; 1945 TCPSTAT_INC(tcps_drops); 1946 tp = tcp_close(tp); 1947 break; 1948 1949 case TCPS_CLOSING: 1950 case TCPS_LAST_ACK: 1951 KASSERT(ti_locked == TI_WLOCKED, 1952 ("tcp_do_segment: TH_RST 2 ti_locked %d", 1953 ti_locked)); 1954 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1955 1956 tp = tcp_close(tp); 1957 break; 1958 } 1959 } 1960 goto drop; 1961 } 1962 1963 /* 1964 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1965 * and it's less than ts_recent, drop it. 1966 */ 1967 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 1968 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1969 1970 /* Check to see if ts_recent is over 24 days old. */ 1971 if (ticks - tp->ts_recent_age > TCP_PAWS_IDLE) { 1972 /* 1973 * Invalidate ts_recent. If this segment updates 1974 * ts_recent, the age will be reset later and ts_recent 1975 * will get a valid value. If it does not, setting 1976 * ts_recent to zero will at least satisfy the 1977 * requirement that zero be placed in the timestamp 1978 * echo reply when ts_recent isn't valid. The 1979 * age isn't reset until we get a valid ts_recent 1980 * because we don't want out-of-order segments to be 1981 * dropped when ts_recent is old. 1982 */ 1983 tp->ts_recent = 0; 1984 } else { 1985 TCPSTAT_INC(tcps_rcvduppack); 1986 TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 1987 TCPSTAT_INC(tcps_pawsdrop); 1988 if (tlen) 1989 goto dropafterack; 1990 goto drop; 1991 } 1992 } 1993 1994 /* 1995 * In the SYN-RECEIVED state, validate that the packet belongs to 1996 * this connection before trimming the data to fit the receive 1997 * window. Check the sequence number versus IRS since we know 1998 * the sequence numbers haven't wrapped. This is a partial fix 1999 * for the "LAND" DoS attack. 2000 */ 2001 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2002 rstreason = BANDLIM_RST_OPENPORT; 2003 goto dropwithreset; 2004 } 2005 2006 todrop = tp->rcv_nxt - th->th_seq; 2007 if (todrop > 0) { 2008 /* 2009 * If this is a duplicate SYN for our current connection, 2010 * advance over it and pretend and it's not a SYN. 2011 */ 2012 if (thflags & TH_SYN && th->th_seq == tp->irs) { 2013 thflags &= ~TH_SYN; 2014 th->th_seq++; 2015 if (th->th_urp > 1) 2016 th->th_urp--; 2017 else 2018 thflags &= ~TH_URG; 2019 todrop--; 2020 } 2021 /* 2022 * Following if statement from Stevens, vol. 2, p. 960. 2023 */ 2024 if (todrop > tlen 2025 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2026 /* 2027 * Any valid FIN must be to the left of the window. 2028 * At this point the FIN must be a duplicate or out 2029 * of sequence; drop it. 2030 */ 2031 thflags &= ~TH_FIN; 2032 2033 /* 2034 * Send an ACK to resynchronize and drop any data. 2035 * But keep on processing for RST or ACK. 2036 */ 2037 tp->t_flags |= TF_ACKNOW; 2038 todrop = tlen; 2039 TCPSTAT_INC(tcps_rcvduppack); 2040 TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2041 } else { 2042 TCPSTAT_INC(tcps_rcvpartduppack); 2043 TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2044 } 2045 drop_hdrlen += todrop; /* drop from the top afterwards */ 2046 th->th_seq += todrop; 2047 tlen -= todrop; 2048 if (th->th_urp > todrop) 2049 th->th_urp -= todrop; 2050 else { 2051 thflags &= ~TH_URG; 2052 th->th_urp = 0; 2053 } 2054 } 2055 2056 /* 2057 * If new data are received on a connection after the 2058 * user processes are gone, then RST the other end. 2059 */ 2060 if ((so->so_state & SS_NOFDREF) && 2061 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2062 char *s; 2063 2064 KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 2065 "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2066 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2067 2068 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 2069 log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 2070 "was closed, sending RST and removing tcpcb\n", 2071 s, __func__, tcpstates[tp->t_state], tlen); 2072 free(s, M_TCPLOG); 2073 } 2074 tp = tcp_close(tp); 2075 TCPSTAT_INC(tcps_rcvafterclose); 2076 rstreason = BANDLIM_UNLIMITED; 2077 goto dropwithreset; 2078 } 2079 2080 /* 2081 * If segment ends after window, drop trailing data 2082 * (and PUSH and FIN); if nothing left, just ACK. 2083 */ 2084 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2085 if (todrop > 0) { 2086 TCPSTAT_INC(tcps_rcvpackafterwin); 2087 if (todrop >= tlen) { 2088 TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2089 /* 2090 * If window is closed can only take segments at 2091 * window edge, and have to drop data and PUSH from 2092 * incoming segments. Continue processing, but 2093 * remember to ack. Otherwise, drop segment 2094 * and ack. 2095 */ 2096 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2097 tp->t_flags |= TF_ACKNOW; 2098 TCPSTAT_INC(tcps_rcvwinprobe); 2099 } else 2100 goto dropafterack; 2101 } else 2102 TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2103 m_adj(m, -todrop); 2104 tlen -= todrop; 2105 thflags &= ~(TH_PUSH|TH_FIN); 2106 } 2107 2108 /* 2109 * If last ACK falls within this segment's sequence numbers, 2110 * record its timestamp. 2111 * NOTE: 2112 * 1) That the test incorporates suggestions from the latest 2113 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2114 * 2) That updating only on newer timestamps interferes with 2115 * our earlier PAWS tests, so this check should be solely 2116 * predicated on the sequence space of this segment. 2117 * 3) That we modify the segment boundary check to be 2118 * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2119 * instead of RFC1323's 2120 * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2121 * This modified check allows us to overcome RFC1323's 2122 * limitations as described in Stevens TCP/IP Illustrated 2123 * Vol. 2 p.869. In such cases, we can still calculate the 2124 * RTT correctly when RCV.NXT == Last.ACK.Sent. 2125 */ 2126 if ((to.to_flags & TOF_TS) != 0 && 2127 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2128 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2129 ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2130 tp->ts_recent_age = ticks; 2131 tp->ts_recent = to.to_tsval; 2132 } 2133 2134 /* 2135 * If a SYN is in the window, then this is an 2136 * error and we send an RST and drop the connection. 2137 */ 2138 if (thflags & TH_SYN) { 2139 KASSERT(ti_locked == TI_WLOCKED, 2140 ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2141 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2142 2143 tp = tcp_drop(tp, ECONNRESET); 2144 rstreason = BANDLIM_UNLIMITED; 2145 goto drop; 2146 } 2147 2148 /* 2149 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2150 * flag is on (half-synchronized state), then queue data for 2151 * later processing; else drop segment and return. 2152 */ 2153 if ((thflags & TH_ACK) == 0) { 2154 if (tp->t_state == TCPS_SYN_RECEIVED || 2155 (tp->t_flags & TF_NEEDSYN)) 2156 goto step6; 2157 else if (tp->t_flags & TF_ACKNOW) 2158 goto dropafterack; 2159 else 2160 goto drop; 2161 } 2162 2163 /* 2164 * Ack processing. 2165 */ 2166 switch (tp->t_state) { 2167 2168 /* 2169 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2170 * ESTABLISHED state and continue processing. 2171 * The ACK was checked above. 2172 */ 2173 case TCPS_SYN_RECEIVED: 2174 2175 TCPSTAT_INC(tcps_connects); 2176 soisconnected(so); 2177 /* Do window scaling? */ 2178 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2179 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2180 tp->rcv_scale = tp->request_r_scale; 2181 tp->snd_wnd = tiwin; 2182 } 2183 /* 2184 * Make transitions: 2185 * SYN-RECEIVED -> ESTABLISHED 2186 * SYN-RECEIVED* -> FIN-WAIT-1 2187 */ 2188 tp->t_starttime = ticks; 2189 if (tp->t_flags & TF_NEEDFIN) { 2190 tp->t_state = TCPS_FIN_WAIT_1; 2191 tp->t_flags &= ~TF_NEEDFIN; 2192 } else { 2193 tp->t_state = TCPS_ESTABLISHED; 2194 cc_conn_init(tp); 2195 tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 2196 } 2197 /* 2198 * If segment contains data or ACK, will call tcp_reass() 2199 * later; if not, do so now to pass queued data to user. 2200 */ 2201 if (tlen == 0 && (thflags & TH_FIN) == 0) 2202 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2203 (struct mbuf *)0); 2204 tp->snd_wl1 = th->th_seq - 1; 2205 /* FALLTHROUGH */ 2206 2207 /* 2208 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2209 * ACKs. If the ack is in the range 2210 * tp->snd_una < th->th_ack <= tp->snd_max 2211 * then advance tp->snd_una to th->th_ack and drop 2212 * data from the retransmission queue. If this ACK reflects 2213 * more up to date window information we update our window information. 2214 */ 2215 case TCPS_ESTABLISHED: 2216 case TCPS_FIN_WAIT_1: 2217 case TCPS_FIN_WAIT_2: 2218 case TCPS_CLOSE_WAIT: 2219 case TCPS_CLOSING: 2220 case TCPS_LAST_ACK: 2221 if (SEQ_GT(th->th_ack, tp->snd_max)) { 2222 TCPSTAT_INC(tcps_rcvacktoomuch); 2223 goto dropafterack; 2224 } 2225 if ((tp->t_flags & TF_SACK_PERMIT) && 2226 ((to.to_flags & TOF_SACK) || 2227 !TAILQ_EMPTY(&tp->snd_holes))) 2228 tcp_sack_doack(tp, &to, th->th_ack); 2229 2230 /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 2231 hhook_run_tcp_est_in(tp, th, &to); 2232 2233 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2234 if (tlen == 0 && tiwin == tp->snd_wnd) { 2235 TCPSTAT_INC(tcps_rcvdupack); 2236 /* 2237 * If we have outstanding data (other than 2238 * a window probe), this is a completely 2239 * duplicate ack (ie, window info didn't 2240 * change), the ack is the biggest we've 2241 * seen and we've seen exactly our rexmt 2242 * threshhold of them, assume a packet 2243 * has been dropped and retransmit it. 2244 * Kludge snd_nxt & the congestion 2245 * window so we send only this one 2246 * packet. 2247 * 2248 * We know we're losing at the current 2249 * window size so do congestion avoidance 2250 * (set ssthresh to half the current window 2251 * and pull our congestion window back to 2252 * the new ssthresh). 2253 * 2254 * Dup acks mean that packets have left the 2255 * network (they're now cached at the receiver) 2256 * so bump cwnd by the amount in the receiver 2257 * to keep a constant cwnd packets in the 2258 * network. 2259 * 2260 * When using TCP ECN, notify the peer that 2261 * we reduced the cwnd. 2262 */ 2263 if (!tcp_timer_active(tp, TT_REXMT) || 2264 th->th_ack != tp->snd_una) 2265 tp->t_dupacks = 0; 2266 else if (++tp->t_dupacks > tcprexmtthresh || 2267 IN_FASTRECOVERY(tp->t_flags)) { 2268 cc_ack_received(tp, th, CC_DUPACK); 2269 if ((tp->t_flags & TF_SACK_PERMIT) && 2270 IN_FASTRECOVERY(tp->t_flags)) { 2271 int awnd; 2272 2273 /* 2274 * Compute the amount of data in flight first. 2275 * We can inject new data into the pipe iff 2276 * we have less than 1/2 the original window's 2277 * worth of data in flight. 2278 */ 2279 awnd = (tp->snd_nxt - tp->snd_fack) + 2280 tp->sackhint.sack_bytes_rexmit; 2281 if (awnd < tp->snd_ssthresh) { 2282 tp->snd_cwnd += tp->t_maxseg; 2283 if (tp->snd_cwnd > tp->snd_ssthresh) 2284 tp->snd_cwnd = tp->snd_ssthresh; 2285 } 2286 } else 2287 tp->snd_cwnd += tp->t_maxseg; 2288 (void) tcp_output(tp); 2289 goto drop; 2290 } else if (tp->t_dupacks == tcprexmtthresh) { 2291 tcp_seq onxt = tp->snd_nxt; 2292 2293 /* 2294 * If we're doing sack, check to 2295 * see if we're already in sack 2296 * recovery. If we're not doing sack, 2297 * check to see if we're in newreno 2298 * recovery. 2299 */ 2300 if (tp->t_flags & TF_SACK_PERMIT) { 2301 if (IN_FASTRECOVERY(tp->t_flags)) { 2302 tp->t_dupacks = 0; 2303 break; 2304 } 2305 } else { 2306 if (SEQ_LEQ(th->th_ack, 2307 tp->snd_recover)) { 2308 tp->t_dupacks = 0; 2309 break; 2310 } 2311 } 2312 /* Congestion signal before ack. */ 2313 cc_cong_signal(tp, th, CC_NDUPACK); 2314 cc_ack_received(tp, th, CC_DUPACK); 2315 tcp_timer_activate(tp, TT_REXMT, 0); 2316 tp->t_rtttime = 0; 2317 if (tp->t_flags & TF_SACK_PERMIT) { 2318 TCPSTAT_INC( 2319 tcps_sack_recovery_episode); 2320 tp->sack_newdata = tp->snd_nxt; 2321 tp->snd_cwnd = tp->t_maxseg; 2322 (void) tcp_output(tp); 2323 goto drop; 2324 } 2325 tp->snd_nxt = th->th_ack; 2326 tp->snd_cwnd = tp->t_maxseg; 2327 (void) tcp_output(tp); 2328 KASSERT(tp->snd_limited <= 2, 2329 ("%s: tp->snd_limited too big", 2330 __func__)); 2331 tp->snd_cwnd = tp->snd_ssthresh + 2332 tp->t_maxseg * 2333 (tp->t_dupacks - tp->snd_limited); 2334 if (SEQ_GT(onxt, tp->snd_nxt)) 2335 tp->snd_nxt = onxt; 2336 goto drop; 2337 } else if (V_tcp_do_rfc3042) { 2338 cc_ack_received(tp, th, CC_DUPACK); 2339 u_long oldcwnd = tp->snd_cwnd; 2340 tcp_seq oldsndmax = tp->snd_max; 2341 u_int sent; 2342 2343 KASSERT(tp->t_dupacks == 1 || 2344 tp->t_dupacks == 2, 2345 ("%s: dupacks not 1 or 2", 2346 __func__)); 2347 if (tp->t_dupacks == 1) 2348 tp->snd_limited = 0; 2349 tp->snd_cwnd = 2350 (tp->snd_nxt - tp->snd_una) + 2351 (tp->t_dupacks - tp->snd_limited) * 2352 tp->t_maxseg; 2353 (void) tcp_output(tp); 2354 sent = tp->snd_max - oldsndmax; 2355 if (sent > tp->t_maxseg) { 2356 KASSERT((tp->t_dupacks == 2 && 2357 tp->snd_limited == 0) || 2358 (sent == tp->t_maxseg + 1 && 2359 tp->t_flags & TF_SENTFIN), 2360 ("%s: sent too much", 2361 __func__)); 2362 tp->snd_limited = 2; 2363 } else if (sent > 0) 2364 ++tp->snd_limited; 2365 tp->snd_cwnd = oldcwnd; 2366 goto drop; 2367 } 2368 } else 2369 tp->t_dupacks = 0; 2370 break; 2371 } 2372 2373 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2374 ("%s: th_ack <= snd_una", __func__)); 2375 2376 /* 2377 * If the congestion window was inflated to account 2378 * for the other side's cached packets, retract it. 2379 */ 2380 if (IN_FASTRECOVERY(tp->t_flags)) { 2381 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 2382 if (tp->t_flags & TF_SACK_PERMIT) 2383 tcp_sack_partialack(tp, th); 2384 else 2385 tcp_newreno_partial_ack(tp, th); 2386 } else 2387 cc_post_recovery(tp, th); 2388 } 2389 tp->t_dupacks = 0; 2390 /* 2391 * If we reach this point, ACK is not a duplicate, 2392 * i.e., it ACKs something we sent. 2393 */ 2394 if (tp->t_flags & TF_NEEDSYN) { 2395 /* 2396 * T/TCP: Connection was half-synchronized, and our 2397 * SYN has been ACK'd (so connection is now fully 2398 * synchronized). Go to non-starred state, 2399 * increment snd_una for ACK of SYN, and check if 2400 * we can do window scaling. 2401 */ 2402 tp->t_flags &= ~TF_NEEDSYN; 2403 tp->snd_una++; 2404 /* Do window scaling? */ 2405 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2406 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2407 tp->rcv_scale = tp->request_r_scale; 2408 /* Send window already scaled. */ 2409 } 2410 } 2411 2412process_ACK: 2413 INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2414 KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2415 ("tcp_input: process_ACK ti_locked %d", ti_locked)); 2416 INP_WLOCK_ASSERT(tp->t_inpcb); 2417 2418 acked = BYTES_THIS_ACK(tp, th); 2419 TCPSTAT_INC(tcps_rcvackpack); 2420 TCPSTAT_ADD(tcps_rcvackbyte, acked); 2421 2422 /* 2423 * If we just performed our first retransmit, and the ACK 2424 * arrives within our recovery window, then it was a mistake 2425 * to do the retransmit in the first place. Recover our 2426 * original cwnd and ssthresh, and proceed to transmit where 2427 * we left off. 2428 */ 2429 if (tp->t_rxtshift == 1 && (int)(ticks - tp->t_badrxtwin) < 0) 2430 cc_cong_signal(tp, th, CC_RTO_ERR); 2431 2432 /* 2433 * If we have a timestamp reply, update smoothed 2434 * round trip time. If no timestamp is present but 2435 * transmit timer is running and timed sequence 2436 * number was acked, update smoothed round trip time. 2437 * Since we now have an rtt measurement, cancel the 2438 * timer backoff (cf., Phil Karn's retransmit alg.). 2439 * Recompute the initial retransmit timer. 2440 * 2441 * Some boxes send broken timestamp replies 2442 * during the SYN+ACK phase, ignore 2443 * timestamps of 0 or we could calculate a 2444 * huge RTT and blow up the retransmit timer. 2445 */ 2446 if ((to.to_flags & TOF_TS) != 0 && 2447 to.to_tsecr) { 2448 if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 2449 tp->t_rttlow = ticks - to.to_tsecr; 2450 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2451 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2452 if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2453 tp->t_rttlow = ticks - tp->t_rtttime; 2454 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2455 } 2456 2457 /* 2458 * If all outstanding data is acked, stop retransmit 2459 * timer and remember to restart (more output or persist). 2460 * If there is more data to be acked, restart retransmit 2461 * timer, using current (possibly backed-off) value. 2462 */ 2463 if (th->th_ack == tp->snd_max) { 2464 tcp_timer_activate(tp, TT_REXMT, 0); 2465 needoutput = 1; 2466 } else if (!tcp_timer_active(tp, TT_PERSIST)) 2467 tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2468 2469 /* 2470 * If no data (only SYN) was ACK'd, 2471 * skip rest of ACK processing. 2472 */ 2473 if (acked == 0) 2474 goto step6; 2475 2476 /* 2477 * Let the congestion control algorithm update congestion 2478 * control related information. This typically means increasing 2479 * the congestion window. 2480 */ 2481 cc_ack_received(tp, th, CC_ACK); 2482 2483 SOCKBUF_LOCK(&so->so_snd); 2484 if (acked > so->so_snd.sb_cc) { 2485 tp->snd_wnd -= so->so_snd.sb_cc; 2486 sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2487 ourfinisacked = 1; 2488 } else { 2489 sbdrop_locked(&so->so_snd, acked); 2490 tp->snd_wnd -= acked; 2491 ourfinisacked = 0; 2492 } 2493 /* NB: sowwakeup_locked() does an implicit unlock. */ 2494 sowwakeup_locked(so); 2495 /* Detect una wraparound. */ 2496 if (!IN_RECOVERY(tp->t_flags) && 2497 SEQ_GT(tp->snd_una, tp->snd_recover) && 2498 SEQ_LEQ(th->th_ack, tp->snd_recover)) 2499 tp->snd_recover = th->th_ack - 1; 2500 /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2501 if (IN_RECOVERY(tp->t_flags) && 2502 SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2503 EXIT_RECOVERY(tp->t_flags); 2504 } 2505 tp->snd_una = th->th_ack; 2506 if (tp->t_flags & TF_SACK_PERMIT) { 2507 if (SEQ_GT(tp->snd_una, tp->snd_recover)) 2508 tp->snd_recover = tp->snd_una; 2509 } 2510 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2511 tp->snd_nxt = tp->snd_una; 2512 2513 switch (tp->t_state) { 2514 2515 /* 2516 * In FIN_WAIT_1 STATE in addition to the processing 2517 * for the ESTABLISHED state if our FIN is now acknowledged 2518 * then enter FIN_WAIT_2. 2519 */ 2520 case TCPS_FIN_WAIT_1: 2521 if (ourfinisacked) { 2522 /* 2523 * If we can't receive any more 2524 * data, then closing user can proceed. 2525 * Starting the timer is contrary to the 2526 * specification, but if we don't get a FIN 2527 * we'll hang forever. 2528 * 2529 * XXXjl: 2530 * we should release the tp also, and use a 2531 * compressed state. 2532 */ 2533 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 2534 int timeout; 2535 2536 soisdisconnected(so); 2537 timeout = (tcp_fast_finwait2_recycle) ? 2538 tcp_finwait2_timeout : tcp_maxidle; 2539 tcp_timer_activate(tp, TT_2MSL, timeout); 2540 } 2541 tp->t_state = TCPS_FIN_WAIT_2; 2542 } 2543 break; 2544 2545 /* 2546 * In CLOSING STATE in addition to the processing for 2547 * the ESTABLISHED state if the ACK acknowledges our FIN 2548 * then enter the TIME-WAIT state, otherwise ignore 2549 * the segment. 2550 */ 2551 case TCPS_CLOSING: 2552 if (ourfinisacked) { 2553 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2554 tcp_twstart(tp); 2555 INP_INFO_WUNLOCK(&V_tcbinfo); 2556 m_freem(m); 2557 return; 2558 } 2559 break; 2560 2561 /* 2562 * In LAST_ACK, we may still be waiting for data to drain 2563 * and/or to be acked, as well as for the ack of our FIN. 2564 * If our FIN is now acknowledged, delete the TCB, 2565 * enter the closed state and return. 2566 */ 2567 case TCPS_LAST_ACK: 2568 if (ourfinisacked) { 2569 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2570 tp = tcp_close(tp); 2571 goto drop; 2572 } 2573 break; 2574 } 2575 } 2576 2577step6: 2578 INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2579 KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2580 ("tcp_do_segment: step6 ti_locked %d", ti_locked)); 2581 INP_WLOCK_ASSERT(tp->t_inpcb); 2582 2583 /* 2584 * Update window information. 2585 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2586 */ 2587 if ((thflags & TH_ACK) && 2588 (SEQ_LT(tp->snd_wl1, th->th_seq) || 2589 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2590 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2591 /* keep track of pure window updates */ 2592 if (tlen == 0 && 2593 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2594 TCPSTAT_INC(tcps_rcvwinupd); 2595 tp->snd_wnd = tiwin; 2596 tp->snd_wl1 = th->th_seq; 2597 tp->snd_wl2 = th->th_ack; 2598 if (tp->snd_wnd > tp->max_sndwnd) 2599 tp->max_sndwnd = tp->snd_wnd; 2600 needoutput = 1; 2601 } 2602 2603 /* 2604 * Process segments with URG. 2605 */ 2606 if ((thflags & TH_URG) && th->th_urp && 2607 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2608 /* 2609 * This is a kludge, but if we receive and accept 2610 * random urgent pointers, we'll crash in 2611 * soreceive. It's hard to imagine someone 2612 * actually wanting to send this much urgent data. 2613 */ 2614 SOCKBUF_LOCK(&so->so_rcv); 2615 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2616 th->th_urp = 0; /* XXX */ 2617 thflags &= ~TH_URG; /* XXX */ 2618 SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2619 goto dodata; /* XXX */ 2620 } 2621 /* 2622 * If this segment advances the known urgent pointer, 2623 * then mark the data stream. This should not happen 2624 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2625 * a FIN has been received from the remote side. 2626 * In these states we ignore the URG. 2627 * 2628 * According to RFC961 (Assigned Protocols), 2629 * the urgent pointer points to the last octet 2630 * of urgent data. We continue, however, 2631 * to consider it to indicate the first octet 2632 * of data past the urgent section as the original 2633 * spec states (in one of two places). 2634 */ 2635 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2636 tp->rcv_up = th->th_seq + th->th_urp; 2637 so->so_oobmark = so->so_rcv.sb_cc + 2638 (tp->rcv_up - tp->rcv_nxt) - 1; 2639 if (so->so_oobmark == 0) 2640 so->so_rcv.sb_state |= SBS_RCVATMARK; 2641 sohasoutofband(so); 2642 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2643 } 2644 SOCKBUF_UNLOCK(&so->so_rcv); 2645 /* 2646 * Remove out of band data so doesn't get presented to user. 2647 * This can happen independent of advancing the URG pointer, 2648 * but if two URG's are pending at once, some out-of-band 2649 * data may creep in... ick. 2650 */ 2651 if (th->th_urp <= (u_long)tlen && 2652 !(so->so_options & SO_OOBINLINE)) { 2653 /* hdr drop is delayed */ 2654 tcp_pulloutofband(so, th, m, drop_hdrlen); 2655 } 2656 } else { 2657 /* 2658 * If no out of band data is expected, 2659 * pull receive urgent pointer along 2660 * with the receive window. 2661 */ 2662 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2663 tp->rcv_up = tp->rcv_nxt; 2664 } 2665dodata: /* XXX */ 2666 INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2667 KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2668 ("tcp_do_segment: dodata ti_locked %d", ti_locked)); 2669 INP_WLOCK_ASSERT(tp->t_inpcb); 2670 2671 /* 2672 * Process the segment text, merging it into the TCP sequencing queue, 2673 * and arranging for acknowledgment of receipt if necessary. 2674 * This process logically involves adjusting tp->rcv_wnd as data 2675 * is presented to the user (this happens in tcp_usrreq.c, 2676 * case PRU_RCVD). If a FIN has already been received on this 2677 * connection then we just ignore the text. 2678 */ 2679 if ((tlen || (thflags & TH_FIN)) && 2680 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2681 tcp_seq save_start = th->th_seq; 2682 m_adj(m, drop_hdrlen); /* delayed header drop */ 2683 /* 2684 * Insert segment which includes th into TCP reassembly queue 2685 * with control block tp. Set thflags to whether reassembly now 2686 * includes a segment with FIN. This handles the common case 2687 * inline (segment is the next to be received on an established 2688 * connection, and the queue is empty), avoiding linkage into 2689 * and removal from the queue and repetition of various 2690 * conversions. 2691 * Set DELACK for segments received in order, but ack 2692 * immediately when segments are out of order (so 2693 * fast retransmit can work). 2694 */ 2695 if (th->th_seq == tp->rcv_nxt && 2696 LIST_EMPTY(&tp->t_segq) && 2697 TCPS_HAVEESTABLISHED(tp->t_state)) { 2698 if (DELAY_ACK(tp)) 2699 tp->t_flags |= TF_DELACK; 2700 else 2701 tp->t_flags |= TF_ACKNOW; 2702 tp->rcv_nxt += tlen; 2703 thflags = th->th_flags & TH_FIN; 2704 TCPSTAT_INC(tcps_rcvpack); 2705 TCPSTAT_ADD(tcps_rcvbyte, tlen); 2706 ND6_HINT(tp); 2707 SOCKBUF_LOCK(&so->so_rcv); 2708 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2709 m_freem(m); 2710 else 2711 sbappendstream_locked(&so->so_rcv, m); 2712 /* NB: sorwakeup_locked() does an implicit unlock. */ 2713 sorwakeup_locked(so); 2714 } else { 2715 /* 2716 * XXX: Due to the header drop above "th" is 2717 * theoretically invalid by now. Fortunately 2718 * m_adj() doesn't actually frees any mbufs 2719 * when trimming from the head. 2720 */ 2721 thflags = tcp_reass(tp, th, &tlen, m); 2722 tp->t_flags |= TF_ACKNOW; 2723 } 2724 if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2725 tcp_update_sack_list(tp, save_start, save_start + tlen); 2726#if 0 2727 /* 2728 * Note the amount of data that peer has sent into 2729 * our window, in order to estimate the sender's 2730 * buffer size. 2731 * XXX: Unused. 2732 */ 2733 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2734#endif 2735 } else { 2736 m_freem(m); 2737 thflags &= ~TH_FIN; 2738 } 2739 2740 /* 2741 * If FIN is received ACK the FIN and let the user know 2742 * that the connection is closing. 2743 */ 2744 if (thflags & TH_FIN) { 2745 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2746 socantrcvmore(so); 2747 /* 2748 * If connection is half-synchronized 2749 * (ie NEEDSYN flag on) then delay ACK, 2750 * so it may be piggybacked when SYN is sent. 2751 * Otherwise, since we received a FIN then no 2752 * more input can be expected, send ACK now. 2753 */ 2754 if (tp->t_flags & TF_NEEDSYN) 2755 tp->t_flags |= TF_DELACK; 2756 else 2757 tp->t_flags |= TF_ACKNOW; 2758 tp->rcv_nxt++; 2759 } 2760 switch (tp->t_state) { 2761 2762 /* 2763 * In SYN_RECEIVED and ESTABLISHED STATES 2764 * enter the CLOSE_WAIT state. 2765 */ 2766 case TCPS_SYN_RECEIVED: 2767 tp->t_starttime = ticks; 2768 /* FALLTHROUGH */ 2769 case TCPS_ESTABLISHED: 2770 tp->t_state = TCPS_CLOSE_WAIT; 2771 break; 2772 2773 /* 2774 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2775 * enter the CLOSING state. 2776 */ 2777 case TCPS_FIN_WAIT_1: 2778 tp->t_state = TCPS_CLOSING; 2779 break; 2780 2781 /* 2782 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2783 * starting the time-wait timer, turning off the other 2784 * standard timers. 2785 */ 2786 case TCPS_FIN_WAIT_2: 2787 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2788 KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 2789 "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 2790 ti_locked)); 2791 2792 tcp_twstart(tp); 2793 INP_INFO_WUNLOCK(&V_tcbinfo); 2794 return; 2795 } 2796 } 2797 if (ti_locked == TI_RLOCKED) 2798 INP_INFO_RUNLOCK(&V_tcbinfo); 2799 else if (ti_locked == TI_WLOCKED) 2800 INP_INFO_WUNLOCK(&V_tcbinfo); 2801 else 2802 panic("%s: dodata epilogue ti_locked %d", __func__, 2803 ti_locked); 2804 ti_locked = TI_UNLOCKED; 2805 2806#ifdef TCPDEBUG 2807 if (so->so_options & SO_DEBUG) 2808 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2809 &tcp_savetcp, 0); 2810#endif 2811 2812 /* 2813 * Return any desired output. 2814 */ 2815 if (needoutput || (tp->t_flags & TF_ACKNOW)) 2816 (void) tcp_output(tp); 2817 2818check_delack: 2819 KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2820 __func__, ti_locked)); 2821 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2822 INP_WLOCK_ASSERT(tp->t_inpcb); 2823 2824 if (tp->t_flags & TF_DELACK) { 2825 tp->t_flags &= ~TF_DELACK; 2826 tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 2827 } 2828 INP_WUNLOCK(tp->t_inpcb); 2829 return; 2830 2831dropafterack: 2832 KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2833 ("tcp_do_segment: dropafterack ti_locked %d", ti_locked)); 2834 2835 /* 2836 * Generate an ACK dropping incoming segment if it occupies 2837 * sequence space, where the ACK reflects our state. 2838 * 2839 * We can now skip the test for the RST flag since all 2840 * paths to this code happen after packets containing 2841 * RST have been dropped. 2842 * 2843 * In the SYN-RECEIVED state, don't send an ACK unless the 2844 * segment we received passes the SYN-RECEIVED ACK test. 2845 * If it fails send a RST. This breaks the loop in the 2846 * "LAND" DoS attack, and also prevents an ACK storm 2847 * between two listening ports that have been sent forged 2848 * SYN segments, each with the source address of the other. 2849 */ 2850 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2851 (SEQ_GT(tp->snd_una, th->th_ack) || 2852 SEQ_GT(th->th_ack, tp->snd_max)) ) { 2853 rstreason = BANDLIM_RST_OPENPORT; 2854 goto dropwithreset; 2855 } 2856#ifdef TCPDEBUG 2857 if (so->so_options & SO_DEBUG) 2858 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2859 &tcp_savetcp, 0); 2860#endif 2861 if (ti_locked == TI_RLOCKED) 2862 INP_INFO_RUNLOCK(&V_tcbinfo); 2863 else if (ti_locked == TI_WLOCKED) 2864 INP_INFO_WUNLOCK(&V_tcbinfo); 2865 else 2866 panic("%s: dropafterack epilogue ti_locked %d", __func__, 2867 ti_locked); 2868 ti_locked = TI_UNLOCKED; 2869 2870 tp->t_flags |= TF_ACKNOW; 2871 (void) tcp_output(tp); 2872 INP_WUNLOCK(tp->t_inpcb); 2873 m_freem(m); 2874 return; 2875 2876dropwithreset: 2877 if (ti_locked == TI_RLOCKED) 2878 INP_INFO_RUNLOCK(&V_tcbinfo); 2879 else if (ti_locked == TI_WLOCKED) 2880 INP_INFO_WUNLOCK(&V_tcbinfo); 2881 else 2882 panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 2883 ti_locked = TI_UNLOCKED; 2884 2885 if (tp != NULL) { 2886 tcp_dropwithreset(m, th, tp, tlen, rstreason); 2887 INP_WUNLOCK(tp->t_inpcb); 2888 } else 2889 tcp_dropwithreset(m, th, NULL, tlen, rstreason); 2890 return; 2891 2892drop: 2893 if (ti_locked == TI_RLOCKED) 2894 INP_INFO_RUNLOCK(&V_tcbinfo); 2895 else if (ti_locked == TI_WLOCKED) 2896 INP_INFO_WUNLOCK(&V_tcbinfo); 2897#ifdef INVARIANTS 2898 else 2899 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2900#endif 2901 ti_locked = TI_UNLOCKED; 2902 2903 /* 2904 * Drop space held by incoming segment and return. 2905 */ 2906#ifdef TCPDEBUG 2907 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2908 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2909 &tcp_savetcp, 0); 2910#endif 2911 if (tp != NULL) 2912 INP_WUNLOCK(tp->t_inpcb); 2913 m_freem(m); 2914} 2915 2916/* 2917 * Issue RST and make ACK acceptable to originator of segment. 2918 * The mbuf must still include the original packet header. 2919 * tp may be NULL. 2920 */ 2921static void 2922tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2923 int tlen, int rstreason) 2924{ 2925 struct ip *ip; 2926#ifdef INET6 2927 struct ip6_hdr *ip6; 2928#endif 2929 2930 if (tp != NULL) { 2931 INP_WLOCK_ASSERT(tp->t_inpcb); 2932 } 2933 2934 /* Don't bother if destination was broadcast/multicast. */ 2935 if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2936 goto drop; 2937#ifdef INET6 2938 if (mtod(m, struct ip *)->ip_v == 6) { 2939 ip6 = mtod(m, struct ip6_hdr *); 2940 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2941 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2942 goto drop; 2943 /* IPv6 anycast check is done at tcp6_input() */ 2944 } else 2945#endif 2946 { 2947 ip = mtod(m, struct ip *); 2948 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2949 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2950 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2951 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2952 goto drop; 2953 } 2954 2955 /* Perform bandwidth limiting. */ 2956 if (badport_bandlim(rstreason) < 0) 2957 goto drop; 2958 2959 /* tcp_respond consumes the mbuf chain. */ 2960 if (th->th_flags & TH_ACK) { 2961 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2962 th->th_ack, TH_RST); 2963 } else { 2964 if (th->th_flags & TH_SYN) 2965 tlen++; 2966 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2967 (tcp_seq)0, TH_RST|TH_ACK); 2968 } 2969 return; 2970drop: 2971 m_freem(m); 2972} 2973 2974/* 2975 * Parse TCP options and place in tcpopt. 2976 */ 2977static void 2978tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2979{ 2980 int opt, optlen; 2981 2982 to->to_flags = 0; 2983 for (; cnt > 0; cnt -= optlen, cp += optlen) { 2984 opt = cp[0]; 2985 if (opt == TCPOPT_EOL) 2986 break; 2987 if (opt == TCPOPT_NOP) 2988 optlen = 1; 2989 else { 2990 if (cnt < 2) 2991 break; 2992 optlen = cp[1]; 2993 if (optlen < 2 || optlen > cnt) 2994 break; 2995 } 2996 switch (opt) { 2997 case TCPOPT_MAXSEG: 2998 if (optlen != TCPOLEN_MAXSEG) 2999 continue; 3000 if (!(flags & TO_SYN)) 3001 continue; 3002 to->to_flags |= TOF_MSS; 3003 bcopy((char *)cp + 2, 3004 (char *)&to->to_mss, sizeof(to->to_mss)); 3005 to->to_mss = ntohs(to->to_mss); 3006 break; 3007 case TCPOPT_WINDOW: 3008 if (optlen != TCPOLEN_WINDOW) 3009 continue; 3010 if (!(flags & TO_SYN)) 3011 continue; 3012 to->to_flags |= TOF_SCALE; 3013 to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3014 break; 3015 case TCPOPT_TIMESTAMP: 3016 if (optlen != TCPOLEN_TIMESTAMP) 3017 continue; 3018 to->to_flags |= TOF_TS; 3019 bcopy((char *)cp + 2, 3020 (char *)&to->to_tsval, sizeof(to->to_tsval)); 3021 to->to_tsval = ntohl(to->to_tsval); 3022 bcopy((char *)cp + 6, 3023 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3024 to->to_tsecr = ntohl(to->to_tsecr); 3025 break; 3026#ifdef TCP_SIGNATURE 3027 /* 3028 * XXX In order to reply to a host which has set the 3029 * TCP_SIGNATURE option in its initial SYN, we have to 3030 * record the fact that the option was observed here 3031 * for the syncache code to perform the correct response. 3032 */ 3033 case TCPOPT_SIGNATURE: 3034 if (optlen != TCPOLEN_SIGNATURE) 3035 continue; 3036 to->to_flags |= TOF_SIGNATURE; 3037 to->to_signature = cp + 2; 3038 break; 3039#endif 3040 case TCPOPT_SACK_PERMITTED: 3041 if (optlen != TCPOLEN_SACK_PERMITTED) 3042 continue; 3043 if (!(flags & TO_SYN)) 3044 continue; 3045 if (!V_tcp_do_sack) 3046 continue; 3047 to->to_flags |= TOF_SACKPERM; 3048 break; 3049 case TCPOPT_SACK: 3050 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 3051 continue; 3052 if (flags & TO_SYN) 3053 continue; 3054 to->to_flags |= TOF_SACK; 3055 to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 3056 to->to_sacks = cp + 2; 3057 TCPSTAT_INC(tcps_sack_rcv_blocks); 3058 break; 3059 default: 3060 continue; 3061 } 3062 } 3063} 3064 3065/* 3066 * Pull out of band byte out of a segment so 3067 * it doesn't appear in the user's data queue. 3068 * It is still reflected in the segment length for 3069 * sequencing purposes. 3070 */ 3071static void 3072tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3073 int off) 3074{ 3075 int cnt = off + th->th_urp - 1; 3076 3077 while (cnt >= 0) { 3078 if (m->m_len > cnt) { 3079 char *cp = mtod(m, caddr_t) + cnt; 3080 struct tcpcb *tp = sototcpcb(so); 3081 3082 INP_WLOCK_ASSERT(tp->t_inpcb); 3083 3084 tp->t_iobc = *cp; 3085 tp->t_oobflags |= TCPOOB_HAVEDATA; 3086 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3087 m->m_len--; 3088 if (m->m_flags & M_PKTHDR) 3089 m->m_pkthdr.len--; 3090 return; 3091 } 3092 cnt -= m->m_len; 3093 m = m->m_next; 3094 if (m == NULL) 3095 break; 3096 } 3097 panic("tcp_pulloutofband"); 3098} 3099 3100/* 3101 * Collect new round-trip time estimate 3102 * and update averages and current timeout. 3103 */ 3104static void 3105tcp_xmit_timer(struct tcpcb *tp, int rtt) 3106{ 3107 int delta; 3108 3109 INP_WLOCK_ASSERT(tp->t_inpcb); 3110 3111 TCPSTAT_INC(tcps_rttupdated); 3112 tp->t_rttupdated++; 3113 if (tp->t_srtt != 0) { 3114 /* 3115 * srtt is stored as fixed point with 5 bits after the 3116 * binary point (i.e., scaled by 8). The following magic 3117 * is equivalent to the smoothing algorithm in rfc793 with 3118 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3119 * point). Adjust rtt to origin 0. 3120 */ 3121 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3122 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3123 3124 if ((tp->t_srtt += delta) <= 0) 3125 tp->t_srtt = 1; 3126 3127 /* 3128 * We accumulate a smoothed rtt variance (actually, a 3129 * smoothed mean difference), then set the retransmit 3130 * timer to smoothed rtt + 4 times the smoothed variance. 3131 * rttvar is stored as fixed point with 4 bits after the 3132 * binary point (scaled by 16). The following is 3133 * equivalent to rfc793 smoothing with an alpha of .75 3134 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3135 * rfc793's wired-in beta. 3136 */ 3137 if (delta < 0) 3138 delta = -delta; 3139 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3140 if ((tp->t_rttvar += delta) <= 0) 3141 tp->t_rttvar = 1; 3142 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 3143 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3144 } else { 3145 /* 3146 * No rtt measurement yet - use the unsmoothed rtt. 3147 * Set the variance to half the rtt (so our first 3148 * retransmit happens at 3*rtt). 3149 */ 3150 tp->t_srtt = rtt << TCP_RTT_SHIFT; 3151 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 3152 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3153 } 3154 tp->t_rtttime = 0; 3155 tp->t_rxtshift = 0; 3156 3157 /* 3158 * the retransmit should happen at rtt + 4 * rttvar. 3159 * Because of the way we do the smoothing, srtt and rttvar 3160 * will each average +1/2 tick of bias. When we compute 3161 * the retransmit timer, we want 1/2 tick of rounding and 3162 * 1 extra tick because of +-1/2 tick uncertainty in the 3163 * firing of the timer. The bias will give us exactly the 3164 * 1.5 tick we need. But, because the bias is 3165 * statistical, we have to test that we don't drop below 3166 * the minimum feasible timer (which is 2 ticks). 3167 */ 3168 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 3169 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3170 3171 /* 3172 * We received an ack for a packet that wasn't retransmitted; 3173 * it is probably safe to discard any error indications we've 3174 * received recently. This isn't quite right, but close enough 3175 * for now (a route might have failed after we sent a segment, 3176 * and the return path might not be symmetrical). 3177 */ 3178 tp->t_softerror = 0; 3179} 3180 3181/* 3182 * Determine a reasonable value for maxseg size. 3183 * If the route is known, check route for mtu. 3184 * If none, use an mss that can be handled on the outgoing 3185 * interface without forcing IP to fragment; if bigger than 3186 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 3187 * to utilize large mbufs. If no route is found, route has no mtu, 3188 * or the destination isn't local, use a default, hopefully conservative 3189 * size (usually 512 or the default IP max size, but no more than the mtu 3190 * of the interface), as we can't discover anything about intervening 3191 * gateways or networks. We also initialize the congestion/slow start 3192 * window to be a single segment if the destination isn't local. 3193 * While looking at the routing entry, we also initialize other path-dependent 3194 * parameters from pre-set or cached values in the routing entry. 3195 * 3196 * Also take into account the space needed for options that we 3197 * send regularly. Make maxseg shorter by that amount to assure 3198 * that we can send maxseg amount of data even when the options 3199 * are present. Store the upper limit of the length of options plus 3200 * data in maxopd. 3201 * 3202 * In case of T/TCP, we call this routine during implicit connection 3203 * setup as well (offer = -1), to initialize maxseg from the cached 3204 * MSS of our peer. 3205 * 3206 * NOTE that this routine is only called when we process an incoming 3207 * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 3208 */ 3209void 3210tcp_mss_update(struct tcpcb *tp, int offer, 3211 struct hc_metrics_lite *metricptr, int *mtuflags) 3212{ 3213 int mss; 3214 u_long maxmtu; 3215 struct inpcb *inp = tp->t_inpcb; 3216 struct hc_metrics_lite metrics; 3217 int origoffer = offer; 3218#ifdef INET6 3219 int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3220 size_t min_protoh = isipv6 ? 3221 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3222 sizeof (struct tcpiphdr); 3223#else 3224 const size_t min_protoh = sizeof(struct tcpiphdr); 3225#endif 3226 3227 INP_WLOCK_ASSERT(tp->t_inpcb); 3228 3229 /* Initialize. */ 3230#ifdef INET6 3231 if (isipv6) { 3232 maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags); 3233 tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 3234 } else 3235#endif 3236 { 3237 maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags); 3238 tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3239 } 3240 3241 /* 3242 * No route to sender, stay with default mss and return. 3243 */ 3244 if (maxmtu == 0) { 3245 /* 3246 * In case we return early we need to initialize metrics 3247 * to a defined state as tcp_hc_get() would do for us 3248 * if there was no cache hit. 3249 */ 3250 if (metricptr != NULL) 3251 bzero(metricptr, sizeof(struct hc_metrics_lite)); 3252 return; 3253 } 3254 3255 /* What have we got? */ 3256 switch (offer) { 3257 case 0: 3258 /* 3259 * Offer == 0 means that there was no MSS on the SYN 3260 * segment, in this case we use tcp_mssdflt as 3261 * already assigned to t_maxopd above. 3262 */ 3263 offer = tp->t_maxopd; 3264 break; 3265 3266 case -1: 3267 /* 3268 * Offer == -1 means that we didn't receive SYN yet. 3269 */ 3270 /* FALLTHROUGH */ 3271 3272 default: 3273 /* 3274 * Prevent DoS attack with too small MSS. Round up 3275 * to at least minmss. 3276 */ 3277 offer = max(offer, V_tcp_minmss); 3278 } 3279 3280 /* 3281 * rmx information is now retrieved from tcp_hostcache. 3282 */ 3283 tcp_hc_get(&inp->inp_inc, &metrics); 3284 if (metricptr != NULL) 3285 bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 3286 3287 /* 3288 * If there's a discovered mtu int tcp hostcache, use it 3289 * else, use the link mtu. 3290 */ 3291 if (metrics.rmx_mtu) 3292 mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3293 else { 3294#ifdef INET6 3295 if (isipv6) { 3296 mss = maxmtu - min_protoh; 3297 if (!V_path_mtu_discovery && 3298 !in6_localaddr(&inp->in6p_faddr)) 3299 mss = min(mss, V_tcp_v6mssdflt); 3300 } else 3301#endif 3302 { 3303 mss = maxmtu - min_protoh; 3304 if (!V_path_mtu_discovery && 3305 !in_localaddr(inp->inp_faddr)) 3306 mss = min(mss, V_tcp_mssdflt); 3307 } 3308 /* 3309 * XXX - The above conditional (mss = maxmtu - min_protoh) 3310 * probably violates the TCP spec. 3311 * The problem is that, since we don't know the 3312 * other end's MSS, we are supposed to use a conservative 3313 * default. But, if we do that, then MTU discovery will 3314 * never actually take place, because the conservative 3315 * default is much less than the MTUs typically seen 3316 * on the Internet today. For the moment, we'll sweep 3317 * this under the carpet. 3318 * 3319 * The conservative default might not actually be a problem 3320 * if the only case this occurs is when sending an initial 3321 * SYN with options and data to a host we've never talked 3322 * to before. Then, they will reply with an MSS value which 3323 * will get recorded and the new parameters should get 3324 * recomputed. For Further Study. 3325 */ 3326 } 3327 mss = min(mss, offer); 3328 3329 /* 3330 * Sanity check: make sure that maxopd will be large 3331 * enough to allow some data on segments even if the 3332 * all the option space is used (40bytes). Otherwise 3333 * funny things may happen in tcp_output. 3334 */ 3335 mss = max(mss, 64); 3336 3337 /* 3338 * maxopd stores the maximum length of data AND options 3339 * in a segment; maxseg is the amount of data in a normal 3340 * segment. We need to store this value (maxopd) apart 3341 * from maxseg, because now every segment carries options 3342 * and thus we normally have somewhat less data in segments. 3343 */ 3344 tp->t_maxopd = mss; 3345 3346 /* 3347 * origoffer==-1 indicates that no segments were received yet. 3348 * In this case we just guess. 3349 */ 3350 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3351 (origoffer == -1 || 3352 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3353 mss -= TCPOLEN_TSTAMP_APPA; 3354 3355#if (MCLBYTES & (MCLBYTES - 1)) == 0 3356 if (mss > MCLBYTES) 3357 mss &= ~(MCLBYTES-1); 3358#else 3359 if (mss > MCLBYTES) 3360 mss = mss / MCLBYTES * MCLBYTES; 3361#endif 3362 tp->t_maxseg = mss; 3363} 3364 3365void 3366tcp_mss(struct tcpcb *tp, int offer) 3367{ 3368 int mss; 3369 u_long bufsize; 3370 struct inpcb *inp; 3371 struct socket *so; 3372 struct hc_metrics_lite metrics; 3373 int mtuflags = 0; 3374 3375 KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 3376 3377 tcp_mss_update(tp, offer, &metrics, &mtuflags); 3378 3379 mss = tp->t_maxseg; 3380 inp = tp->t_inpcb; 3381 3382 /* 3383 * If there's a pipesize, change the socket buffer to that size, 3384 * don't change if sb_hiwat is different than default (then it 3385 * has been changed on purpose with setsockopt). 3386 * Make the socket buffers an integral number of mss units; 3387 * if the mss is larger than the socket buffer, decrease the mss. 3388 */ 3389 so = inp->inp_socket; 3390 SOCKBUF_LOCK(&so->so_snd); 3391 if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 3392 bufsize = metrics.rmx_sendpipe; 3393 else 3394 bufsize = so->so_snd.sb_hiwat; 3395 if (bufsize < mss) 3396 mss = bufsize; 3397 else { 3398 bufsize = roundup(bufsize, mss); 3399 if (bufsize > sb_max) 3400 bufsize = sb_max; 3401 if (bufsize > so->so_snd.sb_hiwat) 3402 (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3403 } 3404 SOCKBUF_UNLOCK(&so->so_snd); 3405 tp->t_maxseg = mss; 3406 3407 SOCKBUF_LOCK(&so->so_rcv); 3408 if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 3409 bufsize = metrics.rmx_recvpipe; 3410 else 3411 bufsize = so->so_rcv.sb_hiwat; 3412 if (bufsize > mss) { 3413 bufsize = roundup(bufsize, mss); 3414 if (bufsize > sb_max) 3415 bufsize = sb_max; 3416 if (bufsize > so->so_rcv.sb_hiwat) 3417 (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3418 } 3419 SOCKBUF_UNLOCK(&so->so_rcv); 3420 3421 /* Check the interface for TSO capabilities. */ 3422 if (mtuflags & CSUM_TSO) 3423 tp->t_flags |= TF_TSO; 3424} 3425 3426/* 3427 * Determine the MSS option to send on an outgoing SYN. 3428 */ 3429int 3430tcp_mssopt(struct in_conninfo *inc) 3431{ 3432 int mss = 0; 3433 u_long maxmtu = 0; 3434 u_long thcmtu = 0; 3435 size_t min_protoh; 3436 3437 KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3438 3439#ifdef INET6 3440 if (inc->inc_flags & INC_ISIPV6) { 3441 mss = V_tcp_v6mssdflt; 3442 maxmtu = tcp_maxmtu6(inc, NULL); 3443 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 3444 min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3445 } else 3446#endif 3447 { 3448 mss = V_tcp_mssdflt; 3449 maxmtu = tcp_maxmtu(inc, NULL); 3450 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 3451 min_protoh = sizeof(struct tcpiphdr); 3452 } 3453 if (maxmtu && thcmtu) 3454 mss = min(maxmtu, thcmtu) - min_protoh; 3455 else if (maxmtu || thcmtu) 3456 mss = max(maxmtu, thcmtu) - min_protoh; 3457 3458 return (mss); 3459} 3460 3461 3462/* 3463 * On a partial ack arrives, force the retransmission of the 3464 * next unacknowledged segment. Do not clear tp->t_dupacks. 3465 * By setting snd_nxt to ti_ack, this forces retransmission timer to 3466 * be started again. 3467 */ 3468static void 3469tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 3470{ 3471 tcp_seq onxt = tp->snd_nxt; 3472 u_long ocwnd = tp->snd_cwnd; 3473 3474 INP_WLOCK_ASSERT(tp->t_inpcb); 3475 3476 tcp_timer_activate(tp, TT_REXMT, 0); 3477 tp->t_rtttime = 0; 3478 tp->snd_nxt = th->th_ack; 3479 /* 3480 * Set snd_cwnd to one segment beyond acknowledged offset. 3481 * (tp->snd_una has not yet been updated when this function is called.) 3482 */ 3483 tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th); 3484 tp->t_flags |= TF_ACKNOW; 3485 (void) tcp_output(tp); 3486 tp->snd_cwnd = ocwnd; 3487 if (SEQ_GT(onxt, tp->snd_nxt)) 3488 tp->snd_nxt = onxt; 3489 /* 3490 * Partial window deflation. Relies on fact that tp->snd_una 3491 * not updated yet. 3492 */ 3493 if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3494 tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3495 else 3496 tp->snd_cwnd = 0; 3497 tp->snd_cwnd += tp->t_maxseg; 3498} 3499