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