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