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