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