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