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