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