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