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