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) == 0) 609 break; 610 case 2: 611 log(LOG_INFO, 612 "Connection attempt to TCP %s:%d " 613 "from %s:%d flags:0x%02x\n", 614 dbuf, ntohs(th->th_dport), sbuf, 615 ntohs(th->th_sport), thflags); 616 break; 617 default: 618 break; 619 } 620 } 621 if (blackhole) { 622 switch (blackhole) { 623 case 1: 624 if (thflags & TH_SYN) 625 goto drop; 626 break; 627 case 2: 628 goto drop; 629 default: 630 goto drop; 631 } 632 } 633 rstreason = BANDLIM_RST_CLOSEDPORT; 634 goto dropwithreset; 635 } 636 INP_LOCK(inp); 637 tp = intotcpcb(inp); 638 if (tp == 0) { 639 INP_UNLOCK(inp); 640 rstreason = BANDLIM_RST_CLOSEDPORT; 641 goto dropwithreset; 642 } 643 if (tp->t_state == TCPS_CLOSED) 644 goto drop; 645 646 /* Unscale the window into a 32-bit value. */ 647 if ((thflags & TH_SYN) == 0) 648 tiwin = th->th_win << tp->snd_scale; 649 else 650 tiwin = th->th_win; 651 652 so = inp->inp_socket; 653 #ifdef MAC 654 if (mac_check_socket_deliver(so, m)) 655 goto drop; 656 #endif 657 #ifdef TCPDEBUG 658 if (so->so_options & SO_DEBUG) { 659 ostate = tp->t_state; 660 if (isipv6) 661 bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 662 else 663 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 664 tcp_savetcp = *th; 665 } 666 #endif 667 if (so->so_options & SO_ACCEPTCONN) { 668 struct in_conninfo inc; 669 670 #ifdef INET6 671 inc.inc_isipv6 = isipv6; 672 #endif 673 if (isipv6) { 674 inc.inc6_faddr = ip6->ip6_src; 675 inc.inc6_laddr = ip6->ip6_dst; 676 inc.inc6_route.ro_rt = NULL; /* XXX */ 677 } else { 678 inc.inc_faddr = ip->ip_src; 679 inc.inc_laddr = ip->ip_dst; 680 inc.inc_route.ro_rt = NULL; /* XXX */ 681 } 682 inc.inc_fport = th->th_sport; 683 inc.inc_lport = th->th_dport; 684 685 /* 686 * If the state is LISTEN then ignore segment if it contains 687 * a RST. If the segment contains an ACK then it is bad and 688 * send a RST. If it does not contain a SYN then it is not 689 * interesting; drop it. 690 * 691 * If the state is SYN_RECEIVED (syncache) and seg contains 692 * an ACK, but not for our SYN/ACK, send a RST. If the seg 693 * contains a RST, check the sequence number to see if it 694 * is a valid reset segment. 695 */ 696 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 697 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 698 if (!syncache_expand(&inc, th, &so, m)) { 699 /* 700 * No syncache entry, or ACK was not 701 * for our SYN/ACK. Send a RST. 702 */ 703 tcpstat.tcps_badsyn++; 704 rstreason = BANDLIM_RST_OPENPORT; 705 goto dropwithreset; 706 } 707 if (so == NULL) { 708 /* 709 * Could not complete 3-way handshake, 710 * connection is being closed down, and 711 * syncache will free mbuf. 712 */ 713 INP_UNLOCK(inp); 714 INP_INFO_WUNLOCK(&tcbinfo); 715 return; 716 } 717 /* 718 * Socket is created in state SYN_RECEIVED. 719 * Continue processing segment. 720 */ 721 INP_UNLOCK(inp); 722 inp = sotoinpcb(so); 723 INP_LOCK(inp); 724 tp = intotcpcb(inp); 725 /* 726 * This is what would have happened in 727 * tcp_output() when the SYN,ACK was sent. 728 */ 729 tp->snd_up = tp->snd_una; 730 tp->snd_max = tp->snd_nxt = tp->iss + 1; 731 tp->last_ack_sent = tp->rcv_nxt; 732 /* 733 * XXX possible bug - it doesn't appear that tp->snd_wnd is unscaled 734 * until the _second_ ACK is received: 735 * rcv SYN (set wscale opts) --> send SYN/ACK, set snd_wnd = window. 736 * rcv ACK, calculate tiwin --> process SYN_RECEIVED, determine wscale, 737 * move to ESTAB, set snd_wnd to tiwin. 738 */ 739 tp->snd_wnd = tiwin; /* unscaled */ 740 goto after_listen; 741 } 742 if (thflags & TH_RST) { 743 syncache_chkrst(&inc, th); 744 goto drop; 745 } 746 if (thflags & TH_ACK) { 747 syncache_badack(&inc); 748 tcpstat.tcps_badsyn++; 749 rstreason = BANDLIM_RST_OPENPORT; 750 goto dropwithreset; 751 } 752 goto drop; 753 } 754 755 /* 756 * Segment's flags are (SYN) or (SYN|FIN). 757 */ 758 #ifdef INET6 759 /* 760 * If deprecated address is forbidden, 761 * we do not accept SYN to deprecated interface 762 * address to prevent any new inbound connection from 763 * getting established. 764 * When we do not accept SYN, we send a TCP RST, 765 * with deprecated source address (instead of dropping 766 * it). We compromise it as it is much better for peer 767 * to send a RST, and RST will be the final packet 768 * for the exchange. 769 * 770 * If we do not forbid deprecated addresses, we accept 771 * the SYN packet. RFC2462 does not suggest dropping 772 * SYN in this case. 773 * If we decipher RFC2462 5.5.4, it says like this: 774 * 1. use of deprecated addr with existing 775 * communication is okay - "SHOULD continue to be 776 * used" 777 * 2. use of it with new communication: 778 * (2a) "SHOULD NOT be used if alternate address 779 * with sufficient scope is available" 780 * (2b) nothing mentioned otherwise. 781 * Here we fall into (2b) case as we have no choice in 782 * our source address selection - we must obey the peer. 783 * 784 * The wording in RFC2462 is confusing, and there are 785 * multiple description text for deprecated address 786 * handling - worse, they are not exactly the same. 787 * I believe 5.5.4 is the best one, so we follow 5.5.4. 788 */ 789 if (isipv6 && !ip6_use_deprecated) { 790 struct in6_ifaddr *ia6; 791 792 if ((ia6 = ip6_getdstifaddr(m)) && 793 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 794 INP_UNLOCK(inp); 795 tp = NULL; 796 rstreason = BANDLIM_RST_OPENPORT; 797 goto dropwithreset; 798 } 799 } 800 #endif 801 /* 802 * If it is from this socket, drop it, it must be forged. 803 * Don't bother responding if the destination was a broadcast. 804 */ 805 if (th->th_dport == th->th_sport) { 806 if (isipv6) { 807 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 808 &ip6->ip6_src)) 809 goto drop; 810 } else { 811 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 812 goto drop; 813 } 814 } 815 /* 816 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 817 * 818 * Note that it is quite possible to receive unicast 819 * link-layer packets with a broadcast IP address. Use 820 * in_broadcast() to find them. 821 */ 822 if (m->m_flags & (M_BCAST|M_MCAST)) 823 goto drop; 824 if (isipv6) { 825 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 826 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 827 goto drop; 828 } else { 829 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 830 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 831 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 832 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 833 goto drop; 834 } 835 /* 836 * SYN appears to be valid; create compressed TCP state 837 * for syncache, or perform t/tcp connection. 838 */ 839 if (so->so_qlen <= so->so_qlimit) { 840 tcp_dooptions(&to, optp, optlen, 1); 841 if (!syncache_add(&inc, &to, th, &so, m)) 842 goto drop; 843 if (so == NULL) { 844 /* 845 * Entry added to syncache, mbuf used to 846 * send SYN,ACK packet. 847 */ 848 KASSERT(headlocked, ("headlocked")); 849 INP_UNLOCK(inp); 850 INP_INFO_WUNLOCK(&tcbinfo); 851 return; 852 } 853 /* 854 * Segment passed TAO tests. 855 */ 856 INP_UNLOCK(inp); 857 inp = sotoinpcb(so); 858 INP_LOCK(inp); 859 tp = intotcpcb(inp); 860 tp->snd_wnd = tiwin; 861 tp->t_starttime = ticks; 862 tp->t_state = TCPS_ESTABLISHED; 863 864 /* 865 * If there is a FIN, or if there is data and the 866 * connection is local, then delay SYN,ACK(SYN) in 867 * the hope of piggy-backing it on a response 868 * segment. Otherwise must send ACK now in case 869 * the other side is slow starting. 870 */ 871 if (DELAY_ACK(tp) && 872 ((thflags & TH_FIN) || 873 (tlen != 0 && 874 ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 875 (!isipv6 && in_localaddr(inp->inp_faddr)))))) { 876 callout_reset(tp->tt_delack, tcp_delacktime, 877 tcp_timer_delack, tp); 878 tp->t_flags |= TF_NEEDSYN; 879 } else 880 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 881 882 tcpstat.tcps_connects++; 883 soisconnected(so); 884 goto trimthenstep6; 885 } 886 goto drop; 887 } 888 after_listen: 889 890 /* XXX temp debugging */ 891 /* should not happen - syncache should pick up these connections */ 892 if (tp->t_state == TCPS_LISTEN) 893 panic("tcp_input: TCPS_LISTEN"); 894 895 /* 896 * Segment received on connection. 897 * Reset idle time and keep-alive timer. 898 */ 899 tp->t_rcvtime = ticks; 900 if (TCPS_HAVEESTABLISHED(tp->t_state)) 901 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 902 903 /* 904 * Process options. 905 * XXX this is tradtitional behavior, may need to be cleaned up. 906 */ 907 tcp_dooptions(&to, optp, optlen, thflags & TH_SYN); 908 if (thflags & TH_SYN) { 909 if (to.to_flags & TOF_SCALE) { 910 tp->t_flags |= TF_RCVD_SCALE; 911 tp->requested_s_scale = to.to_requested_s_scale; 912 } 913 if (to.to_flags & TOF_TS) { 914 tp->t_flags |= TF_RCVD_TSTMP; 915 tp->ts_recent = to.to_tsval; 916 tp->ts_recent_age = ticks; 917 } 918 if (to.to_flags & (TOF_CC|TOF_CCNEW)) 919 tp->t_flags |= TF_RCVD_CC; 920 if (to.to_flags & TOF_MSS) 921 tcp_mss(tp, to.to_mss); 922 } 923 924 /* 925 * Header prediction: check for the two common cases 926 * of a uni-directional data xfer. If the packet has 927 * no control flags, is in-sequence, the window didn't 928 * change and we're not retransmitting, it's a 929 * candidate. If the length is zero and the ack moved 930 * forward, we're the sender side of the xfer. Just 931 * free the data acked & wake any higher level process 932 * that was blocked waiting for space. If the length 933 * is non-zero and the ack didn't move, we're the 934 * receiver side. If we're getting packets in-order 935 * (the reassembly queue is empty), add the data to 936 * the socket buffer and note that we need a delayed ack. 937 * Make sure that the hidden state-flags are also off. 938 * Since we check for TCPS_ESTABLISHED above, it can only 939 * be TH_NEEDSYN. 940 */ 941 if (tp->t_state == TCPS_ESTABLISHED && 942 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 943 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 944 ((to.to_flags & TOF_TS) == 0 || 945 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 946 /* 947 * Using the CC option is compulsory if once started: 948 * the segment is OK if no T/TCP was negotiated or 949 * if the segment has a CC option equal to CCrecv 950 */ 951 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 952 ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 953 th->th_seq == tp->rcv_nxt && 954 tiwin && tiwin == tp->snd_wnd && 955 tp->snd_nxt == tp->snd_max) { 956 957 /* 958 * If last ACK falls within this segment's sequence numbers, 959 * record the timestamp. 960 * NOTE that the test is modified according to the latest 961 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 962 */ 963 if ((to.to_flags & TOF_TS) != 0 && 964 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 965 tp->ts_recent_age = ticks; 966 tp->ts_recent = to.to_tsval; 967 } 968 969 if (tlen == 0) { 970 if (SEQ_GT(th->th_ack, tp->snd_una) && 971 SEQ_LEQ(th->th_ack, tp->snd_max) && 972 tp->snd_cwnd >= tp->snd_wnd && 973 ((!tcp_do_newreno && 974 tp->t_dupacks < tcprexmtthresh) || 975 (tcp_do_newreno && 976 !SEQ_LT(tp->snd_una, tp->snd_recover)))) { 977 KASSERT(headlocked, ("headlocked")); 978 INP_INFO_WUNLOCK(&tcbinfo); 979 headlocked = 0; 980 /* 981 * this is a pure ack for outstanding data. 982 */ 983 ++tcpstat.tcps_predack; 984 /* 985 * "bad retransmit" recovery 986 */ 987 if (tp->t_rxtshift == 1 && 988 ticks < tp->t_badrxtwin) { 989 ++tcpstat.tcps_sndrexmitbad; 990 tp->snd_cwnd = tp->snd_cwnd_prev; 991 tp->snd_ssthresh = 992 tp->snd_ssthresh_prev; 993 tp->snd_high = tp->snd_high_prev; 994 tp->snd_nxt = tp->snd_max; 995 tp->t_badrxtwin = 0; 996 } 997 998 /* 999 * Recalculate the transmit timer / rtt. 1000 * 1001 * Some boxes send broken timestamp replies 1002 * during the SYN+ACK phase, ignore 1003 * timestamps of 0 or we could calculate a 1004 * huge RTT and blow up the retransmit timer. 1005 */ 1006 if ((to.to_flags & TOF_TS) != 0 && 1007 to.to_tsecr) { 1008 tcp_xmit_timer(tp, 1009 ticks - to.to_tsecr + 1); 1010 } else if (tp->t_rtttime && 1011 SEQ_GT(th->th_ack, tp->t_rtseq)) { 1012 tcp_xmit_timer(tp, 1013 ticks - tp->t_rtttime); 1014 } 1015 tcp_xmit_bandwidth_limit(tp, th->th_ack); 1016 acked = th->th_ack - tp->snd_una; 1017 tcpstat.tcps_rcvackpack++; 1018 tcpstat.tcps_rcvackbyte += acked; 1019 sbdrop(&so->so_snd, acked); 1020 if (SEQ_GT(tp->snd_una, tp->snd_high) && 1021 SEQ_LEQ(th->th_ack, tp->snd_high)) 1022 tp->snd_high = th->th_ack - 1; 1023 tp->snd_una = tp->snd_recover = th->th_ack; 1024 /* 1025 * pull snd_wl2 up to prevent seq wrap relative 1026 * to th_ack. 1027 */ 1028 tp->snd_wl2 = th->th_ack; 1029 tp->t_dupacks = 0; 1030 m_freem(m); 1031 ND6_HINT(tp); /* some progress has been done */ 1032 1033 /* 1034 * If all outstanding data are acked, stop 1035 * retransmit timer, otherwise restart timer 1036 * using current (possibly backed-off) value. 1037 * If process is waiting for space, 1038 * wakeup/selwakeup/signal. If data 1039 * are ready to send, let tcp_output 1040 * decide between more output or persist. 1041 */ 1042 if (tp->snd_una == tp->snd_max) 1043 callout_stop(tp->tt_rexmt); 1044 else if (!callout_active(tp->tt_persist)) 1045 callout_reset(tp->tt_rexmt, 1046 tp->t_rxtcur, 1047 tcp_timer_rexmt, tp); 1048 1049 sowwakeup(so); 1050 if (so->so_snd.sb_cc) 1051 (void) tcp_output(tp); 1052 INP_UNLOCK(inp); 1053 return; 1054 } 1055 } else if (th->th_ack == tp->snd_una && 1056 LIST_EMPTY(&tp->t_segq) && 1057 tlen <= sbspace(&so->so_rcv)) { 1058 KASSERT(headlocked, ("headlocked")); 1059 INP_INFO_WUNLOCK(&tcbinfo); 1060 headlocked = 0; 1061 /* 1062 * this is a pure, in-sequence data packet 1063 * with nothing on the reassembly queue and 1064 * we have enough buffer space to take it. 1065 */ 1066 ++tcpstat.tcps_preddat; 1067 tp->rcv_nxt += tlen; 1068 /* 1069 * Pull snd_wl1 up to prevent seq wrap relative to 1070 * th_seq. 1071 */ 1072 tp->snd_wl1 = th->th_seq; 1073 /* 1074 * Pull rcv_up up to prevent seq wrap relative to 1075 * rcv_nxt. 1076 */ 1077 tp->rcv_up = tp->rcv_nxt; 1078 tcpstat.tcps_rcvpack++; 1079 tcpstat.tcps_rcvbyte += tlen; 1080 ND6_HINT(tp); /* some progress has been done */ 1081 /* 1082 * Add data to socket buffer. 1083 */ 1084 if (so->so_state & SS_CANTRCVMORE) { 1085 m_freem(m); 1086 } else { 1087 m_adj(m, drop_hdrlen); /* delayed header drop */ 1088 sbappend(&so->so_rcv, m); 1089 } 1090 sorwakeup(so); 1091 if (DELAY_ACK(tp)) { 1092 callout_reset(tp->tt_delack, tcp_delacktime, 1093 tcp_timer_delack, tp); 1094 } else { 1095 tp->t_flags |= TF_ACKNOW; 1096 tcp_output(tp); 1097 } 1098 INP_UNLOCK(inp); 1099 return; 1100 } 1101 } 1102 1103 /* 1104 * Calculate amount of space in receive window, 1105 * and then do TCP input processing. 1106 * Receive window is amount of space in rcv queue, 1107 * but not less than advertised window. 1108 */ 1109 { int win; 1110 1111 win = sbspace(&so->so_rcv); 1112 if (win < 0) 1113 win = 0; 1114 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1115 } 1116 1117 switch (tp->t_state) { 1118 1119 /* 1120 * If the state is SYN_RECEIVED: 1121 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1122 */ 1123 case TCPS_SYN_RECEIVED: 1124 if ((thflags & TH_ACK) && 1125 (SEQ_LEQ(th->th_ack, tp->snd_una) || 1126 SEQ_GT(th->th_ack, tp->snd_max))) { 1127 rstreason = BANDLIM_RST_OPENPORT; 1128 goto dropwithreset; 1129 } 1130 break; 1131 1132 /* 1133 * If the state is SYN_SENT: 1134 * if seg contains an ACK, but not for our SYN, drop the input. 1135 * if seg contains a RST, then drop the connection. 1136 * if seg does not contain SYN, then drop it. 1137 * Otherwise this is an acceptable SYN segment 1138 * initialize tp->rcv_nxt and tp->irs 1139 * if seg contains ack then advance tp->snd_una 1140 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1141 * arrange for segment to be acked (eventually) 1142 * continue processing rest of data/controls, beginning with URG 1143 */ 1144 case TCPS_SYN_SENT: 1145 if ((taop = tcp_gettaocache(&inp->inp_inc)) == NULL) { 1146 taop = &tao_noncached; 1147 bzero(taop, sizeof(*taop)); 1148 } 1149 1150 if ((thflags & TH_ACK) && 1151 (SEQ_LEQ(th->th_ack, tp->iss) || 1152 SEQ_GT(th->th_ack, tp->snd_max))) { 1153 /* 1154 * If we have a cached CCsent for the remote host, 1155 * hence we haven't just crashed and restarted, 1156 * do not send a RST. This may be a retransmission 1157 * from the other side after our earlier ACK was lost. 1158 * Our new SYN, when it arrives, will serve as the 1159 * needed ACK. 1160 */ 1161 if (taop->tao_ccsent != 0) 1162 goto drop; 1163 else { 1164 rstreason = BANDLIM_UNLIMITED; 1165 goto dropwithreset; 1166 } 1167 } 1168 if (thflags & TH_RST) { 1169 if (thflags & TH_ACK) 1170 tp = tcp_drop(tp, ECONNREFUSED); 1171 goto drop; 1172 } 1173 if ((thflags & TH_SYN) == 0) 1174 goto drop; 1175 tp->snd_wnd = th->th_win; /* initial send window */ 1176 tp->cc_recv = to.to_cc; /* foreign CC */ 1177 1178 tp->irs = th->th_seq; 1179 tcp_rcvseqinit(tp); 1180 if (thflags & TH_ACK) { 1181 /* 1182 * Our SYN was acked. If segment contains CC.ECHO 1183 * option, check it to make sure this segment really 1184 * matches our SYN. If not, just drop it as old 1185 * duplicate, but send an RST if we're still playing 1186 * by the old rules. If no CC.ECHO option, make sure 1187 * we don't get fooled into using T/TCP. 1188 */ 1189 if (to.to_flags & TOF_CCECHO) { 1190 if (tp->cc_send != to.to_ccecho) { 1191 if (taop->tao_ccsent != 0) 1192 goto drop; 1193 else { 1194 rstreason = BANDLIM_UNLIMITED; 1195 goto dropwithreset; 1196 } 1197 } 1198 } else 1199 tp->t_flags &= ~TF_RCVD_CC; 1200 tcpstat.tcps_connects++; 1201 soisconnected(so); 1202 #ifdef MAC 1203 mac_set_socket_peer_from_mbuf(m, so); 1204 #endif 1205 /* Do window scaling on this connection? */ 1206 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1207 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1208 tp->snd_scale = tp->requested_s_scale; 1209 tp->rcv_scale = tp->request_r_scale; 1210 } 1211 /* Segment is acceptable, update cache if undefined. */ 1212 if (taop->tao_ccsent == 0) 1213 taop->tao_ccsent = to.to_ccecho; 1214 1215 tp->rcv_adv += tp->rcv_wnd; 1216 tp->snd_una++; /* SYN is acked */ 1217 /* 1218 * If there's data, delay ACK; if there's also a FIN 1219 * ACKNOW will be turned on later. 1220 */ 1221 if (DELAY_ACK(tp) && tlen != 0) 1222 callout_reset(tp->tt_delack, tcp_delacktime, 1223 tcp_timer_delack, tp); 1224 else 1225 tp->t_flags |= TF_ACKNOW; 1226 /* 1227 * Received <SYN,ACK> in SYN_SENT[*] state. 1228 * Transitions: 1229 * SYN_SENT --> ESTABLISHED 1230 * SYN_SENT* --> FIN_WAIT_1 1231 */ 1232 tp->t_starttime = ticks; 1233 if (tp->t_flags & TF_NEEDFIN) { 1234 tp->t_state = TCPS_FIN_WAIT_1; 1235 tp->t_flags &= ~TF_NEEDFIN; 1236 thflags &= ~TH_SYN; 1237 } else { 1238 tp->t_state = TCPS_ESTABLISHED; 1239 callout_reset(tp->tt_keep, tcp_keepidle, 1240 tcp_timer_keep, tp); 1241 } 1242 } else { 1243 /* 1244 * Received initial SYN in SYN-SENT[*] state => 1245 * simultaneous open. If segment contains CC option 1246 * and there is a cached CC, apply TAO test. 1247 * If it succeeds, connection is * half-synchronized. 1248 * Otherwise, do 3-way handshake: 1249 * SYN-SENT -> SYN-RECEIVED 1250 * SYN-SENT* -> SYN-RECEIVED* 1251 * If there was no CC option, clear cached CC value. 1252 */ 1253 tp->t_flags |= TF_ACKNOW; 1254 callout_stop(tp->tt_rexmt); 1255 if (to.to_flags & TOF_CC) { 1256 if (taop->tao_cc != 0 && 1257 CC_GT(to.to_cc, taop->tao_cc)) { 1258 /* 1259 * update cache and make transition: 1260 * SYN-SENT -> ESTABLISHED* 1261 * SYN-SENT* -> FIN-WAIT-1* 1262 */ 1263 taop->tao_cc = to.to_cc; 1264 tp->t_starttime = ticks; 1265 if (tp->t_flags & TF_NEEDFIN) { 1266 tp->t_state = TCPS_FIN_WAIT_1; 1267 tp->t_flags &= ~TF_NEEDFIN; 1268 } else { 1269 tp->t_state = TCPS_ESTABLISHED; 1270 callout_reset(tp->tt_keep, 1271 tcp_keepidle, 1272 tcp_timer_keep, 1273 tp); 1274 } 1275 tp->t_flags |= TF_NEEDSYN; 1276 } else 1277 tp->t_state = TCPS_SYN_RECEIVED; 1278 } else { 1279 /* CC.NEW or no option => invalidate cache */ 1280 taop->tao_cc = 0; 1281 tp->t_state = TCPS_SYN_RECEIVED; 1282 } 1283 } 1284 1285 trimthenstep6: 1286 /* 1287 * Advance th->th_seq to correspond to first data byte. 1288 * If data, trim to stay within window, 1289 * dropping FIN if necessary. 1290 */ 1291 th->th_seq++; 1292 if (tlen > tp->rcv_wnd) { 1293 todrop = tlen - tp->rcv_wnd; 1294 m_adj(m, -todrop); 1295 tlen = tp->rcv_wnd; 1296 thflags &= ~TH_FIN; 1297 tcpstat.tcps_rcvpackafterwin++; 1298 tcpstat.tcps_rcvbyteafterwin += todrop; 1299 } 1300 tp->snd_wl1 = th->th_seq - 1; 1301 tp->rcv_up = th->th_seq; 1302 /* 1303 * Client side of transaction: already sent SYN and data. 1304 * If the remote host used T/TCP to validate the SYN, 1305 * our data will be ACK'd; if so, enter normal data segment 1306 * processing in the middle of step 5, ack processing. 1307 * Otherwise, goto step 6. 1308 */ 1309 if (thflags & TH_ACK) 1310 goto process_ACK; 1311 1312 goto step6; 1313 1314 /* 1315 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1316 * if segment contains a SYN and CC [not CC.NEW] option: 1317 * if state == TIME_WAIT and connection duration > MSL, 1318 * drop packet and send RST; 1319 * 1320 * if SEG.CC > CCrecv then is new SYN, and can implicitly 1321 * ack the FIN (and data) in retransmission queue. 1322 * Complete close and delete TCPCB. Then reprocess 1323 * segment, hoping to find new TCPCB in LISTEN state; 1324 * 1325 * else must be old SYN; drop it. 1326 * else do normal processing. 1327 */ 1328 case TCPS_LAST_ACK: 1329 case TCPS_CLOSING: 1330 case TCPS_TIME_WAIT: 1331 if ((thflags & TH_SYN) && 1332 (to.to_flags & TOF_CC) && tp->cc_recv != 0) { 1333 if (tp->t_state == TCPS_TIME_WAIT && 1334 (ticks - tp->t_starttime) > tcp_msl) { 1335 rstreason = BANDLIM_UNLIMITED; 1336 goto dropwithreset; 1337 } 1338 if (CC_GT(to.to_cc, tp->cc_recv)) { 1339 tp = tcp_close(tp); 1340 goto findpcb; 1341 } 1342 else 1343 goto drop; 1344 } 1345 break; /* continue normal processing */ 1346 } 1347 1348 /* 1349 * States other than LISTEN or SYN_SENT. 1350 * First check the RST flag and sequence number since reset segments 1351 * are exempt from the timestamp and connection count tests. This 1352 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 1353 * below which allowed reset segments in half the sequence space 1354 * to fall though and be processed (which gives forged reset 1355 * segments with a random sequence number a 50 percent chance of 1356 * killing a connection). 1357 * Then check timestamp, if present. 1358 * Then check the connection count, if present. 1359 * Then check that at least some bytes of segment are within 1360 * receive window. If segment begins before rcv_nxt, 1361 * drop leading data (and SYN); if nothing left, just ack. 1362 * 1363 * 1364 * If the RST bit is set, check the sequence number to see 1365 * if this is a valid reset segment. 1366 * RFC 793 page 37: 1367 * In all states except SYN-SENT, all reset (RST) segments 1368 * are validated by checking their SEQ-fields. A reset is 1369 * valid if its sequence number is in the window. 1370 * Note: this does not take into account delayed ACKs, so 1371 * we should test against last_ack_sent instead of rcv_nxt. 1372 * The sequence number in the reset segment is normally an 1373 * echo of our outgoing acknowlegement numbers, but some hosts 1374 * send a reset with the sequence number at the rightmost edge 1375 * of our receive window, and we have to handle this case. 1376 * If we have multiple segments in flight, the intial reset 1377 * segment sequence numbers will be to the left of last_ack_sent, 1378 * but they will eventually catch up. 1379 * In any case, it never made sense to trim reset segments to 1380 * fit the receive window since RFC 1122 says: 1381 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 1382 * 1383 * A TCP SHOULD allow a received RST segment to include data. 1384 * 1385 * DISCUSSION 1386 * It has been suggested that a RST segment could contain 1387 * ASCII text that encoded and explained the cause of the 1388 * RST. No standard has yet been established for such 1389 * data. 1390 * 1391 * If the reset segment passes the sequence number test examine 1392 * the state: 1393 * SYN_RECEIVED STATE: 1394 * If passive open, return to LISTEN state. 1395 * If active open, inform user that connection was refused. 1396 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 1397 * Inform user that connection was reset, and close tcb. 1398 * CLOSING, LAST_ACK STATES: 1399 * Close the tcb. 1400 * TIME_WAIT STATE: 1401 * Drop the segment - see Stevens, vol. 2, p. 964 and 1402 * RFC 1337. 1403 */ 1404 if (thflags & TH_RST) { 1405 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1406 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 1407 switch (tp->t_state) { 1408 1409 case TCPS_SYN_RECEIVED: 1410 so->so_error = ECONNREFUSED; 1411 goto close; 1412 1413 case TCPS_ESTABLISHED: 1414 case TCPS_FIN_WAIT_1: 1415 case TCPS_FIN_WAIT_2: 1416 case TCPS_CLOSE_WAIT: 1417 so->so_error = ECONNRESET; 1418 close: 1419 tp->t_state = TCPS_CLOSED; 1420 tcpstat.tcps_drops++; 1421 tp = tcp_close(tp); 1422 break; 1423 1424 case TCPS_CLOSING: 1425 case TCPS_LAST_ACK: 1426 tp = tcp_close(tp); 1427 break; 1428 1429 case TCPS_TIME_WAIT: 1430 break; 1431 } 1432 } 1433 goto drop; 1434 } 1435 1436 /* 1437 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1438 * and it's less than ts_recent, drop it. 1439 */ 1440 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 1441 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1442 1443 /* Check to see if ts_recent is over 24 days old. */ 1444 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1445 /* 1446 * Invalidate ts_recent. If this segment updates 1447 * ts_recent, the age will be reset later and ts_recent 1448 * will get a valid value. If it does not, setting 1449 * ts_recent to zero will at least satisfy the 1450 * requirement that zero be placed in the timestamp 1451 * echo reply when ts_recent isn't valid. The 1452 * age isn't reset until we get a valid ts_recent 1453 * because we don't want out-of-order segments to be 1454 * dropped when ts_recent is old. 1455 */ 1456 tp->ts_recent = 0; 1457 } else { 1458 tcpstat.tcps_rcvduppack++; 1459 tcpstat.tcps_rcvdupbyte += tlen; 1460 tcpstat.tcps_pawsdrop++; 1461 if (tlen) 1462 goto dropafterack; 1463 goto drop; 1464 } 1465 } 1466 1467 /* 1468 * T/TCP mechanism 1469 * If T/TCP was negotiated and the segment doesn't have CC, 1470 * or if its CC is wrong then drop the segment. 1471 * RST segments do not have to comply with this. 1472 */ 1473 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1474 ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1475 goto dropafterack; 1476 1477 /* 1478 * In the SYN-RECEIVED state, validate that the packet belongs to 1479 * this connection before trimming the data to fit the receive 1480 * window. Check the sequence number versus IRS since we know 1481 * the sequence numbers haven't wrapped. This is a partial fix 1482 * for the "LAND" DoS attack. 1483 */ 1484 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1485 rstreason = BANDLIM_RST_OPENPORT; 1486 goto dropwithreset; 1487 } 1488 1489 todrop = tp->rcv_nxt - th->th_seq; 1490 if (todrop > 0) { 1491 if (thflags & TH_SYN) { 1492 thflags &= ~TH_SYN; 1493 th->th_seq++; 1494 if (th->th_urp > 1) 1495 th->th_urp--; 1496 else 1497 thflags &= ~TH_URG; 1498 todrop--; 1499 } 1500 /* 1501 * Following if statement from Stevens, vol. 2, p. 960. 1502 */ 1503 if (todrop > tlen 1504 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1505 /* 1506 * Any valid FIN must be to the left of the window. 1507 * At this point the FIN must be a duplicate or out 1508 * of sequence; drop it. 1509 */ 1510 thflags &= ~TH_FIN; 1511 1512 /* 1513 * Send an ACK to resynchronize and drop any data. 1514 * But keep on processing for RST or ACK. 1515 */ 1516 tp->t_flags |= TF_ACKNOW; 1517 todrop = tlen; 1518 tcpstat.tcps_rcvduppack++; 1519 tcpstat.tcps_rcvdupbyte += todrop; 1520 } else { 1521 tcpstat.tcps_rcvpartduppack++; 1522 tcpstat.tcps_rcvpartdupbyte += todrop; 1523 } 1524 drop_hdrlen += todrop; /* drop from the top afterwards */ 1525 th->th_seq += todrop; 1526 tlen -= todrop; 1527 if (th->th_urp > todrop) 1528 th->th_urp -= todrop; 1529 else { 1530 thflags &= ~TH_URG; 1531 th->th_urp = 0; 1532 } 1533 } 1534 1535 /* 1536 * If new data are received on a connection after the 1537 * user processes are gone, then RST the other end. 1538 */ 1539 if ((so->so_state & SS_NOFDREF) && 1540 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1541 tp = tcp_close(tp); 1542 tcpstat.tcps_rcvafterclose++; 1543 rstreason = BANDLIM_UNLIMITED; 1544 goto dropwithreset; 1545 } 1546 1547 /* 1548 * If segment ends after window, drop trailing data 1549 * (and PUSH and FIN); if nothing left, just ACK. 1550 */ 1551 todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1552 if (todrop > 0) { 1553 tcpstat.tcps_rcvpackafterwin++; 1554 if (todrop >= tlen) { 1555 tcpstat.tcps_rcvbyteafterwin += tlen; 1556 /* 1557 * If a new connection request is received 1558 * while in TIME_WAIT, drop the old connection 1559 * and start over if the sequence numbers 1560 * are above the previous ones. 1561 */ 1562 if (thflags & TH_SYN && 1563 tp->t_state == TCPS_TIME_WAIT && 1564 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1565 tp = tcp_close(tp); 1566 goto findpcb; 1567 } 1568 /* 1569 * If window is closed can only take segments at 1570 * window edge, and have to drop data and PUSH from 1571 * incoming segments. Continue processing, but 1572 * remember to ack. Otherwise, drop segment 1573 * and ack. 1574 */ 1575 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1576 tp->t_flags |= TF_ACKNOW; 1577 tcpstat.tcps_rcvwinprobe++; 1578 } else 1579 goto dropafterack; 1580 } else 1581 tcpstat.tcps_rcvbyteafterwin += todrop; 1582 m_adj(m, -todrop); 1583 tlen -= todrop; 1584 thflags &= ~(TH_PUSH|TH_FIN); 1585 } 1586 1587 /* 1588 * If last ACK falls within this segment's sequence numbers, 1589 * record its timestamp. 1590 * NOTE that the test is modified according to the latest 1591 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1592 */ 1593 if ((to.to_flags & TOF_TS) != 0 && 1594 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1595 tp->ts_recent_age = ticks; 1596 tp->ts_recent = to.to_tsval; 1597 } 1598 1599 /* 1600 * If a SYN is in the window, then this is an 1601 * error and we send an RST and drop the connection. 1602 */ 1603 if (thflags & TH_SYN) { 1604 tp = tcp_drop(tp, ECONNRESET); 1605 rstreason = BANDLIM_UNLIMITED; 1606 goto dropwithreset; 1607 } 1608 1609 /* 1610 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1611 * flag is on (half-synchronized state), then queue data for 1612 * later processing; else drop segment and return. 1613 */ 1614 if ((thflags & TH_ACK) == 0) { 1615 if (tp->t_state == TCPS_SYN_RECEIVED || 1616 (tp->t_flags & TF_NEEDSYN)) 1617 goto step6; 1618 else 1619 goto drop; 1620 } 1621 1622 /* 1623 * Ack processing. 1624 */ 1625 switch (tp->t_state) { 1626 1627 /* 1628 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1629 * ESTABLISHED state and continue processing. 1630 * The ACK was checked above. 1631 */ 1632 case TCPS_SYN_RECEIVED: 1633 1634 tcpstat.tcps_connects++; 1635 soisconnected(so); 1636 /* Do window scaling? */ 1637 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1638 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1639 tp->snd_scale = tp->requested_s_scale; 1640 tp->rcv_scale = tp->request_r_scale; 1641 } 1642 /* 1643 * Upon successful completion of 3-way handshake, 1644 * update cache.CC if it was undefined, pass any queued 1645 * data to the user, and advance state appropriately. 1646 */ 1647 if ((taop = tcp_gettaocache(&inp->inp_inc)) != NULL && 1648 taop->tao_cc == 0) 1649 taop->tao_cc = tp->cc_recv; 1650 1651 /* 1652 * Make transitions: 1653 * SYN-RECEIVED -> ESTABLISHED 1654 * SYN-RECEIVED* -> FIN-WAIT-1 1655 */ 1656 tp->t_starttime = ticks; 1657 if (tp->t_flags & TF_NEEDFIN) { 1658 tp->t_state = TCPS_FIN_WAIT_1; 1659 tp->t_flags &= ~TF_NEEDFIN; 1660 } else { 1661 tp->t_state = TCPS_ESTABLISHED; 1662 callout_reset(tp->tt_keep, tcp_keepidle, 1663 tcp_timer_keep, tp); 1664 } 1665 /* 1666 * If segment contains data or ACK, will call tcp_reass() 1667 * later; if not, do so now to pass queued data to user. 1668 */ 1669 if (tlen == 0 && (thflags & TH_FIN) == 0) 1670 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1671 (struct mbuf *)0); 1672 tp->snd_wl1 = th->th_seq - 1; 1673 /* FALLTHROUGH */ 1674 1675 /* 1676 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1677 * ACKs. If the ack is in the range 1678 * tp->snd_una < th->th_ack <= tp->snd_max 1679 * then advance tp->snd_una to th->th_ack and drop 1680 * data from the retransmission queue. If this ACK reflects 1681 * more up to date window information we update our window information. 1682 */ 1683 case TCPS_ESTABLISHED: 1684 case TCPS_FIN_WAIT_1: 1685 case TCPS_FIN_WAIT_2: 1686 case TCPS_CLOSE_WAIT: 1687 case TCPS_CLOSING: 1688 case TCPS_LAST_ACK: 1689 case TCPS_TIME_WAIT: 1690 1691 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1692 if (tlen == 0 && tiwin == tp->snd_wnd) { 1693 tcpstat.tcps_rcvdupack++; 1694 /* 1695 * If we have outstanding data (other than 1696 * a window probe), this is a completely 1697 * duplicate ack (ie, window info didn't 1698 * change), the ack is the biggest we've 1699 * seen and we've seen exactly our rexmt 1700 * threshhold of them, assume a packet 1701 * has been dropped and retransmit it. 1702 * Kludge snd_nxt & the congestion 1703 * window so we send only this one 1704 * packet. 1705 * 1706 * We know we're losing at the current 1707 * window size so do congestion avoidance 1708 * (set ssthresh to half the current window 1709 * and pull our congestion window back to 1710 * the new ssthresh). 1711 * 1712 * Dup acks mean that packets have left the 1713 * network (they're now cached at the receiver) 1714 * so bump cwnd by the amount in the receiver 1715 * to keep a constant cwnd packets in the 1716 * network. 1717 */ 1718 if (!callout_active(tp->tt_rexmt) || 1719 th->th_ack != tp->snd_una) 1720 tp->t_dupacks = 0; 1721 else if (++tp->t_dupacks > tcprexmtthresh || 1722 (tcp_do_newreno && 1723 SEQ_LT(tp->snd_una, 1724 tp->snd_recover))) { 1725 tp->snd_cwnd += tp->t_maxseg; 1726 (void) tcp_output(tp); 1727 goto drop; 1728 } else if (tp->t_dupacks == tcprexmtthresh) { 1729 tcp_seq onxt = tp->snd_nxt; 1730 u_int win; 1731 if (tcp_do_newreno && 1732 SEQ_LEQ(th->th_ack, tp->snd_high)) { 1733 tp->t_dupacks = 0; 1734 break; 1735 } 1736 win = min(tp->snd_wnd, tp->snd_cwnd) / 1737 2 / tp->t_maxseg; 1738 if (win < 2) 1739 win = 2; 1740 tp->snd_ssthresh = win * tp->t_maxseg; 1741 tp->snd_recover = tp->snd_max; 1742 callout_stop(tp->tt_rexmt); 1743 tp->t_rtttime = 0; 1744 tp->snd_nxt = th->th_ack; 1745 tp->snd_cwnd = tp->t_maxseg; 1746 (void) tcp_output(tp); 1747 tp->snd_cwnd = tp->snd_ssthresh + 1748 tp->t_maxseg * tp->t_dupacks; 1749 if (SEQ_GT(onxt, tp->snd_nxt)) 1750 tp->snd_nxt = onxt; 1751 goto drop; 1752 } 1753 } else 1754 tp->t_dupacks = 0; 1755 break; 1756 } 1757 1758 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); 1759 1760 /* 1761 * If the congestion window was inflated to account 1762 * for the other side's cached packets, retract it. 1763 */ 1764 if (tcp_do_newreno) { 1765 if (SEQ_LT(tp->snd_una, tp->snd_recover)) { 1766 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 1767 tcp_newreno_partial_ack(tp, th); 1768 } else { 1769 /* 1770 * Window inflation should have left us 1771 * with approximately snd_ssthresh 1772 * outstanding data. 1773 * But in case we would be inclined to 1774 * send a burst, better to do it via 1775 * the slow start mechanism. 1776 */ 1777 if (SEQ_GT(th->th_ack + 1778 tp->snd_ssthresh, 1779 tp->snd_max)) 1780 tp->snd_cwnd = tp->snd_max - 1781 th->th_ack + 1782 tp->t_maxseg; 1783 else 1784 tp->snd_cwnd = tp->snd_ssthresh; 1785 } 1786 } 1787 } else { 1788 if (tp->t_dupacks >= tcprexmtthresh && 1789 tp->snd_cwnd > tp->snd_ssthresh) 1790 tp->snd_cwnd = tp->snd_ssthresh; 1791 } 1792 tp->t_dupacks = 0; 1793 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1794 tcpstat.tcps_rcvacktoomuch++; 1795 goto dropafterack; 1796 } 1797 /* 1798 * If we reach this point, ACK is not a duplicate, 1799 * i.e., it ACKs something we sent. 1800 */ 1801 if (tp->t_flags & TF_NEEDSYN) { 1802 /* 1803 * T/TCP: Connection was half-synchronized, and our 1804 * SYN has been ACK'd (so connection is now fully 1805 * synchronized). Go to non-starred state, 1806 * increment snd_una for ACK of SYN, and check if 1807 * we can do window scaling. 1808 */ 1809 tp->t_flags &= ~TF_NEEDSYN; 1810 tp->snd_una++; 1811 /* Do window scaling? */ 1812 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1813 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1814 tp->snd_scale = tp->requested_s_scale; 1815 tp->rcv_scale = tp->request_r_scale; 1816 } 1817 } 1818 1819 process_ACK: 1820 acked = th->th_ack - tp->snd_una; 1821 tcpstat.tcps_rcvackpack++; 1822 tcpstat.tcps_rcvackbyte += acked; 1823 1824 /* 1825 * If we just performed our first retransmit, and the ACK 1826 * arrives within our recovery window, then it was a mistake 1827 * to do the retransmit in the first place. Recover our 1828 * original cwnd and ssthresh, and proceed to transmit where 1829 * we left off. 1830 */ 1831 if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1832 ++tcpstat.tcps_sndrexmitbad; 1833 tp->snd_cwnd = tp->snd_cwnd_prev; 1834 tp->snd_ssthresh = tp->snd_ssthresh_prev; 1835 tp->snd_high = tp->snd_high_prev; 1836 tp->snd_nxt = tp->snd_max; 1837 tp->t_badrxtwin = 0; /* XXX probably not required */ 1838 } 1839 1840 /* 1841 * If we have a timestamp reply, update smoothed 1842 * round trip time. If no timestamp is present but 1843 * transmit timer is running and timed sequence 1844 * number was acked, update smoothed round trip time. 1845 * Since we now have an rtt measurement, cancel the 1846 * timer backoff (cf., Phil Karn's retransmit alg.). 1847 * Recompute the initial retransmit timer. 1848 * 1849 * Some boxes send broken timestamp replies 1850 * during the SYN+ACK phase, ignore 1851 * timestamps of 0 or we could calculate a 1852 * huge RTT and blow up the retransmit timer. 1853 */ 1854 if ((to.to_flags & TOF_TS) != 0 && 1855 to.to_tsecr) { 1856 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1857 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 1858 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1859 } 1860 tcp_xmit_bandwidth_limit(tp, th->th_ack); 1861 1862 /* 1863 * If all outstanding data is acked, stop retransmit 1864 * timer and remember to restart (more output or persist). 1865 * If there is more data to be acked, restart retransmit 1866 * timer, using current (possibly backed-off) value. 1867 */ 1868 if (th->th_ack == tp->snd_max) { 1869 callout_stop(tp->tt_rexmt); 1870 needoutput = 1; 1871 } else if (!callout_active(tp->tt_persist)) 1872 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 1873 tcp_timer_rexmt, tp); 1874 1875 /* 1876 * If no data (only SYN) was ACK'd, 1877 * skip rest of ACK processing. 1878 */ 1879 if (acked == 0) 1880 goto step6; 1881 1882 /* 1883 * When new data is acked, open the congestion window. 1884 * If the window gives us less than ssthresh packets 1885 * in flight, open exponentially (maxseg per packet). 1886 * Otherwise open linearly: maxseg per window 1887 * (maxseg^2 / cwnd per packet). 1888 */ 1889 if (!tcp_do_newreno || SEQ_GEQ(tp->snd_una, tp->snd_recover)) { 1890 register u_int cw = tp->snd_cwnd; 1891 register u_int incr = tp->t_maxseg; 1892 if (cw > tp->snd_ssthresh) 1893 incr = incr * incr / cw; 1894 tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 1895 } 1896 if (acked > so->so_snd.sb_cc) { 1897 tp->snd_wnd -= so->so_snd.sb_cc; 1898 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1899 ourfinisacked = 1; 1900 } else { 1901 sbdrop(&so->so_snd, acked); 1902 tp->snd_wnd -= acked; 1903 ourfinisacked = 0; 1904 } 1905 sowwakeup(so); 1906 /* detect una wraparound */ 1907 if (SEQ_GEQ(tp->snd_una, tp->snd_recover) && 1908 SEQ_LT(th->th_ack, tp->snd_recover)) 1909 tp->snd_recover = th->th_ack; 1910 if (SEQ_GT(tp->snd_una, tp->snd_high) && 1911 SEQ_LEQ(th->th_ack, tp->snd_high)) 1912 tp->snd_high = th->th_ack - 1; 1913 tp->snd_una = th->th_ack; 1914 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1915 tp->snd_nxt = tp->snd_una; 1916 1917 switch (tp->t_state) { 1918 1919 /* 1920 * In FIN_WAIT_1 STATE in addition to the processing 1921 * for the ESTABLISHED state if our FIN is now acknowledged 1922 * then enter FIN_WAIT_2. 1923 */ 1924 case TCPS_FIN_WAIT_1: 1925 if (ourfinisacked) { 1926 /* 1927 * If we can't receive any more 1928 * data, then closing user can proceed. 1929 * Starting the timer is contrary to the 1930 * specification, but if we don't get a FIN 1931 * we'll hang forever. 1932 */ 1933 if (so->so_state & SS_CANTRCVMORE) { 1934 soisdisconnected(so); 1935 callout_reset(tp->tt_2msl, tcp_maxidle, 1936 tcp_timer_2msl, tp); 1937 } 1938 tp->t_state = TCPS_FIN_WAIT_2; 1939 } 1940 break; 1941 1942 /* 1943 * In CLOSING STATE in addition to the processing for 1944 * the ESTABLISHED state if the ACK acknowledges our FIN 1945 * then enter the TIME-WAIT state, otherwise ignore 1946 * the segment. 1947 */ 1948 case TCPS_CLOSING: 1949 if (ourfinisacked) { 1950 tp->t_state = TCPS_TIME_WAIT; 1951 tcp_canceltimers(tp); 1952 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1953 if (tp->cc_recv != 0 && 1954 (ticks - tp->t_starttime) < tcp_msl) 1955 callout_reset(tp->tt_2msl, 1956 tp->t_rxtcur * 1957 TCPTV_TWTRUNC, 1958 tcp_timer_2msl, tp); 1959 else 1960 callout_reset(tp->tt_2msl, 2 * tcp_msl, 1961 tcp_timer_2msl, tp); 1962 soisdisconnected(so); 1963 } 1964 break; 1965 1966 /* 1967 * In LAST_ACK, we may still be waiting for data to drain 1968 * and/or to be acked, as well as for the ack of our FIN. 1969 * If our FIN is now acknowledged, delete the TCB, 1970 * enter the closed state and return. 1971 */ 1972 case TCPS_LAST_ACK: 1973 if (ourfinisacked) { 1974 tp = tcp_close(tp); 1975 goto drop; 1976 } 1977 break; 1978 1979 /* 1980 * In TIME_WAIT state the only thing that should arrive 1981 * is a retransmission of the remote FIN. Acknowledge 1982 * it and restart the finack timer. 1983 */ 1984 case TCPS_TIME_WAIT: 1985 callout_reset(tp->tt_2msl, 2 * tcp_msl, 1986 tcp_timer_2msl, tp); 1987 goto dropafterack; 1988 } 1989 } 1990 1991 step6: 1992 /* 1993 * Update window information. 1994 * Don't look at window if no ACK: TAC's send garbage on first SYN. 1995 */ 1996 if ((thflags & TH_ACK) && 1997 (SEQ_LT(tp->snd_wl1, th->th_seq) || 1998 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 1999 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2000 /* keep track of pure window updates */ 2001 if (tlen == 0 && 2002 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2003 tcpstat.tcps_rcvwinupd++; 2004 tp->snd_wnd = tiwin; 2005 tp->snd_wl1 = th->th_seq; 2006 tp->snd_wl2 = th->th_ack; 2007 if (tp->snd_wnd > tp->max_sndwnd) 2008 tp->max_sndwnd = tp->snd_wnd; 2009 needoutput = 1; 2010 } 2011 2012 /* 2013 * Process segments with URG. 2014 */ 2015 if ((thflags & TH_URG) && th->th_urp && 2016 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2017 /* 2018 * This is a kludge, but if we receive and accept 2019 * random urgent pointers, we'll crash in 2020 * soreceive. It's hard to imagine someone 2021 * actually wanting to send this much urgent data. 2022 */ 2023 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2024 th->th_urp = 0; /* XXX */ 2025 thflags &= ~TH_URG; /* XXX */ 2026 goto dodata; /* XXX */ 2027 } 2028 /* 2029 * If this segment advances the known urgent pointer, 2030 * then mark the data stream. This should not happen 2031 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2032 * a FIN has been received from the remote side. 2033 * In these states we ignore the URG. 2034 * 2035 * According to RFC961 (Assigned Protocols), 2036 * the urgent pointer points to the last octet 2037 * of urgent data. We continue, however, 2038 * to consider it to indicate the first octet 2039 * of data past the urgent section as the original 2040 * spec states (in one of two places). 2041 */ 2042 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2043 tp->rcv_up = th->th_seq + th->th_urp; 2044 so->so_oobmark = so->so_rcv.sb_cc + 2045 (tp->rcv_up - tp->rcv_nxt) - 1; 2046 if (so->so_oobmark == 0) 2047 so->so_state |= SS_RCVATMARK; 2048 sohasoutofband(so); 2049 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2050 } 2051 /* 2052 * Remove out of band data so doesn't get presented to user. 2053 * This can happen independent of advancing the URG pointer, 2054 * but if two URG's are pending at once, some out-of-band 2055 * data may creep in... ick. 2056 */ 2057 if (th->th_urp <= (u_long)tlen && 2058 !(so->so_options & SO_OOBINLINE)) { 2059 /* hdr drop is delayed */ 2060 tcp_pulloutofband(so, th, m, drop_hdrlen); 2061 } 2062 } else { 2063 /* 2064 * If no out of band data is expected, 2065 * pull receive urgent pointer along 2066 * with the receive window. 2067 */ 2068 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2069 tp->rcv_up = tp->rcv_nxt; 2070 } 2071 dodata: /* XXX */ 2072 KASSERT(headlocked, ("headlocked")); 2073 INP_INFO_WUNLOCK(&tcbinfo); 2074 headlocked = 0; 2075 /* 2076 * Process the segment text, merging it into the TCP sequencing queue, 2077 * and arranging for acknowledgment of receipt if necessary. 2078 * This process logically involves adjusting tp->rcv_wnd as data 2079 * is presented to the user (this happens in tcp_usrreq.c, 2080 * case PRU_RCVD). If a FIN has already been received on this 2081 * connection then we just ignore the text. 2082 */ 2083 if ((tlen || (thflags & TH_FIN)) && 2084 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2085 m_adj(m, drop_hdrlen); /* delayed header drop */ 2086 /* 2087 * Insert segment which includes th into TCP reassembly queue 2088 * with control block tp. Set thflags to whether reassembly now 2089 * includes a segment with FIN. This handles the common case 2090 * inline (segment is the next to be received on an established 2091 * connection, and the queue is empty), avoiding linkage into 2092 * and removal from the queue and repetition of various 2093 * conversions. 2094 * Set DELACK for segments received in order, but ack 2095 * immediately when segments are out of order (so 2096 * fast retransmit can work). 2097 */ 2098 if (th->th_seq == tp->rcv_nxt && 2099 LIST_EMPTY(&tp->t_segq) && 2100 TCPS_HAVEESTABLISHED(tp->t_state)) { 2101 if (DELAY_ACK(tp)) 2102 callout_reset(tp->tt_delack, tcp_delacktime, 2103 tcp_timer_delack, tp); 2104 else 2105 tp->t_flags |= TF_ACKNOW; 2106 tp->rcv_nxt += tlen; 2107 thflags = th->th_flags & TH_FIN; 2108 tcpstat.tcps_rcvpack++; 2109 tcpstat.tcps_rcvbyte += tlen; 2110 ND6_HINT(tp); 2111 if (so->so_state & SS_CANTRCVMORE) 2112 m_freem(m); 2113 else 2114 sbappend(&so->so_rcv, m); 2115 sorwakeup(so); 2116 } else { 2117 thflags = tcp_reass(tp, th, &tlen, m); 2118 tp->t_flags |= TF_ACKNOW; 2119 } 2120 2121 /* 2122 * Note the amount of data that peer has sent into 2123 * our window, in order to estimate the sender's 2124 * buffer size. 2125 */ 2126 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2127 } else { 2128 m_freem(m); 2129 thflags &= ~TH_FIN; 2130 } 2131 2132 /* 2133 * If FIN is received ACK the FIN and let the user know 2134 * that the connection is closing. 2135 */ 2136 if (thflags & TH_FIN) { 2137 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2138 socantrcvmore(so); 2139 /* 2140 * If connection is half-synchronized 2141 * (ie NEEDSYN flag on) then delay ACK, 2142 * so it may be piggybacked when SYN is sent. 2143 * Otherwise, since we received a FIN then no 2144 * more input can be expected, send ACK now. 2145 */ 2146 if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN)) 2147 callout_reset(tp->tt_delack, tcp_delacktime, 2148 tcp_timer_delack, tp); 2149 else 2150 tp->t_flags |= TF_ACKNOW; 2151 tp->rcv_nxt++; 2152 } 2153 switch (tp->t_state) { 2154 2155 /* 2156 * In SYN_RECEIVED and ESTABLISHED STATES 2157 * enter the CLOSE_WAIT state. 2158 */ 2159 case TCPS_SYN_RECEIVED: 2160 tp->t_starttime = ticks; 2161 /*FALLTHROUGH*/ 2162 case TCPS_ESTABLISHED: 2163 tp->t_state = TCPS_CLOSE_WAIT; 2164 break; 2165 2166 /* 2167 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2168 * enter the CLOSING state. 2169 */ 2170 case TCPS_FIN_WAIT_1: 2171 tp->t_state = TCPS_CLOSING; 2172 break; 2173 2174 /* 2175 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2176 * starting the time-wait timer, turning off the other 2177 * standard timers. 2178 */ 2179 case TCPS_FIN_WAIT_2: 2180 tp->t_state = TCPS_TIME_WAIT; 2181 tcp_canceltimers(tp); 2182 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 2183 if (tp->cc_recv != 0 && 2184 (ticks - tp->t_starttime) < tcp_msl) { 2185 callout_reset(tp->tt_2msl, 2186 tp->t_rxtcur * TCPTV_TWTRUNC, 2187 tcp_timer_2msl, tp); 2188 /* For transaction client, force ACK now. */ 2189 tp->t_flags |= TF_ACKNOW; 2190 } 2191 else 2192 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2193 tcp_timer_2msl, tp); 2194 soisdisconnected(so); 2195 break; 2196 2197 /* 2198 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2199 */ 2200 case TCPS_TIME_WAIT: 2201 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2202 tcp_timer_2msl, tp); 2203 break; 2204 } 2205 } 2206 #ifdef TCPDEBUG 2207 if (so->so_options & SO_DEBUG) 2208 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2209 &tcp_savetcp, 0); 2210 #endif 2211 2212 /* 2213 * Return any desired output. 2214 */ 2215 if (needoutput || (tp->t_flags & TF_ACKNOW)) 2216 (void) tcp_output(tp); 2217 INP_UNLOCK(inp); 2218 return; 2219 2220 dropafterack: 2221 /* 2222 * Generate an ACK dropping incoming segment if it occupies 2223 * sequence space, where the ACK reflects our state. 2224 * 2225 * We can now skip the test for the RST flag since all 2226 * paths to this code happen after packets containing 2227 * RST have been dropped. 2228 * 2229 * In the SYN-RECEIVED state, don't send an ACK unless the 2230 * segment we received passes the SYN-RECEIVED ACK test. 2231 * If it fails send a RST. This breaks the loop in the 2232 * "LAND" DoS attack, and also prevents an ACK storm 2233 * between two listening ports that have been sent forged 2234 * SYN segments, each with the source address of the other. 2235 */ 2236 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2237 (SEQ_GT(tp->snd_una, th->th_ack) || 2238 SEQ_GT(th->th_ack, tp->snd_max)) ) { 2239 rstreason = BANDLIM_RST_OPENPORT; 2240 goto dropwithreset; 2241 } 2242 #ifdef TCPDEBUG 2243 if (so->so_options & SO_DEBUG) 2244 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2245 &tcp_savetcp, 0); 2246 #endif 2247 if (headlocked) 2248 INP_INFO_WUNLOCK(&tcbinfo); 2249 m_freem(m); 2250 tp->t_flags |= TF_ACKNOW; 2251 (void) tcp_output(tp); 2252 INP_UNLOCK(inp); 2253 return; 2254 2255 dropwithreset: 2256 /* 2257 * Generate a RST, dropping incoming segment. 2258 * Make ACK acceptable to originator of segment. 2259 * Don't bother to respond if destination was broadcast/multicast. 2260 */ 2261 if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2262 goto drop; 2263 if (isipv6) { 2264 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2265 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2266 goto drop; 2267 } else { 2268 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2269 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2270 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2271 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2272 goto drop; 2273 } 2274 /* IPv6 anycast check is done at tcp6_input() */ 2275 2276 /* 2277 * Perform bandwidth limiting. 2278 */ 2279 if (badport_bandlim(rstreason) < 0) 2280 goto drop; 2281 2282 #ifdef TCPDEBUG 2283 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2284 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2285 &tcp_savetcp, 0); 2286 #endif 2287 2288 if (tp) 2289 INP_UNLOCK(inp); 2290 2291 if (thflags & TH_ACK) 2292 /* mtod() below is safe as long as hdr dropping is delayed */ 2293 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2294 TH_RST); 2295 else { 2296 if (thflags & TH_SYN) 2297 tlen++; 2298 /* mtod() below is safe as long as hdr dropping is delayed */ 2299 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2300 (tcp_seq)0, TH_RST|TH_ACK); 2301 } 2302 if (headlocked) 2303 INP_INFO_WUNLOCK(&tcbinfo); 2304 return; 2305 2306 drop: 2307 /* 2308 * Drop space held by incoming segment and return. 2309 */ 2310 #ifdef TCPDEBUG 2311 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2312 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2313 &tcp_savetcp, 0); 2314 #endif 2315 if (tp) 2316 INP_UNLOCK(inp); 2317 m_freem(m); 2318 if (headlocked) 2319 INP_INFO_WUNLOCK(&tcbinfo); 2320 return; 2321 } 2322 2323 /* 2324 * Parse TCP options and place in tcpopt. 2325 */ 2326 static void 2327 tcp_dooptions(to, cp, cnt, is_syn) 2328 struct tcpopt *to; 2329 u_char *cp; 2330 int cnt; 2331 { 2332 int opt, optlen; 2333 2334 to->to_flags = 0; 2335 for (; cnt > 0; cnt -= optlen, cp += optlen) { 2336 opt = cp[0]; 2337 if (opt == TCPOPT_EOL) 2338 break; 2339 if (opt == TCPOPT_NOP) 2340 optlen = 1; 2341 else { 2342 if (cnt < 2) 2343 break; 2344 optlen = cp[1]; 2345 if (optlen < 2 || optlen > cnt) 2346 break; 2347 } 2348 switch (opt) { 2349 case TCPOPT_MAXSEG: 2350 if (optlen != TCPOLEN_MAXSEG) 2351 continue; 2352 if (!is_syn) 2353 continue; 2354 to->to_flags |= TOF_MSS; 2355 bcopy((char *)cp + 2, 2356 (char *)&to->to_mss, sizeof(to->to_mss)); 2357 to->to_mss = ntohs(to->to_mss); 2358 break; 2359 case TCPOPT_WINDOW: 2360 if (optlen != TCPOLEN_WINDOW) 2361 continue; 2362 if (! is_syn) 2363 continue; 2364 to->to_flags |= TOF_SCALE; 2365 to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2366 break; 2367 case TCPOPT_TIMESTAMP: 2368 if (optlen != TCPOLEN_TIMESTAMP) 2369 continue; 2370 to->to_flags |= TOF_TS; 2371 bcopy((char *)cp + 2, 2372 (char *)&to->to_tsval, sizeof(to->to_tsval)); 2373 to->to_tsval = ntohl(to->to_tsval); 2374 bcopy((char *)cp + 6, 2375 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2376 to->to_tsecr = ntohl(to->to_tsecr); 2377 break; 2378 case TCPOPT_CC: 2379 if (optlen != TCPOLEN_CC) 2380 continue; 2381 to->to_flags |= TOF_CC; 2382 bcopy((char *)cp + 2, 2383 (char *)&to->to_cc, sizeof(to->to_cc)); 2384 to->to_cc = ntohl(to->to_cc); 2385 break; 2386 case TCPOPT_CCNEW: 2387 if (optlen != TCPOLEN_CC) 2388 continue; 2389 if (!is_syn) 2390 continue; 2391 to->to_flags |= TOF_CCNEW; 2392 bcopy((char *)cp + 2, 2393 (char *)&to->to_cc, sizeof(to->to_cc)); 2394 to->to_cc = ntohl(to->to_cc); 2395 break; 2396 case TCPOPT_CCECHO: 2397 if (optlen != TCPOLEN_CC) 2398 continue; 2399 if (!is_syn) 2400 continue; 2401 to->to_flags |= TOF_CCECHO; 2402 bcopy((char *)cp + 2, 2403 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2404 to->to_ccecho = ntohl(to->to_ccecho); 2405 break; 2406 default: 2407 continue; 2408 } 2409 } 2410 } 2411 2412 /* 2413 * Pull out of band byte out of a segment so 2414 * it doesn't appear in the user's data queue. 2415 * It is still reflected in the segment length for 2416 * sequencing purposes. 2417 */ 2418 static void 2419 tcp_pulloutofband(so, th, m, off) 2420 struct socket *so; 2421 struct tcphdr *th; 2422 register struct mbuf *m; 2423 int off; /* delayed to be droped hdrlen */ 2424 { 2425 int cnt = off + th->th_urp - 1; 2426 2427 while (cnt >= 0) { 2428 if (m->m_len > cnt) { 2429 char *cp = mtod(m, caddr_t) + cnt; 2430 struct tcpcb *tp = sototcpcb(so); 2431 2432 tp->t_iobc = *cp; 2433 tp->t_oobflags |= TCPOOB_HAVEDATA; 2434 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2435 m->m_len--; 2436 if (m->m_flags & M_PKTHDR) 2437 m->m_pkthdr.len--; 2438 return; 2439 } 2440 cnt -= m->m_len; 2441 m = m->m_next; 2442 if (m == 0) 2443 break; 2444 } 2445 panic("tcp_pulloutofband"); 2446 } 2447 2448 /* 2449 * Collect new round-trip time estimate 2450 * and update averages and current timeout. 2451 */ 2452 static void 2453 tcp_xmit_timer(tp, rtt) 2454 register struct tcpcb *tp; 2455 int rtt; 2456 { 2457 register int delta; 2458 2459 tcpstat.tcps_rttupdated++; 2460 tp->t_rttupdated++; 2461 if (tp->t_srtt != 0) { 2462 /* 2463 * srtt is stored as fixed point with 5 bits after the 2464 * binary point (i.e., scaled by 8). The following magic 2465 * is equivalent to the smoothing algorithm in rfc793 with 2466 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2467 * point). Adjust rtt to origin 0. 2468 */ 2469 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2470 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2471 2472 if ((tp->t_srtt += delta) <= 0) 2473 tp->t_srtt = 1; 2474 2475 /* 2476 * We accumulate a smoothed rtt variance (actually, a 2477 * smoothed mean difference), then set the retransmit 2478 * timer to smoothed rtt + 4 times the smoothed variance. 2479 * rttvar is stored as fixed point with 4 bits after the 2480 * binary point (scaled by 16). The following is 2481 * equivalent to rfc793 smoothing with an alpha of .75 2482 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2483 * rfc793's wired-in beta. 2484 */ 2485 if (delta < 0) 2486 delta = -delta; 2487 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2488 if ((tp->t_rttvar += delta) <= 0) 2489 tp->t_rttvar = 1; 2490 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 2491 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2492 } else { 2493 /* 2494 * No rtt measurement yet - use the unsmoothed rtt. 2495 * Set the variance to half the rtt (so our first 2496 * retransmit happens at 3*rtt). 2497 */ 2498 tp->t_srtt = rtt << TCP_RTT_SHIFT; 2499 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2500 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2501 } 2502 tp->t_rtttime = 0; 2503 tp->t_rxtshift = 0; 2504 2505 /* 2506 * the retransmit should happen at rtt + 4 * rttvar. 2507 * Because of the way we do the smoothing, srtt and rttvar 2508 * will each average +1/2 tick of bias. When we compute 2509 * the retransmit timer, we want 1/2 tick of rounding and 2510 * 1 extra tick because of +-1/2 tick uncertainty in the 2511 * firing of the timer. The bias will give us exactly the 2512 * 1.5 tick we need. But, because the bias is 2513 * statistical, we have to test that we don't drop below 2514 * the minimum feasible timer (which is 2 ticks). 2515 */ 2516 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 2517 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2518 2519 /* 2520 * We received an ack for a packet that wasn't retransmitted; 2521 * it is probably safe to discard any error indications we've 2522 * received recently. This isn't quite right, but close enough 2523 * for now (a route might have failed after we sent a segment, 2524 * and the return path might not be symmetrical). 2525 */ 2526 tp->t_softerror = 0; 2527 } 2528 2529 /* 2530 * Determine a reasonable value for maxseg size. 2531 * If the route is known, check route for mtu. 2532 * If none, use an mss that can be handled on the outgoing 2533 * interface without forcing IP to fragment; if bigger than 2534 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2535 * to utilize large mbufs. If no route is found, route has no mtu, 2536 * or the destination isn't local, use a default, hopefully conservative 2537 * size (usually 512 or the default IP max size, but no more than the mtu 2538 * of the interface), as we can't discover anything about intervening 2539 * gateways or networks. We also initialize the congestion/slow start 2540 * window to be a single segment if the destination isn't local. 2541 * While looking at the routing entry, we also initialize other path-dependent 2542 * parameters from pre-set or cached values in the routing entry. 2543 * 2544 * Also take into account the space needed for options that we 2545 * send regularly. Make maxseg shorter by that amount to assure 2546 * that we can send maxseg amount of data even when the options 2547 * are present. Store the upper limit of the length of options plus 2548 * data in maxopd. 2549 * 2550 * NOTE that this routine is only called when we process an incoming 2551 * segment, for outgoing segments only tcp_mssopt is called. 2552 * 2553 * In case of T/TCP, we call this routine during implicit connection 2554 * setup as well (offer = -1), to initialize maxseg from the cached 2555 * MSS of our peer. 2556 */ 2557 void 2558 tcp_mss(tp, offer) 2559 struct tcpcb *tp; 2560 int offer; 2561 { 2562 register struct rtentry *rt; 2563 struct ifnet *ifp; 2564 register int rtt, mss; 2565 u_long bufsize; 2566 struct inpcb *inp = tp->t_inpcb; 2567 struct socket *so; 2568 struct rmxp_tao *taop; 2569 int origoffer = offer; 2570 #ifdef INET6 2571 int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2572 size_t min_protoh = isipv6 ? 2573 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2574 sizeof (struct tcpiphdr); 2575 #else 2576 const int isipv6 = 0; 2577 const size_t min_protoh = sizeof (struct tcpiphdr); 2578 #endif 2579 2580 if (isipv6) 2581 rt = tcp_rtlookup6(&inp->inp_inc); 2582 else 2583 rt = tcp_rtlookup(&inp->inp_inc); 2584 if (rt == NULL) { 2585 tp->t_maxopd = tp->t_maxseg = 2586 isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 2587 return; 2588 } 2589 ifp = rt->rt_ifp; 2590 so = inp->inp_socket; 2591 2592 taop = rmx_taop(rt->rt_rmx); 2593 /* 2594 * Offer == -1 means that we didn't receive SYN yet, 2595 * use cached value in that case; 2596 */ 2597 if (offer == -1) 2598 offer = taop->tao_mssopt; 2599 /* 2600 * Offer == 0 means that there was no MSS on the SYN segment, 2601 * in this case we use tcp_mssdflt. 2602 */ 2603 if (offer == 0) 2604 offer = isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 2605 else 2606 /* 2607 * Sanity check: make sure that maxopd will be large 2608 * enough to allow some data on segments even is the 2609 * all the option space is used (40bytes). Otherwise 2610 * funny things may happen in tcp_output. 2611 */ 2612 offer = max(offer, 64); 2613 taop->tao_mssopt = offer; 2614 2615 /* 2616 * While we're here, check if there's an initial rtt 2617 * or rttvar. Convert from the route-table units 2618 * to scaled multiples of the slow timeout timer. 2619 */ 2620 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2621 /* 2622 * XXX the lock bit for RTT indicates that the value 2623 * is also a minimum value; this is subject to time. 2624 */ 2625 if (rt->rt_rmx.rmx_locks & RTV_RTT) 2626 tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 2627 tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 2628 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 2629 tcpstat.tcps_usedrtt++; 2630 if (rt->rt_rmx.rmx_rttvar) { 2631 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2632 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2633 tcpstat.tcps_usedrttvar++; 2634 } else { 2635 /* default variation is +- 1 rtt */ 2636 tp->t_rttvar = 2637 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2638 } 2639 TCPT_RANGESET(tp->t_rxtcur, 2640 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2641 tp->t_rttmin, TCPTV_REXMTMAX); 2642 } 2643 /* 2644 * if there's an mtu associated with the route, use it 2645 * else, use the link mtu. 2646 */ 2647 if (rt->rt_rmx.rmx_mtu) 2648 mss = rt->rt_rmx.rmx_mtu - min_protoh; 2649 else { 2650 if (isipv6) { 2651 mss = nd_ifinfo[rt->rt_ifp->if_index].linkmtu - 2652 min_protoh; 2653 if (!in6_localaddr(&inp->in6p_faddr)) 2654 mss = min(mss, tcp_v6mssdflt); 2655 } else { 2656 mss = ifp->if_mtu - min_protoh; 2657 if (!in_localaddr(inp->inp_faddr)) 2658 mss = min(mss, tcp_mssdflt); 2659 } 2660 } 2661 mss = min(mss, offer); 2662 /* 2663 * maxopd stores the maximum length of data AND options 2664 * in a segment; maxseg is the amount of data in a normal 2665 * segment. We need to store this value (maxopd) apart 2666 * from maxseg, because now every segment carries options 2667 * and thus we normally have somewhat less data in segments. 2668 */ 2669 tp->t_maxopd = mss; 2670 2671 /* 2672 * In case of T/TCP, origoffer==-1 indicates, that no segments 2673 * were received yet. In this case we just guess, otherwise 2674 * we do the same as before T/TCP. 2675 */ 2676 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2677 (origoffer == -1 || 2678 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2679 mss -= TCPOLEN_TSTAMP_APPA; 2680 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2681 (origoffer == -1 || 2682 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2683 mss -= TCPOLEN_CC_APPA; 2684 2685 #if (MCLBYTES & (MCLBYTES - 1)) == 0 2686 if (mss > MCLBYTES) 2687 mss &= ~(MCLBYTES-1); 2688 #else 2689 if (mss > MCLBYTES) 2690 mss = mss / MCLBYTES * MCLBYTES; 2691 #endif 2692 /* 2693 * If there's a pipesize, change the socket buffer 2694 * to that size. Make the socket buffers an integral 2695 * number of mss units; if the mss is larger than 2696 * the socket buffer, decrease the mss. 2697 */ 2698 #ifdef RTV_SPIPE 2699 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2700 #endif 2701 bufsize = so->so_snd.sb_hiwat; 2702 if (bufsize < mss) 2703 mss = bufsize; 2704 else { 2705 bufsize = roundup(bufsize, mss); 2706 if (bufsize > sb_max) 2707 bufsize = sb_max; 2708 if (bufsize > so->so_snd.sb_hiwat) 2709 (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2710 } 2711 tp->t_maxseg = mss; 2712 2713 #ifdef RTV_RPIPE 2714 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2715 #endif 2716 bufsize = so->so_rcv.sb_hiwat; 2717 if (bufsize > mss) { 2718 bufsize = roundup(bufsize, mss); 2719 if (bufsize > sb_max) 2720 bufsize = sb_max; 2721 if (bufsize > so->so_rcv.sb_hiwat) 2722 (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2723 } 2724 2725 /* 2726 * Set the slow-start flight size depending on whether this 2727 * is a local network or not. 2728 */ 2729 if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2730 (!isipv6 && in_localaddr(inp->inp_faddr))) 2731 tp->snd_cwnd = mss * ss_fltsz_local; 2732 else 2733 tp->snd_cwnd = mss * ss_fltsz; 2734 2735 if (rt->rt_rmx.rmx_ssthresh) { 2736 /* 2737 * There's some sort of gateway or interface 2738 * buffer limit on the path. Use this to set 2739 * the slow start threshhold, but set the 2740 * threshold to no less than 2*mss. 2741 */ 2742 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2743 tcpstat.tcps_usedssthresh++; 2744 } 2745 } 2746 2747 /* 2748 * Determine the MSS option to send on an outgoing SYN. 2749 */ 2750 int 2751 tcp_mssopt(tp) 2752 struct tcpcb *tp; 2753 { 2754 struct rtentry *rt; 2755 #ifdef INET6 2756 int isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2757 size_t min_protoh = isipv6 ? 2758 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2759 sizeof (struct tcpiphdr); 2760 #else 2761 const int isipv6 = 0; 2762 const size_t min_protoh = sizeof (struct tcpiphdr); 2763 #endif 2764 2765 if (isipv6) 2766 rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc); 2767 else 2768 rt = tcp_rtlookup(&tp->t_inpcb->inp_inc); 2769 if (rt == NULL) 2770 return (isipv6 ? tcp_v6mssdflt : tcp_mssdflt); 2771 2772 return (rt->rt_ifp->if_mtu - min_protoh); 2773 } 2774 2775 2776 /* 2777 * On a partial ack arrives, force the retransmission of the 2778 * next unacknowledged segment. Do not clear tp->t_dupacks. 2779 * By setting snd_nxt to ti_ack, this forces retransmission timer to 2780 * be started again. 2781 */ 2782 static void 2783 tcp_newreno_partial_ack(tp, th) 2784 struct tcpcb *tp; 2785 struct tcphdr *th; 2786 { 2787 tcp_seq onxt = tp->snd_nxt; 2788 u_long ocwnd = tp->snd_cwnd; 2789 2790 callout_stop(tp->tt_rexmt); 2791 tp->t_rtttime = 0; 2792 tp->snd_nxt = th->th_ack; 2793 /* 2794 * Set snd_cwnd to one segment beyond acknowledged offset. 2795 * (tp->snd_una has not yet been updated when this function is called.) 2796 */ 2797 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 2798 tp->t_flags |= TF_ACKNOW; 2799 (void) tcp_output(tp); 2800 tp->snd_cwnd = ocwnd; 2801 if (SEQ_GT(onxt, tp->snd_nxt)) 2802 tp->snd_nxt = onxt; 2803 /* 2804 * Partial window deflation. Relies on fact that tp->snd_una 2805 * not updated yet. 2806 */ 2807 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 2808 } 2809