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