1 /* $OpenBSD: tcp_input.c,v 1.111 2002/05/16 14:10:51 kjc Exp $ */ 2 /* $NetBSD: tcp_input.c,v 1.23 1996/02/13 23:43:44 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 37 * 38 * NRL grants permission for redistribution and use in source and binary 39 * forms, with or without modification, of the software and documentation 40 * created at NRL provided that the following conditions are met: 41 * 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgements: 49 * This product includes software developed by the University of 50 * California, Berkeley and its contributors. 51 * This product includes software developed at the Information 52 * Technology Division, US Naval Research Laboratory. 53 * 4. Neither the name of the NRL nor the names of its contributors 54 * may be used to endorse or promote products derived from this software 55 * without specific prior written permission. 56 * 57 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 58 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 59 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 60 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 61 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 62 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 63 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 64 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 65 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 66 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 67 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 68 * 69 * The views and conclusions contained in the software and documentation 70 * are those of the authors and should not be interpreted as representing 71 * official policies, either expressed or implied, of the US Naval 72 * Research Laboratory (NRL). 73 */ 74 75 #ifndef TUBA_INCLUDE 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/mbuf.h> 79 #include <sys/protosw.h> 80 #include <sys/socket.h> 81 #include <sys/socketvar.h> 82 #include <sys/kernel.h> 83 84 #include <net/if.h> 85 #include <net/route.h> 86 87 #include <netinet/in.h> 88 #include <netinet/in_systm.h> 89 #include <netinet/ip.h> 90 #include <netinet/in_pcb.h> 91 #include <netinet/ip_var.h> 92 #include <netinet/tcp.h> 93 #include <netinet/tcp_fsm.h> 94 #include <netinet/tcp_seq.h> 95 #include <netinet/tcp_timer.h> 96 #include <netinet/tcp_var.h> 97 #include <netinet/tcpip.h> 98 #include <netinet/tcp_debug.h> 99 100 #ifdef INET6 101 #include <netinet6/in6_var.h> 102 #include <netinet6/nd6.h> 103 104 struct tcpiphdr tcp_saveti; 105 struct tcpipv6hdr tcp_saveti6; 106 107 /* for the packet header length in the mbuf */ 108 #define M_PH_LEN(m) (((struct mbuf *)(m))->m_pkthdr.len) 109 #define M_V6_LEN(m) (M_PH_LEN(m) - sizeof(struct ip6_hdr)) 110 #define M_V4_LEN(m) (M_PH_LEN(m) - sizeof(struct ip)) 111 #endif /* INET6 */ 112 113 int tcprexmtthresh = 3; 114 struct tcpiphdr tcp_saveti; 115 int tcptv_keep_init = TCPTV_KEEP_INIT; 116 117 extern u_long sb_max; 118 119 int tcp_rst_ppslim = 100; /* 100pps */ 120 int tcp_rst_ppslim_count = 0; 121 struct timeval tcp_rst_ppslim_last; 122 123 #endif /* TUBA_INCLUDE */ 124 #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ) 125 126 /* for modulo comparisons of timestamps */ 127 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0) 128 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0) 129 130 /* 131 * Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. 132 */ 133 #ifdef INET6 134 #define ND6_HINT(tp) \ 135 do { \ 136 if (tp && tp->t_inpcb && (tp->t_inpcb->inp_flags & INP_IPV6) && \ 137 tp->t_inpcb->inp_route6.ro_rt) { \ 138 nd6_nud_hint(tp->t_inpcb->inp_route6.ro_rt, NULL, 0); \ 139 } \ 140 } while (0) 141 #else 142 #define ND6_HINT(tp) 143 #endif 144 145 #ifdef TCP_ECN 146 /* 147 * ECN (Explicit Congestion Notification) support based on RFC3168 148 * implementation note: 149 * snd_last is used to track a recovery phase. 150 * when cwnd is reduced, snd_last is set to snd_max. 151 * while snd_last > snd_una, the sender is in a recovery phase and 152 * its cwnd should not be reduced again. 153 * snd_last follows snd_una when not in a recovery phase. 154 */ 155 #endif 156 157 /* 158 * Macro to compute ACK transmission behavior. Delay the ACK unless 159 * we have already delayed an ACK (must send an ACK every two segments). 160 * We also ACK immediately if we received a PUSH and the ACK-on-PUSH 161 * option is enabled. 162 */ 163 #define TCP_SETUP_ACK(tp, tiflags) \ 164 do { \ 165 if ((tp)->t_flags & TF_DELACK || \ 166 (tcp_ack_on_push && (tiflags) & TH_PUSH)) \ 167 tp->t_flags |= TF_ACKNOW; \ 168 else \ 169 TCP_SET_DELACK(tp); \ 170 } while (0) 171 172 /* 173 * Insert segment ti into reassembly queue of tcp with 174 * control block tp. Return TH_FIN if reassembly now includes 175 * a segment with FIN. The macro form does the common case inline 176 * (segment is the next to be received on an established connection, 177 * and the queue is empty), avoiding linkage into and removal 178 * from the queue and repetition of various conversions. 179 * Set DELACK for segments received in order, but ack immediately 180 * when segments are out of order (so fast retransmit can work). 181 */ 182 183 #ifndef TUBA_INCLUDE 184 185 int 186 tcp_reass(tp, th, m, tlen) 187 struct tcpcb *tp; 188 struct tcphdr *th; 189 struct mbuf *m; 190 int *tlen; 191 { 192 struct ipqent *p, *q, *nq, *tiqe; 193 struct socket *so = tp->t_inpcb->inp_socket; 194 int flags; 195 196 /* 197 * Call with th==0 after become established to 198 * force pre-ESTABLISHED data up to user socket. 199 */ 200 if (th == 0) 201 goto present; 202 203 /* 204 * Allocate a new queue entry, before we throw away any data. 205 * If we can't, just drop the packet. XXX 206 */ 207 tiqe = pool_get(&ipqent_pool, PR_NOWAIT); 208 if (tiqe == NULL) { 209 tcpstat.tcps_rcvmemdrop++; 210 m_freem(m); 211 return (0); 212 } 213 214 /* 215 * Find a segment which begins after this one does. 216 */ 217 for (p = NULL, q = tp->segq.lh_first; q != NULL; 218 p = q, q = q->ipqe_q.le_next) 219 if (SEQ_GT(q->ipqe_tcp->th_seq, th->th_seq)) 220 break; 221 222 /* 223 * If there is a preceding segment, it may provide some of 224 * our data already. If so, drop the data from the incoming 225 * segment. If it provides all of our data, drop us. 226 */ 227 if (p != NULL) { 228 struct tcphdr *phdr = p->ipqe_tcp; 229 int i; 230 231 /* conversion to int (in i) handles seq wraparound */ 232 i = phdr->th_seq + phdr->th_reseqlen - th->th_seq; 233 if (i > 0) { 234 if (i >= *tlen) { 235 tcpstat.tcps_rcvduppack++; 236 tcpstat.tcps_rcvdupbyte += *tlen; 237 m_freem(m); 238 pool_put(&ipqent_pool, tiqe); 239 return (0); 240 } 241 m_adj(m, i); 242 *tlen -= i; 243 th->th_seq += i; 244 } 245 } 246 tcpstat.tcps_rcvoopack++; 247 tcpstat.tcps_rcvoobyte += *tlen; 248 249 /* 250 * While we overlap succeeding segments trim them or, 251 * if they are completely covered, dequeue them. 252 */ 253 for (; q != NULL; q = nq) { 254 struct tcphdr *qhdr = q->ipqe_tcp; 255 int i = (th->th_seq + *tlen) - qhdr->th_seq; 256 257 if (i <= 0) 258 break; 259 if (i < qhdr->th_reseqlen) { 260 qhdr->th_seq += i; 261 qhdr->th_reseqlen -= i; 262 m_adj(q->ipqe_m, i); 263 break; 264 } 265 nq = q->ipqe_q.le_next; 266 m_freem(q->ipqe_m); 267 LIST_REMOVE(q, ipqe_q); 268 pool_put(&ipqent_pool, q); 269 } 270 271 /* Insert the new fragment queue entry into place. */ 272 tiqe->ipqe_m = m; 273 th->th_reseqlen = *tlen; 274 tiqe->ipqe_tcp = th; 275 if (p == NULL) { 276 LIST_INSERT_HEAD(&tp->segq, tiqe, ipqe_q); 277 } else { 278 LIST_INSERT_AFTER(p, tiqe, ipqe_q); 279 } 280 281 present: 282 /* 283 * Present data to user, advancing rcv_nxt through 284 * completed sequence space. 285 */ 286 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 287 return (0); 288 q = tp->segq.lh_first; 289 if (q == NULL || q->ipqe_tcp->th_seq != tp->rcv_nxt) 290 return (0); 291 if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_tcp->th_reseqlen) 292 return (0); 293 do { 294 tp->rcv_nxt += q->ipqe_tcp->th_reseqlen; 295 flags = q->ipqe_tcp->th_flags & TH_FIN; 296 297 nq = q->ipqe_q.le_next; 298 LIST_REMOVE(q, ipqe_q); 299 ND6_HINT(tp); 300 if (so->so_state & SS_CANTRCVMORE) 301 m_freem(q->ipqe_m); 302 else 303 sbappend(&so->so_rcv, q->ipqe_m); 304 pool_put(&ipqent_pool, q); 305 q = nq; 306 } while (q != NULL && q->ipqe_tcp->th_seq == tp->rcv_nxt); 307 sorwakeup(so); 308 return (flags); 309 } 310 311 /* 312 * First check for a port-specific bomb. We do not want to drop half-opens 313 * for other ports if this is the only port being bombed. We only check 314 * the bottom 40 half open connections, to avoid wasting too much time. 315 * 316 * Or, otherwise it is more likely a generic syn bomb, so delete the oldest 317 * half-open connection. 318 */ 319 void 320 tcpdropoldhalfopen(avoidtp, port) 321 struct tcpcb *avoidtp; 322 u_int16_t port; 323 { 324 struct inpcb *inp; 325 struct tcpcb *tp; 326 int ncheck = 40; 327 int s; 328 329 s = splnet(); 330 inp = tcbtable.inpt_queue.cqh_first; 331 if (inp) /* XXX */ 332 for (; inp != (struct inpcb *)&tcbtable.inpt_queue && --ncheck; 333 inp = inp->inp_queue.cqe_prev) { 334 if ((tp = (struct tcpcb *)inp->inp_ppcb) && 335 tp != avoidtp && 336 tp->t_state == TCPS_SYN_RECEIVED && 337 port == inp->inp_lport) { 338 tcp_close(tp); 339 goto done; 340 } 341 } 342 343 inp = tcbtable.inpt_queue.cqh_first; 344 if (inp) /* XXX */ 345 for (; inp != (struct inpcb *)&tcbtable.inpt_queue; 346 inp = inp->inp_queue.cqe_prev) { 347 if ((tp = (struct tcpcb *)inp->inp_ppcb) && 348 tp != avoidtp && 349 tp->t_state == TCPS_SYN_RECEIVED) { 350 tcp_close(tp); 351 goto done; 352 } 353 } 354 done: 355 splx(s); 356 } 357 358 #ifdef INET6 359 int 360 tcp6_input(mp, offp, proto) 361 struct mbuf **mp; 362 int *offp, proto; 363 { 364 struct mbuf *m = *mp; 365 366 #if defined(NFAITH) && 0 < NFAITH 367 if (m->m_pkthdr.rcvif) { 368 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 369 /* XXX send icmp6 host/port unreach? */ 370 m_freem(m); 371 return IPPROTO_DONE; 372 } 373 } 374 #endif 375 376 /* 377 * draft-itojun-ipv6-tcp-to-anycast 378 * better place to put this in? 379 */ 380 if (m->m_flags & M_ANYCAST6) { 381 if (m->m_len >= sizeof(struct ip6_hdr)) { 382 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 383 icmp6_error(m, ICMP6_DST_UNREACH, 384 ICMP6_DST_UNREACH_ADDR, 385 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 386 } else 387 m_freem(m); 388 return IPPROTO_DONE; 389 } 390 391 tcp_input(m, *offp, proto); 392 return IPPROTO_DONE; 393 } 394 #endif 395 396 /* 397 * TCP input routine, follows pages 65-76 of the 398 * protocol specification dated September, 1981 very closely. 399 */ 400 void 401 tcp_input(struct mbuf *m, ...) 402 { 403 struct ip *ip; 404 struct inpcb *inp; 405 caddr_t optp = NULL; 406 int optlen = 0; 407 int len, tlen, off; 408 struct tcpcb *tp = 0; 409 int tiflags; 410 struct socket *so = NULL; 411 int todrop, acked, ourfinisacked, needoutput = 0; 412 int hdroptlen = 0; 413 short ostate = 0; 414 struct in_addr laddr; 415 int dropsocket = 0; 416 int iss = 0; 417 u_long tiwin; 418 u_int32_t ts_val, ts_ecr; 419 int ts_present = 0; 420 int iphlen; 421 va_list ap; 422 struct tcphdr *th; 423 #ifdef INET6 424 struct in6_addr laddr6; 425 struct ip6_hdr *ipv6 = NULL; 426 #endif /* INET6 */ 427 #ifdef IPSEC 428 struct m_tag *mtag; 429 struct tdb_ident *tdbi; 430 struct tdb *tdb; 431 int error, s; 432 #endif /* IPSEC */ 433 int af; 434 #ifdef TCP_ECN 435 u_char iptos; 436 #endif 437 438 va_start(ap, m); 439 iphlen = va_arg(ap, int); 440 va_end(ap); 441 442 tcpstat.tcps_rcvtotal++; 443 444 /* 445 * Before we do ANYTHING, we have to figure out if it's TCP/IPv6 or 446 * TCP/IPv4. 447 */ 448 switch (mtod(m, struct ip *)->ip_v) { 449 #ifdef INET6 450 case 6: 451 af = AF_INET6; 452 break; 453 #endif 454 case 4: 455 af = AF_INET; 456 break; 457 default: 458 m_freem(m); 459 return; /*EAFNOSUPPORT*/ 460 } 461 462 /* 463 * Get IP and TCP header together in first mbuf. 464 * Note: IP leaves IP header in first mbuf. 465 */ 466 switch (af) { 467 case AF_INET: 468 #ifdef DIAGNOSTIC 469 if (iphlen < sizeof(struct ip)) { 470 m_freem(m); 471 return; 472 } 473 #endif /* DIAGNOSTIC */ 474 if (iphlen > sizeof(struct ip)) { 475 #if 0 /*XXX*/ 476 ip_stripoptions(m, (struct mbuf *)0); 477 iphlen = sizeof(struct ip); 478 #else 479 m_freem(m); 480 return; 481 #endif 482 } 483 break; 484 #ifdef INET6 485 case AF_INET6: 486 #ifdef DIAGNOSTIC 487 if (iphlen < sizeof(struct ip6_hdr)) { 488 m_freem(m); 489 return; 490 } 491 #endif /* DIAGNOSTIC */ 492 if (iphlen > sizeof(struct ip6_hdr)) { 493 #if 0 /*XXX*/ 494 ipv6_stripoptions(m, iphlen); 495 iphlen = sizeof(struct ip6_hdr); 496 #else 497 m_freem(m); 498 return; 499 #endif 500 } 501 break; 502 #endif 503 default: 504 m_freem(m); 505 return; 506 } 507 508 if (m->m_len < iphlen + sizeof(struct tcphdr)) { 509 m = m_pullup2(m, iphlen + sizeof(struct tcphdr)); 510 if (m == NULL) { 511 tcpstat.tcps_rcvshort++; 512 return; 513 } 514 } 515 516 ip = NULL; 517 #ifdef INET6 518 ipv6 = NULL; 519 #endif 520 switch (af) { 521 case AF_INET: 522 { 523 struct tcpiphdr *ti; 524 525 ip = mtod(m, struct ip *); 526 #if 1 527 tlen = m->m_pkthdr.len - iphlen; 528 #else 529 tlen = ((struct ip *)ti)->ip_len; 530 #endif 531 ti = mtod(m, struct tcpiphdr *); 532 533 #ifdef TCP_ECN 534 /* save ip_tos before clearing it for checksum */ 535 iptos = ip->ip_tos; 536 #endif 537 /* 538 * Checksum extended TCP header and data. 539 */ 540 len = sizeof(struct ip) + tlen; 541 bzero(ti->ti_x1, sizeof ti->ti_x1); 542 ti->ti_len = (u_int16_t)tlen; 543 HTONS(ti->ti_len); 544 if ((m->m_pkthdr.csum & M_TCP_CSUM_IN_OK) == 0) { 545 if (m->m_pkthdr.csum & M_TCP_CSUM_IN_BAD) { 546 tcpstat.tcps_inhwcsum++; 547 tcpstat.tcps_rcvbadsum++; 548 goto drop; 549 } 550 if ((ti->ti_sum = in_cksum(m, len)) != 0) { 551 tcpstat.tcps_rcvbadsum++; 552 goto drop; 553 } 554 } else { 555 m->m_pkthdr.csum &= ~M_TCP_CSUM_IN_OK; 556 tcpstat.tcps_inhwcsum++; 557 } 558 break; 559 } 560 #ifdef INET6 561 case AF_INET6: 562 ipv6 = mtod(m, struct ip6_hdr *); 563 tlen = m->m_pkthdr.len - iphlen; 564 #ifdef TCP_ECN 565 iptos = (ntohl(ipv6->ip6_flow) >> 20) & 0xff; 566 #endif 567 568 /* Be proactive about malicious use of IPv4 mapped address */ 569 if (IN6_IS_ADDR_V4MAPPED(&ipv6->ip6_src) || 570 IN6_IS_ADDR_V4MAPPED(&ipv6->ip6_dst)) { 571 /* XXX stat */ 572 goto drop; 573 } 574 575 /* 576 * Be proactive about unspecified IPv6 address in source. 577 * As we use all-zero to indicate unbounded/unconnected pcb, 578 * unspecified IPv6 address can be used to confuse us. 579 * 580 * Note that packets with unspecified IPv6 destination is 581 * already dropped in ip6_input. 582 */ 583 if (IN6_IS_ADDR_UNSPECIFIED(&ipv6->ip6_src)) { 584 /* XXX stat */ 585 goto drop; 586 } 587 588 /* 589 * Checksum extended TCP header and data. 590 */ 591 if (in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), tlen)) { 592 tcpstat.tcps_rcvbadsum++; 593 goto drop; 594 } 595 break; 596 #endif 597 } 598 #endif /* TUBA_INCLUDE */ 599 600 th = (struct tcphdr *)(mtod(m, caddr_t) + iphlen); 601 602 /* 603 * Check that TCP offset makes sense, 604 * pull out TCP options and adjust length. XXX 605 */ 606 off = th->th_off << 2; 607 if (off < sizeof(struct tcphdr) || off > tlen) { 608 tcpstat.tcps_rcvbadoff++; 609 goto drop; 610 } 611 tlen -= off; 612 if (off > sizeof(struct tcphdr)) { 613 if (m->m_len < iphlen + off) { 614 if ((m = m_pullup2(m, iphlen + off)) == NULL) { 615 tcpstat.tcps_rcvshort++; 616 return; 617 } 618 switch (af) { 619 case AF_INET: 620 ip = mtod(m, struct ip *); 621 break; 622 #ifdef INET6 623 case AF_INET6: 624 ipv6 = mtod(m, struct ip6_hdr *); 625 break; 626 #endif 627 } 628 th = (struct tcphdr *)(mtod(m, caddr_t) + iphlen); 629 } 630 optlen = off - sizeof(struct tcphdr); 631 optp = mtod(m, caddr_t) + iphlen + sizeof(struct tcphdr); 632 /* 633 * Do quick retrieval of timestamp options ("options 634 * prediction?"). If timestamp is the only option and it's 635 * formatted as recommended in RFC 1323 appendix A, we 636 * quickly get the values now and not bother calling 637 * tcp_dooptions(), etc. 638 */ 639 if ((optlen == TCPOLEN_TSTAMP_APPA || 640 (optlen > TCPOLEN_TSTAMP_APPA && 641 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && 642 *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) && 643 (th->th_flags & TH_SYN) == 0) { 644 ts_present = 1; 645 ts_val = ntohl(*(u_int32_t *)(optp + 4)); 646 ts_ecr = ntohl(*(u_int32_t *)(optp + 8)); 647 optp = NULL; /* we've parsed the options */ 648 } 649 } 650 tiflags = th->th_flags; 651 652 /* 653 * Convert TCP protocol specific fields to host format. 654 */ 655 NTOHL(th->th_seq); 656 NTOHL(th->th_ack); 657 NTOHS(th->th_win); 658 NTOHS(th->th_urp); 659 660 /* 661 * Locate pcb for segment. 662 */ 663 findpcb: 664 switch (af) { 665 #ifdef INET6 666 case AF_INET6: 667 inp = in6_pcbhashlookup(&tcbtable, &ipv6->ip6_src, th->th_sport, 668 &ipv6->ip6_dst, th->th_dport); 669 break; 670 #endif 671 case AF_INET: 672 inp = in_pcbhashlookup(&tcbtable, ip->ip_src, th->th_sport, 673 ip->ip_dst, th->th_dport); 674 break; 675 } 676 if (inp == 0) { 677 ++tcpstat.tcps_pcbhashmiss; 678 switch (af) { 679 #ifdef INET6 680 case AF_INET6: 681 inp = in_pcblookup(&tcbtable, &ipv6->ip6_src, 682 th->th_sport, &ipv6->ip6_dst, th->th_dport, 683 INPLOOKUP_WILDCARD | INPLOOKUP_IPV6); 684 break; 685 #endif /* INET6 */ 686 case AF_INET: 687 inp = in_pcblookup(&tcbtable, &ip->ip_src, th->th_sport, 688 &ip->ip_dst, th->th_dport, INPLOOKUP_WILDCARD); 689 break; 690 } 691 /* 692 * If the state is CLOSED (i.e., TCB does not exist) then 693 * all data in the incoming segment is discarded. 694 * If the TCB exists but is in CLOSED state, it is embryonic, 695 * but should either do a listen or a connect soon. 696 */ 697 if (inp == 0) { 698 ++tcpstat.tcps_noport; 699 goto dropwithreset_ratelim; 700 } 701 } 702 703 tp = intotcpcb(inp); 704 if (tp == 0) 705 goto dropwithreset_ratelim; 706 if (tp->t_state == TCPS_CLOSED) 707 goto drop; 708 709 /* Unscale the window into a 32-bit value. */ 710 if ((tiflags & TH_SYN) == 0) 711 tiwin = th->th_win << tp->snd_scale; 712 else 713 tiwin = th->th_win; 714 715 so = inp->inp_socket; 716 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 717 if (so->so_options & SO_DEBUG) { 718 ostate = tp->t_state; 719 switch (af) { 720 #ifdef INET6 721 case AF_INET6: 722 tcp_saveti6 = *(mtod(m, struct tcpipv6hdr *)); 723 break; 724 #endif 725 case AF_INET: 726 tcp_saveti = *(mtod(m, struct tcpiphdr *)); 727 break; 728 } 729 } 730 if (so->so_options & SO_ACCEPTCONN) { 731 struct socket *so1; 732 733 so1 = sonewconn(so, 0); 734 if (so1 == NULL) { 735 tcpdropoldhalfopen(tp, th->th_dport); 736 so1 = sonewconn(so, 0); 737 if (so1 == NULL) 738 goto drop; 739 } 740 so = so1; 741 /* 742 * This is ugly, but .... 743 * 744 * Mark socket as temporary until we're 745 * committed to keeping it. The code at 746 * ``drop'' and ``dropwithreset'' check the 747 * flag dropsocket to see if the temporary 748 * socket created here should be discarded. 749 * We mark the socket as discardable until 750 * we're committed to it below in TCPS_LISTEN. 751 */ 752 dropsocket++; 753 #ifdef IPSEC 754 /* 755 * We need to copy the required security levels 756 * from the old pcb. Ditto for any other 757 * IPsec-related information. 758 */ 759 { 760 struct inpcb *newinp = (struct inpcb *)so->so_pcb; 761 bcopy(inp->inp_seclevel, newinp->inp_seclevel, 762 sizeof(inp->inp_seclevel)); 763 newinp->inp_secrequire = inp->inp_secrequire; 764 if (inp->inp_ipsec_localid != NULL) { 765 newinp->inp_ipsec_localid = inp->inp_ipsec_localid; 766 inp->inp_ipsec_localid->ref_count++; 767 } 768 if (inp->inp_ipsec_remoteid != NULL) { 769 newinp->inp_ipsec_remoteid = inp->inp_ipsec_remoteid; 770 inp->inp_ipsec_remoteid->ref_count++; 771 } 772 if (inp->inp_ipsec_localcred != NULL) { 773 newinp->inp_ipsec_localcred = inp->inp_ipsec_localcred; 774 inp->inp_ipsec_localcred->ref_count++; 775 } 776 if (inp->inp_ipsec_remotecred != NULL) { 777 newinp->inp_ipsec_remotecred = inp->inp_ipsec_remotecred; 778 inp->inp_ipsec_remotecred->ref_count++; 779 } 780 if (inp->inp_ipsec_localauth != NULL) { 781 newinp->inp_ipsec_localauth 782 = inp->inp_ipsec_localauth; 783 inp->inp_ipsec_localauth->ref_count++; 784 } 785 if (inp->inp_ipsec_remoteauth != NULL) { 786 newinp->inp_ipsec_remoteauth 787 = inp->inp_ipsec_remoteauth; 788 inp->inp_ipsec_remoteauth->ref_count++; 789 } 790 } 791 #endif /* IPSEC */ 792 #ifdef INET6 793 /* 794 * inp still has the OLD in_pcb stuff, set the 795 * v6-related flags on the new guy, too. This is 796 * done particularly for the case where an AF_INET6 797 * socket is bound only to a port, and a v4 connection 798 * comes in on that port. 799 * we also copy the flowinfo from the original pcb 800 * to the new one. 801 */ 802 { 803 int flags = inp->inp_flags; 804 struct inpcb *oldinpcb = inp; 805 806 inp = (struct inpcb *)so->so_pcb; 807 inp->inp_flags |= (flags & INP_IPV6); 808 if ((inp->inp_flags & INP_IPV6) != 0) { 809 inp->inp_ipv6.ip6_hlim = 810 oldinpcb->inp_ipv6.ip6_hlim; 811 inp->inp_ipv6.ip6_flow = 812 oldinpcb->inp_ipv6.ip6_flow; 813 } 814 } 815 #else /* INET6 */ 816 inp = (struct inpcb *)so->so_pcb; 817 #endif /* INET6 */ 818 inp->inp_lport = th->th_dport; 819 switch (af) { 820 #ifdef INET6 821 case AF_INET6: 822 inp->inp_laddr6 = ipv6->ip6_dst; 823 824 /*inp->inp_options = ip6_srcroute();*/ /* soon. */ 825 /* 826 * still need to tweak outbound options 827 * processing to include this mbuf in 828 * the right place and put the correct 829 * NextHdr values in the right places. 830 * XXX rja 831 */ 832 break; 833 #endif /* INET6 */ 834 case AF_INET: 835 inp->inp_laddr = ip->ip_dst; 836 inp->inp_options = ip_srcroute(); 837 break; 838 } 839 in_pcbrehash(inp); 840 tp = intotcpcb(inp); 841 tp->t_state = TCPS_LISTEN; 842 843 /* Compute proper scaling value from buffer space */ 844 tcp_rscale(tp, so->so_rcv.sb_hiwat); 845 } 846 } 847 848 #ifdef IPSEC 849 /* Find most recent IPsec tag */ 850 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 851 s = splnet(); 852 if (mtag != NULL) { 853 tdbi = (struct tdb_ident *)(mtag + 1); 854 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto); 855 } else 856 tdb = NULL; 857 ipsp_spd_lookup(m, af, iphlen, &error, IPSP_DIRECTION_IN, 858 tdb, inp); 859 860 /* Latch SA */ 861 if (inp->inp_tdb_in != tdb) { 862 if (tdb) { 863 tdb_add_inp(tdb, inp, 1); 864 if (inp->inp_ipsec_remoteid == NULL && 865 tdb->tdb_srcid != NULL) { 866 inp->inp_ipsec_remoteid = tdb->tdb_srcid; 867 tdb->tdb_srcid->ref_count++; 868 } 869 if (inp->inp_ipsec_remotecred == NULL && 870 tdb->tdb_remote_cred != NULL) { 871 inp->inp_ipsec_remotecred = 872 tdb->tdb_remote_cred; 873 tdb->tdb_remote_cred->ref_count++; 874 } 875 if (inp->inp_ipsec_remoteauth == NULL && 876 tdb->tdb_remote_auth != NULL) { 877 inp->inp_ipsec_remoteauth = 878 tdb->tdb_remote_auth; 879 tdb->tdb_remote_auth->ref_count++; 880 } 881 } else { /* Just reset */ 882 TAILQ_REMOVE(&inp->inp_tdb_in->tdb_inp_in, inp, 883 inp_tdb_in_next); 884 inp->inp_tdb_in = NULL; 885 } 886 } 887 splx(s); 888 889 /* Error or otherwise drop-packet indication */ 890 if (error) 891 goto drop; 892 #endif /* IPSEC */ 893 894 /* 895 * Segment received on connection. 896 * Reset idle time and keep-alive timer. 897 */ 898 tp->t_rcvtime = tcp_now; 899 if (tp->t_state != TCPS_SYN_RECEIVED) 900 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 901 902 #ifdef TCP_SACK 903 if (!tp->sack_disable) 904 tcp_del_sackholes(tp, th); /* Delete stale SACK holes */ 905 #endif /* TCP_SACK */ 906 907 /* 908 * Process options if not in LISTEN state, 909 * else do it below (after getting remote address). 910 */ 911 if (optp && tp->t_state != TCPS_LISTEN) 912 tcp_dooptions(tp, optp, optlen, th, 913 &ts_present, &ts_val, &ts_ecr); 914 915 #ifdef TCP_SACK 916 if (!tp->sack_disable) { 917 tp->rcv_laststart = th->th_seq; /* last rec'vd segment*/ 918 tp->rcv_lastend = th->th_seq + tlen; 919 } 920 #endif /* TCP_SACK */ 921 #ifdef TCP_ECN 922 /* if congestion experienced, set ECE bit in subsequent packets. */ 923 if ((iptos & IPTOS_ECN_MASK) == IPTOS_ECN_CE) { 924 tp->t_flags |= TF_RCVD_CE; 925 tcpstat.tcps_ecn_rcvce++; 926 } 927 #endif 928 /* 929 * Header prediction: check for the two common cases 930 * of a uni-directional data xfer. If the packet has 931 * no control flags, is in-sequence, the window didn't 932 * change and we're not retransmitting, it's a 933 * candidate. If the length is zero and the ack moved 934 * forward, we're the sender side of the xfer. Just 935 * free the data acked & wake any higher level process 936 * that was blocked waiting for space. If the length 937 * is non-zero and the ack didn't move, we're the 938 * receiver side. If we're getting packets in-order 939 * (the reassembly queue is empty), add the data to 940 * the socket buffer and note that we need a delayed ack. 941 */ 942 if (tp->t_state == TCPS_ESTABLISHED && 943 #ifdef TCP_ECN 944 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ECE|TH_CWR|TH_ACK)) == TH_ACK && 945 #else 946 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 947 #endif 948 (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && 949 th->th_seq == tp->rcv_nxt && 950 tiwin && tiwin == tp->snd_wnd && 951 tp->snd_nxt == tp->snd_max) { 952 953 /* 954 * If last ACK falls within this segment's sequence numbers, 955 * record the timestamp. 956 * Fix from Braden, see Stevens p. 870 957 */ 958 if (ts_present && SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 959 tp->ts_recent_age = tcp_now; 960 tp->ts_recent = ts_val; 961 } 962 963 if (tlen == 0) { 964 if (SEQ_GT(th->th_ack, tp->snd_una) && 965 SEQ_LEQ(th->th_ack, tp->snd_max) && 966 tp->snd_cwnd >= tp->snd_wnd && 967 tp->t_dupacks == 0) { 968 /* 969 * this is a pure ack for outstanding data. 970 */ 971 ++tcpstat.tcps_predack; 972 if (ts_present) 973 tcp_xmit_timer(tp, tcp_now-ts_ecr+1); 974 else if (tp->t_rtttime && 975 SEQ_GT(th->th_ack, tp->t_rtseq)) 976 tcp_xmit_timer(tp, 977 tcp_now - tp->t_rtttime); 978 acked = th->th_ack - tp->snd_una; 979 tcpstat.tcps_rcvackpack++; 980 tcpstat.tcps_rcvackbyte += acked; 981 ND6_HINT(tp); 982 sbdrop(&so->so_snd, acked); 983 tp->snd_una = th->th_ack; 984 #if defined(TCP_SACK) || defined(TCP_ECN) 985 /* 986 * We want snd_last to track snd_una so 987 * as to avoid sequence wraparound problems 988 * for very large transfers. 989 */ 990 #ifdef TCP_ECN 991 if (SEQ_GT(tp->snd_una, tp->snd_last)) 992 #endif 993 tp->snd_last = tp->snd_una; 994 #endif /* TCP_SACK */ 995 #if defined(TCP_SACK) && defined(TCP_FACK) 996 tp->snd_fack = tp->snd_una; 997 tp->retran_data = 0; 998 #endif /* TCP_FACK */ 999 m_freem(m); 1000 1001 /* 1002 * If all outstanding data are acked, stop 1003 * retransmit timer, otherwise restart timer 1004 * using current (possibly backed-off) value. 1005 * If process is waiting for space, 1006 * wakeup/selwakeup/signal. If data 1007 * are ready to send, let tcp_output 1008 * decide between more output or persist. 1009 */ 1010 if (tp->snd_una == tp->snd_max) 1011 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1012 else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 1013 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1014 1015 if (sb_notify(&so->so_snd)) 1016 sowwakeup(so); 1017 if (so->so_snd.sb_cc) 1018 (void) tcp_output(tp); 1019 return; 1020 } 1021 } else if (th->th_ack == tp->snd_una && 1022 tp->segq.lh_first == NULL && 1023 tlen <= sbspace(&so->so_rcv)) { 1024 /* 1025 * This is a pure, in-sequence data packet 1026 * with nothing on the reassembly queue and 1027 * we have enough buffer space to take it. 1028 */ 1029 #ifdef TCP_SACK 1030 /* Clean receiver SACK report if present */ 1031 if (!tp->sack_disable && tp->rcv_numsacks) 1032 tcp_clean_sackreport(tp); 1033 #endif /* TCP_SACK */ 1034 ++tcpstat.tcps_preddat; 1035 tp->rcv_nxt += tlen; 1036 tcpstat.tcps_rcvpack++; 1037 tcpstat.tcps_rcvbyte += tlen; 1038 ND6_HINT(tp); 1039 /* 1040 * Drop TCP, IP headers and TCP options then add data 1041 * to socket buffer. 1042 */ 1043 m_adj(m, iphlen + off); 1044 sbappend(&so->so_rcv, m); 1045 sorwakeup(so); 1046 TCP_SETUP_ACK(tp, tiflags); 1047 if (tp->t_flags & TF_ACKNOW) 1048 (void) tcp_output(tp); 1049 return; 1050 } 1051 } 1052 1053 /* 1054 * Compute mbuf offset to TCP data segment. 1055 */ 1056 hdroptlen = iphlen + off; 1057 1058 /* 1059 * Calculate amount of space in receive window, 1060 * and then do TCP input processing. 1061 * Receive window is amount of space in rcv queue, 1062 * but not less than advertised window. 1063 */ 1064 { int win; 1065 1066 win = sbspace(&so->so_rcv); 1067 if (win < 0) 1068 win = 0; 1069 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1070 } 1071 1072 switch (tp->t_state) { 1073 1074 /* 1075 * If the state is LISTEN then ignore segment if it contains an RST. 1076 * If the segment contains an ACK then it is bad and send a RST. 1077 * If it does not contain a SYN then it is not interesting; drop it. 1078 * If it is from this socket, drop it, it must be forged. 1079 * Don't bother responding if the destination was a broadcast. 1080 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1081 * tp->iss, and send a segment: 1082 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1083 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1084 * Fill in remote peer address fields if not previously specified. 1085 * Enter SYN_RECEIVED state, and process any other fields of this 1086 * segment in this state. 1087 */ 1088 case TCPS_LISTEN: { 1089 struct mbuf *am; 1090 struct sockaddr_in *sin; 1091 #ifdef INET6 1092 struct sockaddr_in6 *sin6; 1093 #endif /* INET6 */ 1094 1095 if (tiflags & TH_RST) 1096 goto drop; 1097 if (tiflags & TH_ACK) 1098 goto dropwithreset; 1099 if ((tiflags & TH_SYN) == 0) 1100 goto drop; 1101 if (th->th_dport == th->th_sport) { 1102 switch (af) { 1103 #ifdef INET6 1104 case AF_INET6: 1105 if (IN6_ARE_ADDR_EQUAL(&ipv6->ip6_src, 1106 &ipv6->ip6_dst)) 1107 goto drop; 1108 break; 1109 #endif /* INET6 */ 1110 case AF_INET: 1111 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 1112 goto drop; 1113 break; 1114 } 1115 } 1116 1117 /* 1118 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 1119 */ 1120 if (m->m_flags & (M_BCAST|M_MCAST)) 1121 goto drop; 1122 switch (af) { 1123 #ifdef INET6 1124 case AF_INET6: 1125 if (IN6_IS_ADDR_MULTICAST(&ipv6->ip6_dst)) 1126 goto drop; 1127 break; 1128 #endif /* INET6 */ 1129 case AF_INET: 1130 if (IN_MULTICAST(ip->ip_dst.s_addr) || 1131 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 1132 goto drop; 1133 break; 1134 } 1135 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 1136 if (am == NULL) 1137 goto drop; 1138 switch (af) { 1139 #ifdef INET6 1140 case AF_INET6: 1141 /* 1142 * This is probably the place to set the tp->pf value. 1143 * (Don't forget to do it in the v4 code as well!) 1144 * 1145 * Also, remember to blank out things like flowlabel, or 1146 * set flowlabel for accepted sockets in v6. 1147 * 1148 * FURTHERMORE, this is PROBABLY the place where the 1149 * whole business of key munging is set up for passive 1150 * connections. 1151 */ 1152 am->m_len = sizeof(struct sockaddr_in6); 1153 sin6 = mtod(am, struct sockaddr_in6 *); 1154 sin6->sin6_family = AF_INET6; 1155 sin6->sin6_len = sizeof(struct sockaddr_in6); 1156 sin6->sin6_addr = ipv6->ip6_src; 1157 sin6->sin6_port = th->th_sport; 1158 sin6->sin6_flowinfo = htonl(0x0fffffff) & 1159 inp->inp_ipv6.ip6_flow; 1160 laddr6 = inp->inp_laddr6; 1161 if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6)) 1162 inp->inp_laddr6 = ipv6->ip6_dst; 1163 /* This is a good optimization. */ 1164 if (in6_pcbconnect(inp, am)) { 1165 inp->inp_laddr6 = laddr6; 1166 (void) m_free(am); 1167 goto drop; 1168 } 1169 break; 1170 #endif 1171 case AF_INET: 1172 /* drop IPv4 packet to AF_INET6 socket */ 1173 if (inp->inp_flags & INP_IPV6) { 1174 (void) m_free(am); 1175 goto drop; 1176 } 1177 am->m_len = sizeof(struct sockaddr_in); 1178 sin = mtod(am, struct sockaddr_in *); 1179 sin->sin_family = AF_INET; 1180 sin->sin_len = sizeof(*sin); 1181 sin->sin_addr = ip->ip_src; 1182 sin->sin_port = th->th_sport; 1183 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 1184 laddr = inp->inp_laddr; 1185 if (inp->inp_laddr.s_addr == INADDR_ANY) 1186 inp->inp_laddr = ip->ip_dst; 1187 if (in_pcbconnect(inp, am)) { 1188 inp->inp_laddr = laddr; 1189 (void) m_free(am); 1190 goto drop; 1191 } 1192 (void) m_free(am); 1193 break; 1194 } 1195 tp->t_template = tcp_template(tp); 1196 if (tp->t_template == 0) { 1197 tp = tcp_drop(tp, ENOBUFS); 1198 dropsocket = 0; /* socket is already gone */ 1199 goto drop; 1200 } 1201 if (optp) 1202 tcp_dooptions(tp, optp, optlen, th, 1203 &ts_present, &ts_val, &ts_ecr); 1204 #ifdef TCP_SACK 1205 /* 1206 * If peer did not send a SACK_PERMITTED option (i.e., if 1207 * tcp_dooptions() did not set TF_SACK_PERMIT), set 1208 * sack_disable to 1 if it is currently 0. 1209 */ 1210 if (!tp->sack_disable) 1211 if ((tp->t_flags & TF_SACK_PERMIT) == 0) 1212 tp->sack_disable = 1; 1213 #endif 1214 1215 if (iss) 1216 tp->iss = iss; 1217 else { 1218 #ifdef TCP_COMPAT_42 1219 tcp_iss += TCP_ISSINCR/2; 1220 tp->iss = tcp_iss; 1221 #else /* TCP_COMPAT_42 */ 1222 tp->iss = tcp_rndiss_next(); 1223 #endif /* !TCP_COMPAT_42 */ 1224 } 1225 tp->irs = th->th_seq; 1226 tcp_sendseqinit(tp); 1227 #if defined (TCP_SACK) || defined(TCP_ECN) 1228 tp->snd_last = tp->snd_una; 1229 #endif /* TCP_SACK */ 1230 #if defined(TCP_SACK) && defined(TCP_FACK) 1231 tp->snd_fack = tp->snd_una; 1232 tp->retran_data = 0; 1233 tp->snd_awnd = 0; 1234 #endif /* TCP_FACK */ 1235 #ifdef TCP_ECN 1236 /* 1237 * if both ECE and CWR flag bits are set, peer is ECN capable. 1238 */ 1239 if (tcp_do_ecn && 1240 (tiflags & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 1241 tp->t_flags |= TF_ECN_PERMIT; 1242 tcpstat.tcps_ecn_accepts++; 1243 } 1244 #endif 1245 tcp_rcvseqinit(tp); 1246 tp->t_flags |= TF_ACKNOW; 1247 tp->t_state = TCPS_SYN_RECEIVED; 1248 TCP_TIMER_ARM(tp, TCPT_KEEP, tcptv_keep_init); 1249 dropsocket = 0; /* committed to socket */ 1250 tcpstat.tcps_accepts++; 1251 goto trimthenstep6; 1252 } 1253 1254 /* 1255 * If the state is SYN_RECEIVED: 1256 * if seg contains SYN/ACK, send an RST. 1257 * if seg contains an ACK, but not for our SYN/ACK, send an RST 1258 */ 1259 1260 case TCPS_SYN_RECEIVED: 1261 if (tiflags & TH_ACK) { 1262 if (tiflags & TH_SYN) { 1263 tcpstat.tcps_badsyn++; 1264 goto dropwithreset; 1265 } 1266 if (SEQ_LEQ(th->th_ack, tp->snd_una) || 1267 SEQ_GT(th->th_ack, tp->snd_max)) 1268 goto dropwithreset; 1269 } 1270 break; 1271 1272 /* 1273 * If the state is SYN_SENT: 1274 * if seg contains an ACK, but not for our SYN, drop the input. 1275 * if seg contains a RST, then drop the connection. 1276 * if seg does not contain SYN, then drop it. 1277 * Otherwise this is an acceptable SYN segment 1278 * initialize tp->rcv_nxt and tp->irs 1279 * if seg contains ack then advance tp->snd_una 1280 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1281 * arrange for segment to be acked (eventually) 1282 * continue processing rest of data/controls, beginning with URG 1283 */ 1284 case TCPS_SYN_SENT: 1285 if ((tiflags & TH_ACK) && 1286 (SEQ_LEQ(th->th_ack, tp->iss) || 1287 SEQ_GT(th->th_ack, tp->snd_max))) 1288 goto dropwithreset; 1289 if (tiflags & TH_RST) { 1290 #ifdef TCP_ECN 1291 /* if ECN is enabled, fall back to non-ecn at rexmit */ 1292 if (tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN)) 1293 goto drop; 1294 #endif 1295 if (tiflags & TH_ACK) 1296 tp = tcp_drop(tp, ECONNREFUSED); 1297 goto drop; 1298 } 1299 if ((tiflags & TH_SYN) == 0) 1300 goto drop; 1301 if (tiflags & TH_ACK) { 1302 tp->snd_una = th->th_ack; 1303 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1304 tp->snd_nxt = tp->snd_una; 1305 } 1306 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1307 tp->irs = th->th_seq; 1308 tcp_rcvseqinit(tp); 1309 tp->t_flags |= TF_ACKNOW; 1310 #ifdef TCP_SACK 1311 /* 1312 * If we've sent a SACK_PERMITTED option, and the peer 1313 * also replied with one, then TF_SACK_PERMIT should have 1314 * been set in tcp_dooptions(). If it was not, disable SACKs. 1315 */ 1316 if (!tp->sack_disable) 1317 if ((tp->t_flags & TF_SACK_PERMIT) == 0) 1318 tp->sack_disable = 1; 1319 #endif 1320 #ifdef TCP_ECN 1321 /* 1322 * if ECE is set but CWR is not set for SYN-ACK, or 1323 * both ECE and CWR are set for simultaneous open, 1324 * peer is ECN capable. 1325 */ 1326 if (tcp_do_ecn) { 1327 if ((tiflags & (TH_ACK|TH_ECE|TH_CWR)) 1328 == (TH_ACK|TH_ECE) || 1329 (tiflags & (TH_ACK|TH_ECE|TH_CWR)) 1330 == (TH_ECE|TH_CWR)) { 1331 tp->t_flags |= TF_ECN_PERMIT; 1332 tiflags &= ~(TH_ECE|TH_CWR); 1333 tcpstat.tcps_ecn_accepts++; 1334 } 1335 } 1336 #endif 1337 1338 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 1339 tcpstat.tcps_connects++; 1340 soisconnected(so); 1341 tp->t_state = TCPS_ESTABLISHED; 1342 /* Do window scaling on this connection? */ 1343 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1344 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1345 tp->snd_scale = tp->requested_s_scale; 1346 tp->rcv_scale = tp->request_r_scale; 1347 } 1348 (void) tcp_reass(tp, (struct tcphdr *)0, 1349 (struct mbuf *)0, &tlen); 1350 /* 1351 * if we didn't have to retransmit the SYN, 1352 * use its rtt as our initial srtt & rtt var. 1353 */ 1354 if (tp->t_rtttime) 1355 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 1356 /* 1357 * Since new data was acked (the SYN), open the 1358 * congestion window by one MSS. We do this 1359 * here, because we won't go through the normal 1360 * ACK processing below. And since this is the 1361 * start of the connection, we know we are in 1362 * the exponential phase of slow-start. 1363 */ 1364 tp->snd_cwnd += tp->t_maxseg; 1365 } else 1366 tp->t_state = TCPS_SYN_RECEIVED; 1367 1368 trimthenstep6: 1369 /* 1370 * Advance th->th_seq to correspond to first data byte. 1371 * If data, trim to stay within window, 1372 * dropping FIN if necessary. 1373 */ 1374 th->th_seq++; 1375 if (tlen > tp->rcv_wnd) { 1376 todrop = tlen - tp->rcv_wnd; 1377 m_adj(m, -todrop); 1378 tlen = tp->rcv_wnd; 1379 tiflags &= ~TH_FIN; 1380 tcpstat.tcps_rcvpackafterwin++; 1381 tcpstat.tcps_rcvbyteafterwin += todrop; 1382 } 1383 tp->snd_wl1 = th->th_seq - 1; 1384 tp->rcv_up = th->th_seq; 1385 goto step6; 1386 } 1387 1388 /* 1389 * States other than LISTEN or SYN_SENT. 1390 * First check timestamp, if present. 1391 * Then check that at least some bytes of segment are within 1392 * receive window. If segment begins before rcv_nxt, 1393 * drop leading data (and SYN); if nothing left, just ack. 1394 * 1395 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1396 * and it's less than ts_recent, drop it. 1397 */ 1398 if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && 1399 TSTMP_LT(ts_val, tp->ts_recent)) { 1400 1401 /* Check to see if ts_recent is over 24 days old. */ 1402 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1403 /* 1404 * Invalidate ts_recent. If this segment updates 1405 * ts_recent, the age will be reset later and ts_recent 1406 * will get a valid value. If it does not, setting 1407 * ts_recent to zero will at least satisfy the 1408 * requirement that zero be placed in the timestamp 1409 * echo reply when ts_recent isn't valid. The 1410 * age isn't reset until we get a valid ts_recent 1411 * because we don't want out-of-order segments to be 1412 * dropped when ts_recent is old. 1413 */ 1414 tp->ts_recent = 0; 1415 } else { 1416 tcpstat.tcps_rcvduppack++; 1417 tcpstat.tcps_rcvdupbyte += tlen; 1418 tcpstat.tcps_pawsdrop++; 1419 goto dropafterack; 1420 } 1421 } 1422 1423 todrop = tp->rcv_nxt - th->th_seq; 1424 if (todrop > 0) { 1425 if (tiflags & TH_SYN) { 1426 tiflags &= ~TH_SYN; 1427 th->th_seq++; 1428 if (th->th_urp > 1) 1429 th->th_urp--; 1430 else 1431 tiflags &= ~TH_URG; 1432 todrop--; 1433 } 1434 if (todrop >= tlen || 1435 (todrop == tlen && (tiflags & TH_FIN) == 0)) { 1436 /* 1437 * Any valid FIN must be to the left of the 1438 * window. At this point, FIN must be a 1439 * duplicate or out-of-sequence, so drop it. 1440 */ 1441 tiflags &= ~TH_FIN; 1442 /* 1443 * Send ACK to resynchronize, and drop any data, 1444 * but keep on processing for RST or ACK. 1445 */ 1446 tp->t_flags |= TF_ACKNOW; 1447 tcpstat.tcps_rcvdupbyte += todrop = tlen; 1448 tcpstat.tcps_rcvduppack++; 1449 } else { 1450 tcpstat.tcps_rcvpartduppack++; 1451 tcpstat.tcps_rcvpartdupbyte += todrop; 1452 } 1453 hdroptlen += todrop; /* drop from head afterwards */ 1454 th->th_seq += todrop; 1455 tlen -= todrop; 1456 if (th->th_urp > todrop) 1457 th->th_urp -= todrop; 1458 else { 1459 tiflags &= ~TH_URG; 1460 th->th_urp = 0; 1461 } 1462 } 1463 1464 /* 1465 * If new data are received on a connection after the 1466 * user processes are gone, then RST the other end. 1467 */ 1468 if ((so->so_state & SS_NOFDREF) && 1469 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1470 tp = tcp_close(tp); 1471 tcpstat.tcps_rcvafterclose++; 1472 goto dropwithreset; 1473 } 1474 1475 /* 1476 * If segment ends after window, drop trailing data 1477 * (and PUSH and FIN); if nothing left, just ACK. 1478 */ 1479 todrop = (th->th_seq + tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1480 if (todrop > 0) { 1481 tcpstat.tcps_rcvpackafterwin++; 1482 if (todrop >= tlen) { 1483 tcpstat.tcps_rcvbyteafterwin += tlen; 1484 /* 1485 * If a new connection request is received 1486 * while in TIME_WAIT, drop the old connection 1487 * and start over if the sequence numbers 1488 * are above the previous ones. 1489 */ 1490 if (tiflags & TH_SYN && 1491 tp->t_state == TCPS_TIME_WAIT && 1492 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1493 iss = tp->snd_nxt + TCP_ISSINCR; 1494 tp = tcp_close(tp); 1495 goto findpcb; 1496 } 1497 /* 1498 * If window is closed can only take segments at 1499 * window edge, and have to drop data and PUSH from 1500 * incoming segments. Continue processing, but 1501 * remember to ack. Otherwise, drop segment 1502 * and ack. 1503 */ 1504 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1505 tp->t_flags |= TF_ACKNOW; 1506 tcpstat.tcps_rcvwinprobe++; 1507 } else 1508 goto dropafterack; 1509 } else 1510 tcpstat.tcps_rcvbyteafterwin += todrop; 1511 m_adj(m, -todrop); 1512 tlen -= todrop; 1513 tiflags &= ~(TH_PUSH|TH_FIN); 1514 } 1515 1516 /* 1517 * If last ACK falls within this segment's sequence numbers, 1518 * record its timestamp. 1519 * Fix from Braden, see Stevens p. 870 1520 */ 1521 if (ts_present && TSTMP_GEQ(ts_val, tp->ts_recent) && 1522 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1523 tp->ts_recent_age = tcp_now; 1524 tp->ts_recent = ts_val; 1525 } 1526 1527 /* 1528 * If the RST bit is set examine the state: 1529 * SYN_RECEIVED STATE: 1530 * If passive open, return to LISTEN state. 1531 * If active open, inform user that connection was refused. 1532 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 1533 * Inform user that connection was reset, and close tcb. 1534 * CLOSING, LAST_ACK, TIME_WAIT STATES 1535 * Close the tcb. 1536 */ 1537 if (tiflags & TH_RST) { 1538 if (th->th_seq != tp->last_ack_sent) 1539 goto drop; 1540 1541 switch (tp->t_state) { 1542 case TCPS_SYN_RECEIVED: 1543 #ifdef TCP_ECN 1544 /* if ECN is enabled, fall back to non-ecn at rexmit */ 1545 if (tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN)) 1546 goto drop; 1547 #endif 1548 so->so_error = ECONNREFUSED; 1549 goto close; 1550 1551 case TCPS_ESTABLISHED: 1552 case TCPS_FIN_WAIT_1: 1553 case TCPS_FIN_WAIT_2: 1554 case TCPS_CLOSE_WAIT: 1555 so->so_error = ECONNRESET; 1556 close: 1557 tp->t_state = TCPS_CLOSED; 1558 tcpstat.tcps_drops++; 1559 tp = tcp_close(tp); 1560 goto drop; 1561 case TCPS_CLOSING: 1562 case TCPS_LAST_ACK: 1563 case TCPS_TIME_WAIT: 1564 tp = tcp_close(tp); 1565 goto drop; 1566 } 1567 } 1568 1569 /* 1570 * If a SYN is in the window, then this is an 1571 * error and we send an RST and drop the connection. 1572 */ 1573 if (tiflags & TH_SYN) { 1574 tp = tcp_drop(tp, ECONNRESET); 1575 goto dropwithreset; 1576 } 1577 1578 /* 1579 * If the ACK bit is off we drop the segment and return. 1580 */ 1581 if ((tiflags & TH_ACK) == 0) { 1582 if (tp->t_flags & TF_ACKNOW) 1583 goto dropafterack; 1584 else 1585 goto drop; 1586 } 1587 1588 /* 1589 * Ack processing. 1590 */ 1591 switch (tp->t_state) { 1592 1593 /* 1594 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1595 * ESTABLISHED state and continue processing. 1596 * The ACK was checked above. 1597 */ 1598 case TCPS_SYN_RECEIVED: 1599 tcpstat.tcps_connects++; 1600 soisconnected(so); 1601 tp->t_state = TCPS_ESTABLISHED; 1602 /* Do window scaling? */ 1603 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1604 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1605 tp->snd_scale = tp->requested_s_scale; 1606 tp->rcv_scale = tp->request_r_scale; 1607 } 1608 (void) tcp_reass(tp, (struct tcphdr *)0, (struct mbuf *)0, 1609 &tlen); 1610 tp->snd_wl1 = th->th_seq - 1; 1611 /* fall into ... */ 1612 1613 /* 1614 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1615 * ACKs. If the ack is in the range 1616 * tp->snd_una < th->th_ack <= tp->snd_max 1617 * then advance tp->snd_una to th->th_ack and drop 1618 * data from the retransmission queue. If this ACK reflects 1619 * more up to date window information we update our window information. 1620 */ 1621 case TCPS_ESTABLISHED: 1622 case TCPS_FIN_WAIT_1: 1623 case TCPS_FIN_WAIT_2: 1624 case TCPS_CLOSE_WAIT: 1625 case TCPS_CLOSING: 1626 case TCPS_LAST_ACK: 1627 case TCPS_TIME_WAIT: 1628 #ifdef TCP_ECN 1629 /* 1630 * if we receive ECE and are not already in recovery phase, 1631 * reduce cwnd by half but don't slow-start. 1632 * advance snd_last to snd_max not to reduce cwnd again 1633 * until all outstanding packets are acked. 1634 */ 1635 if (tcp_do_ecn && (tiflags & TH_ECE)) { 1636 if ((tp->t_flags & TF_ECN_PERMIT) && 1637 SEQ_GEQ(tp->snd_una, tp->snd_last)) { 1638 u_int win; 1639 1640 win = min(tp->snd_wnd, tp->snd_cwnd) / tp->t_maxseg; 1641 if (win > 1) { 1642 tp->snd_ssthresh = win / 2 * tp->t_maxseg; 1643 tp->snd_cwnd = tp->snd_ssthresh; 1644 tp->snd_last = tp->snd_max; 1645 tp->t_flags |= TF_SEND_CWR; 1646 tcpstat.tcps_cwr_ecn++; 1647 } 1648 } 1649 tcpstat.tcps_ecn_rcvece++; 1650 } 1651 /* 1652 * if we receive CWR, we know that the peer has reduced 1653 * its congestion window. stop sending ecn-echo. 1654 */ 1655 if ((tiflags & TH_CWR)) { 1656 tp->t_flags &= ~TF_RCVD_CE; 1657 tcpstat.tcps_ecn_rcvcwr++; 1658 } 1659 #endif /* TCP_ECN */ 1660 1661 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1662 /* 1663 * Duplicate/old ACK processing. 1664 * Increments t_dupacks: 1665 * Pure duplicate (same seq/ack/window, no data) 1666 * Doesn't affect t_dupacks: 1667 * Data packets. 1668 * Normal window updates (window opens) 1669 * Resets t_dupacks: 1670 * New data ACKed. 1671 * Window shrinks 1672 * Old ACK 1673 */ 1674 if (tlen) 1675 break; 1676 /* 1677 * If we get an old ACK, there is probably packet 1678 * reordering going on. Be conservative and reset 1679 * t_dupacks so that we are less agressive in 1680 * doing a fast retransmit. 1681 */ 1682 if (th->th_ack != tp->snd_una) { 1683 tp->t_dupacks = 0; 1684 break; 1685 } 1686 if (tiwin == tp->snd_wnd) { 1687 tcpstat.tcps_rcvdupack++; 1688 /* 1689 * If we have outstanding data (other than 1690 * a window probe), this is a completely 1691 * duplicate ack (ie, window info didn't 1692 * change), the ack is the biggest we've 1693 * seen and we've seen exactly our rexmt 1694 * threshhold of them, assume a packet 1695 * has been dropped and retransmit it. 1696 * Kludge snd_nxt & the congestion 1697 * window so we send only this one 1698 * packet. 1699 * 1700 * We know we're losing at the current 1701 * window size so do congestion avoidance 1702 * (set ssthresh to half the current window 1703 * and pull our congestion window back to 1704 * the new ssthresh). 1705 * 1706 * Dup acks mean that packets have left the 1707 * network (they're now cached at the receiver) 1708 * so bump cwnd by the amount in the receiver 1709 * to keep a constant cwnd packets in the 1710 * network. 1711 */ 1712 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0) 1713 tp->t_dupacks = 0; 1714 #if defined(TCP_SACK) && defined(TCP_FACK) 1715 /* 1716 * In FACK, can enter fast rec. if the receiver 1717 * reports a reass. queue longer than 3 segs. 1718 */ 1719 else if (++tp->t_dupacks == tcprexmtthresh || 1720 ((SEQ_GT(tp->snd_fack, tcprexmtthresh * 1721 tp->t_maxseg + tp->snd_una)) && 1722 SEQ_GT(tp->snd_una, tp->snd_last))) { 1723 #else 1724 else if (++tp->t_dupacks == tcprexmtthresh) { 1725 #endif /* TCP_FACK */ 1726 tcp_seq onxt = tp->snd_nxt; 1727 u_long win = 1728 ulmin(tp->snd_wnd, tp->snd_cwnd) / 1729 2 / tp->t_maxseg; 1730 1731 #if defined(TCP_SACK) || defined(TCP_ECN) 1732 if (SEQ_LT(th->th_ack, tp->snd_last)){ 1733 /* 1734 * False fast retx after 1735 * timeout. Do not cut window. 1736 */ 1737 tp->t_dupacks = 0; 1738 goto drop; 1739 } 1740 #endif 1741 if (win < 2) 1742 win = 2; 1743 tp->snd_ssthresh = win * tp->t_maxseg; 1744 #if defined(TCP_SACK) 1745 tp->snd_last = tp->snd_max; 1746 #endif 1747 #ifdef TCP_SACK 1748 if (!tp->sack_disable) { 1749 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1750 tp->t_rtttime = 0; 1751 #ifdef TCP_ECN 1752 tp->t_flags |= TF_SEND_CWR; 1753 #endif 1754 #if 1 /* TCP_ECN */ 1755 tcpstat.tcps_cwr_frecovery++; 1756 #endif 1757 tcpstat.tcps_sndrexmitfast++; 1758 #if defined(TCP_SACK) && defined(TCP_FACK) 1759 tp->t_dupacks = tcprexmtthresh; 1760 (void) tcp_output(tp); 1761 /* 1762 * During FR, snd_cwnd is held 1763 * constant for FACK. 1764 */ 1765 tp->snd_cwnd = tp->snd_ssthresh; 1766 #else 1767 /* 1768 * tcp_output() will send 1769 * oldest SACK-eligible rtx. 1770 */ 1771 (void) tcp_output(tp); 1772 tp->snd_cwnd = tp->snd_ssthresh+ 1773 tp->t_maxseg * tp->t_dupacks; 1774 #endif /* TCP_FACK */ 1775 goto drop; 1776 } 1777 #endif /* TCP_SACK */ 1778 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1779 tp->t_rtttime = 0; 1780 tp->snd_nxt = th->th_ack; 1781 tp->snd_cwnd = tp->t_maxseg; 1782 #ifdef TCP_ECN 1783 tp->t_flags |= TF_SEND_CWR; 1784 #endif 1785 #if 1 /* TCP_ECN */ 1786 tcpstat.tcps_cwr_frecovery++; 1787 #endif 1788 tcpstat.tcps_sndrexmitfast++; 1789 (void) tcp_output(tp); 1790 1791 tp->snd_cwnd = tp->snd_ssthresh + 1792 tp->t_maxseg * tp->t_dupacks; 1793 if (SEQ_GT(onxt, tp->snd_nxt)) 1794 tp->snd_nxt = onxt; 1795 goto drop; 1796 } else if (tp->t_dupacks > tcprexmtthresh) { 1797 #if defined(TCP_SACK) && defined(TCP_FACK) 1798 /* 1799 * while (awnd < cwnd) 1800 * sendsomething(); 1801 */ 1802 if (!tp->sack_disable) { 1803 if (tp->snd_awnd < tp->snd_cwnd) 1804 tcp_output(tp); 1805 goto drop; 1806 } 1807 #endif /* TCP_FACK */ 1808 tp->snd_cwnd += tp->t_maxseg; 1809 (void) tcp_output(tp); 1810 goto drop; 1811 } 1812 } else if (tiwin < tp->snd_wnd) { 1813 /* 1814 * The window was retracted! Previous dup 1815 * ACKs may have been due to packets arriving 1816 * after the shrunken window, not a missing 1817 * packet, so play it safe and reset t_dupacks 1818 */ 1819 tp->t_dupacks = 0; 1820 } 1821 break; 1822 } 1823 /* 1824 * If the congestion window was inflated to account 1825 * for the other side's cached packets, retract it. 1826 */ 1827 #if defined(TCP_SACK) 1828 if (!tp->sack_disable) { 1829 if (tp->t_dupacks >= tcprexmtthresh) { 1830 /* Check for a partial ACK */ 1831 if (tcp_sack_partialack(tp, th)) { 1832 #if defined(TCP_SACK) && defined(TCP_FACK) 1833 /* Force call to tcp_output */ 1834 if (tp->snd_awnd < tp->snd_cwnd) 1835 needoutput = 1; 1836 #else 1837 tp->snd_cwnd += tp->t_maxseg; 1838 needoutput = 1; 1839 #endif /* TCP_FACK */ 1840 } else { 1841 /* Out of fast recovery */ 1842 tp->snd_cwnd = tp->snd_ssthresh; 1843 if (tcp_seq_subtract(tp->snd_max, 1844 th->th_ack) < tp->snd_ssthresh) 1845 tp->snd_cwnd = 1846 tcp_seq_subtract(tp->snd_max, 1847 th->th_ack); 1848 tp->t_dupacks = 0; 1849 #if defined(TCP_SACK) && defined(TCP_FACK) 1850 if (SEQ_GT(th->th_ack, tp->snd_fack)) 1851 tp->snd_fack = th->th_ack; 1852 #endif /* TCP_FACK */ 1853 } 1854 } 1855 } else { 1856 if (tp->t_dupacks >= tcprexmtthresh && 1857 !tcp_newreno(tp, th)) { 1858 /* Out of fast recovery */ 1859 tp->snd_cwnd = tp->snd_ssthresh; 1860 if (tcp_seq_subtract(tp->snd_max, th->th_ack) < 1861 tp->snd_ssthresh) 1862 tp->snd_cwnd = 1863 tcp_seq_subtract(tp->snd_max, 1864 th->th_ack); 1865 tp->t_dupacks = 0; 1866 } 1867 } 1868 if (tp->t_dupacks < tcprexmtthresh) 1869 tp->t_dupacks = 0; 1870 #else /* else no TCP_SACK */ 1871 if (tp->t_dupacks >= tcprexmtthresh && 1872 tp->snd_cwnd > tp->snd_ssthresh) 1873 tp->snd_cwnd = tp->snd_ssthresh; 1874 tp->t_dupacks = 0; 1875 #endif 1876 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1877 tcpstat.tcps_rcvacktoomuch++; 1878 goto dropafterack; 1879 } 1880 acked = th->th_ack - tp->snd_una; 1881 tcpstat.tcps_rcvackpack++; 1882 tcpstat.tcps_rcvackbyte += acked; 1883 1884 /* 1885 * If we have a timestamp reply, update smoothed 1886 * round trip time. If no timestamp is present but 1887 * transmit timer is running and timed sequence 1888 * number was acked, update smoothed round trip time. 1889 * Since we now have an rtt measurement, cancel the 1890 * timer backoff (cf., Phil Karn's retransmit alg.). 1891 * Recompute the initial retransmit timer. 1892 */ 1893 if (ts_present) 1894 tcp_xmit_timer(tp, tcp_now-ts_ecr+1); 1895 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 1896 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 1897 1898 /* 1899 * If all outstanding data is acked, stop retransmit 1900 * timer and remember to restart (more output or persist). 1901 * If there is more data to be acked, restart retransmit 1902 * timer, using current (possibly backed-off) value. 1903 */ 1904 if (th->th_ack == tp->snd_max) { 1905 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1906 needoutput = 1; 1907 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 1908 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1909 /* 1910 * When new data is acked, open the congestion window. 1911 * If the window gives us less than ssthresh packets 1912 * in flight, open exponentially (maxseg per packet). 1913 * Otherwise open linearly: maxseg per window 1914 * (maxseg^2 / cwnd per packet). 1915 */ 1916 { 1917 u_int cw = tp->snd_cwnd; 1918 u_int incr = tp->t_maxseg; 1919 1920 if (cw > tp->snd_ssthresh) 1921 incr = incr * incr / cw; 1922 #if defined (TCP_SACK) 1923 if (tp->t_dupacks < tcprexmtthresh) 1924 #endif 1925 tp->snd_cwnd = ulmin(cw + incr, TCP_MAXWIN<<tp->snd_scale); 1926 } 1927 ND6_HINT(tp); 1928 if (acked > so->so_snd.sb_cc) { 1929 tp->snd_wnd -= so->so_snd.sb_cc; 1930 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1931 ourfinisacked = 1; 1932 } else { 1933 sbdrop(&so->so_snd, acked); 1934 tp->snd_wnd -= acked; 1935 ourfinisacked = 0; 1936 } 1937 if (sb_notify(&so->so_snd)) 1938 sowwakeup(so); 1939 tp->snd_una = th->th_ack; 1940 #ifdef TCP_ECN 1941 /* sync snd_last with snd_una */ 1942 if (SEQ_GT(tp->snd_una, tp->snd_last)) 1943 tp->snd_last = tp->snd_una; 1944 #endif 1945 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1946 tp->snd_nxt = tp->snd_una; 1947 #if defined (TCP_SACK) && defined (TCP_FACK) 1948 if (SEQ_GT(tp->snd_una, tp->snd_fack)) { 1949 tp->snd_fack = tp->snd_una; 1950 /* Update snd_awnd for partial ACK 1951 * without any SACK blocks. 1952 */ 1953 tp->snd_awnd = tcp_seq_subtract(tp->snd_nxt, 1954 tp->snd_fack) + tp->retran_data; 1955 } 1956 #endif 1957 1958 switch (tp->t_state) { 1959 1960 /* 1961 * In FIN_WAIT_1 STATE in addition to the processing 1962 * for the ESTABLISHED state if our FIN is now acknowledged 1963 * then enter FIN_WAIT_2. 1964 */ 1965 case TCPS_FIN_WAIT_1: 1966 if (ourfinisacked) { 1967 /* 1968 * If we can't receive any more 1969 * data, then closing user can proceed. 1970 * Starting the timer is contrary to the 1971 * specification, but if we don't get a FIN 1972 * we'll hang forever. 1973 */ 1974 if (so->so_state & SS_CANTRCVMORE) { 1975 soisdisconnected(so); 1976 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle); 1977 } 1978 tp->t_state = TCPS_FIN_WAIT_2; 1979 } 1980 break; 1981 1982 /* 1983 * In CLOSING STATE in addition to the processing for 1984 * the ESTABLISHED state if the ACK acknowledges our FIN 1985 * then enter the TIME-WAIT state, otherwise ignore 1986 * the segment. 1987 */ 1988 case TCPS_CLOSING: 1989 if (ourfinisacked) { 1990 tp->t_state = TCPS_TIME_WAIT; 1991 tcp_canceltimers(tp); 1992 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 1993 soisdisconnected(so); 1994 } 1995 break; 1996 1997 /* 1998 * In LAST_ACK, we may still be waiting for data to drain 1999 * and/or to be acked, as well as for the ack of our FIN. 2000 * If our FIN is now acknowledged, delete the TCB, 2001 * enter the closed state and return. 2002 */ 2003 case TCPS_LAST_ACK: 2004 if (ourfinisacked) { 2005 tp = tcp_close(tp); 2006 goto drop; 2007 } 2008 break; 2009 2010 /* 2011 * In TIME_WAIT state the only thing that should arrive 2012 * is a retransmission of the remote FIN. Acknowledge 2013 * it and restart the finack timer. 2014 */ 2015 case TCPS_TIME_WAIT: 2016 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2017 goto dropafterack; 2018 } 2019 } 2020 2021 step6: 2022 /* 2023 * Update window information. 2024 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2025 */ 2026 if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) || 2027 (tp->snd_wl1 == th->th_seq && SEQ_LT(tp->snd_wl2, th->th_ack)) || 2028 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))) { 2029 /* keep track of pure window updates */ 2030 if (tlen == 0 && 2031 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2032 tcpstat.tcps_rcvwinupd++; 2033 tp->snd_wnd = tiwin; 2034 tp->snd_wl1 = th->th_seq; 2035 tp->snd_wl2 = th->th_ack; 2036 if (tp->snd_wnd > tp->max_sndwnd) 2037 tp->max_sndwnd = tp->snd_wnd; 2038 needoutput = 1; 2039 } 2040 2041 /* 2042 * Process segments with URG. 2043 */ 2044 if ((tiflags & TH_URG) && th->th_urp && 2045 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2046 /* 2047 * This is a kludge, but if we receive and accept 2048 * random urgent pointers, we'll crash in 2049 * soreceive. It's hard to imagine someone 2050 * actually wanting to send this much urgent data. 2051 */ 2052 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2053 th->th_urp = 0; /* XXX */ 2054 tiflags &= ~TH_URG; /* XXX */ 2055 goto dodata; /* XXX */ 2056 } 2057 /* 2058 * If this segment advances the known urgent pointer, 2059 * then mark the data stream. This should not happen 2060 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2061 * a FIN has been received from the remote side. 2062 * In these states we ignore the URG. 2063 * 2064 * According to RFC961 (Assigned Protocols), 2065 * the urgent pointer points to the last octet 2066 * of urgent data. We continue, however, 2067 * to consider it to indicate the first octet 2068 * of data past the urgent section as the original 2069 * spec states (in one of two places). 2070 */ 2071 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2072 tp->rcv_up = th->th_seq + th->th_urp; 2073 so->so_oobmark = so->so_rcv.sb_cc + 2074 (tp->rcv_up - tp->rcv_nxt) - 1; 2075 if (so->so_oobmark == 0) 2076 so->so_state |= SS_RCVATMARK; 2077 sohasoutofband(so); 2078 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2079 } 2080 /* 2081 * Remove out of band data so doesn't get presented to user. 2082 * This can happen independent of advancing the URG pointer, 2083 * but if two URG's are pending at once, some out-of-band 2084 * data may creep in... ick. 2085 */ 2086 if (th->th_urp <= (u_int16_t) tlen 2087 #ifdef SO_OOBINLINE 2088 && (so->so_options & SO_OOBINLINE) == 0 2089 #endif 2090 ) 2091 tcp_pulloutofband(so, th->th_urp, m, hdroptlen); 2092 } else 2093 /* 2094 * If no out of band data is expected, 2095 * pull receive urgent pointer along 2096 * with the receive window. 2097 */ 2098 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2099 tp->rcv_up = tp->rcv_nxt; 2100 dodata: /* XXX */ 2101 2102 /* 2103 * Process the segment text, merging it into the TCP sequencing queue, 2104 * and arranging for acknowledgment of receipt if necessary. 2105 * This process logically involves adjusting tp->rcv_wnd as data 2106 * is presented to the user (this happens in tcp_usrreq.c, 2107 * case PRU_RCVD). If a FIN has already been received on this 2108 * connection then we just ignore the text. 2109 */ 2110 if ((tlen || (tiflags & TH_FIN)) && 2111 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2112 if (th->th_seq == tp->rcv_nxt && tp->segq.lh_first == NULL && 2113 tp->t_state == TCPS_ESTABLISHED) { 2114 TCP_SETUP_ACK(tp, tiflags); 2115 tp->rcv_nxt += tlen; 2116 tiflags = th->th_flags & TH_FIN; 2117 tcpstat.tcps_rcvpack++; 2118 tcpstat.tcps_rcvbyte += tlen; 2119 ND6_HINT(tp); 2120 m_adj(m, hdroptlen); 2121 sbappend(&so->so_rcv, m); 2122 sorwakeup(so); 2123 } else { 2124 m_adj(m, hdroptlen); 2125 tiflags = tcp_reass(tp, th, m, &tlen); 2126 tp->t_flags |= TF_ACKNOW; 2127 } 2128 #ifdef TCP_SACK 2129 if (!tp->sack_disable) 2130 tcp_update_sack_list(tp); 2131 #endif 2132 2133 /* 2134 * variable len never referenced again in modern BSD, 2135 * so why bother computing it ?? 2136 */ 2137 #if 0 2138 /* 2139 * Note the amount of data that peer has sent into 2140 * our window, in order to estimate the sender's 2141 * buffer size. 2142 */ 2143 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2144 #endif /* 0 */ 2145 } else { 2146 m_freem(m); 2147 tiflags &= ~TH_FIN; 2148 } 2149 2150 /* 2151 * If FIN is received ACK the FIN and let the user know 2152 * that the connection is closing. Ignore a FIN received before 2153 * the connection is fully established. 2154 */ 2155 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) { 2156 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2157 socantrcvmore(so); 2158 tp->t_flags |= TF_ACKNOW; 2159 tp->rcv_nxt++; 2160 } 2161 switch (tp->t_state) { 2162 2163 /* 2164 * In ESTABLISHED STATE enter the CLOSE_WAIT state. 2165 */ 2166 case TCPS_ESTABLISHED: 2167 tp->t_state = TCPS_CLOSE_WAIT; 2168 break; 2169 2170 /* 2171 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2172 * enter the CLOSING state. 2173 */ 2174 case TCPS_FIN_WAIT_1: 2175 tp->t_state = TCPS_CLOSING; 2176 break; 2177 2178 /* 2179 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2180 * starting the time-wait timer, turning off the other 2181 * standard timers. 2182 */ 2183 case TCPS_FIN_WAIT_2: 2184 tp->t_state = TCPS_TIME_WAIT; 2185 tcp_canceltimers(tp); 2186 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2187 soisdisconnected(so); 2188 break; 2189 2190 /* 2191 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2192 */ 2193 case TCPS_TIME_WAIT: 2194 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2195 break; 2196 } 2197 } 2198 if (so->so_options & SO_DEBUG) { 2199 switch (tp->pf == PF_INET6) { 2200 #ifdef INET6 2201 case PF_INET6: 2202 tcp_trace(TA_INPUT, ostate, tp, (caddr_t) &tcp_saveti6, 2203 0, tlen); 2204 break; 2205 #endif /* INET6 */ 2206 case PF_INET: 2207 tcp_trace(TA_INPUT, ostate, tp, (caddr_t) &tcp_saveti, 2208 0, tlen); 2209 break; 2210 } 2211 } 2212 2213 /* 2214 * Return any desired output. 2215 */ 2216 if (needoutput || (tp->t_flags & TF_ACKNOW)) { 2217 (void) tcp_output(tp); 2218 } 2219 return; 2220 2221 dropafterack: 2222 /* 2223 * Generate an ACK dropping incoming segment if it occupies 2224 * sequence space, where the ACK reflects our state. 2225 */ 2226 if (tiflags & TH_RST) 2227 goto drop; 2228 m_freem(m); 2229 tp->t_flags |= TF_ACKNOW; 2230 (void) tcp_output(tp); 2231 return; 2232 2233 dropwithreset_ratelim: 2234 /* 2235 * We may want to rate-limit RSTs in certain situations, 2236 * particularly if we are sending an RST in response to 2237 * an attempt to connect to or otherwise communicate with 2238 * a port for which we have no socket. 2239 */ 2240 if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count, 2241 tcp_rst_ppslim) == 0) { 2242 /* XXX stat */ 2243 goto drop; 2244 } 2245 /* ...fall into dropwithreset... */ 2246 2247 dropwithreset: 2248 /* 2249 * Generate a RST, dropping incoming segment. 2250 * Make ACK acceptable to originator of segment. 2251 * Don't bother to respond if destination was broadcast/multicast. 2252 */ 2253 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2254 goto drop; 2255 switch (af) { 2256 #ifdef INET6 2257 case AF_INET6: 2258 /* For following calls to tcp_respond */ 2259 if (IN6_IS_ADDR_MULTICAST(&ipv6->ip6_dst)) 2260 goto drop; 2261 break; 2262 #endif /* INET6 */ 2263 case AF_INET: 2264 if (IN_MULTICAST(ip->ip_dst.s_addr) || 2265 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2266 goto drop; 2267 } 2268 if (tiflags & TH_ACK) { 2269 tcp_respond(tp, mtod(m, caddr_t), m, (tcp_seq)0, th->th_ack, 2270 TH_RST); 2271 } else { 2272 if (tiflags & TH_SYN) 2273 tlen++; 2274 tcp_respond(tp, mtod(m, caddr_t), m, th->th_seq + tlen, 2275 (tcp_seq)0, TH_RST|TH_ACK); 2276 } 2277 /* destroy temporarily created socket */ 2278 if (dropsocket) 2279 (void) soabort(so); 2280 return; 2281 2282 drop: 2283 /* 2284 * Drop space held by incoming segment and return. 2285 */ 2286 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) { 2287 switch (tp->pf) { 2288 #ifdef INET6 2289 case PF_INET6: 2290 tcp_trace(TA_DROP, ostate, tp, (caddr_t) &tcp_saveti6, 2291 0, tlen); 2292 break; 2293 #endif /* INET6 */ 2294 case PF_INET: 2295 tcp_trace(TA_DROP, ostate, tp, (caddr_t) &tcp_saveti, 2296 0, tlen); 2297 break; 2298 } 2299 } 2300 2301 m_freem(m); 2302 /* destroy temporarily created socket */ 2303 if (dropsocket) 2304 (void) soabort(so); 2305 return; 2306 #ifndef TUBA_INCLUDE 2307 } 2308 2309 void 2310 tcp_dooptions(tp, cp, cnt, th, ts_present, ts_val, ts_ecr) 2311 struct tcpcb *tp; 2312 u_char *cp; 2313 int cnt; 2314 struct tcphdr *th; 2315 int *ts_present; 2316 u_int32_t *ts_val, *ts_ecr; 2317 { 2318 u_int16_t mss = 0; 2319 int opt, optlen; 2320 2321 for (; cnt > 0; cnt -= optlen, cp += optlen) { 2322 opt = cp[0]; 2323 if (opt == TCPOPT_EOL) 2324 break; 2325 if (opt == TCPOPT_NOP) 2326 optlen = 1; 2327 else { 2328 if (cnt < 2) 2329 break; 2330 optlen = cp[1]; 2331 if (optlen < 2 || optlen > cnt) 2332 break; 2333 } 2334 switch (opt) { 2335 2336 default: 2337 continue; 2338 2339 case TCPOPT_MAXSEG: 2340 if (optlen != TCPOLEN_MAXSEG) 2341 continue; 2342 if (!(th->th_flags & TH_SYN)) 2343 continue; 2344 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 2345 NTOHS(mss); 2346 break; 2347 2348 case TCPOPT_WINDOW: 2349 if (optlen != TCPOLEN_WINDOW) 2350 continue; 2351 if (!(th->th_flags & TH_SYN)) 2352 continue; 2353 tp->t_flags |= TF_RCVD_SCALE; 2354 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2355 break; 2356 2357 case TCPOPT_TIMESTAMP: 2358 if (optlen != TCPOLEN_TIMESTAMP) 2359 continue; 2360 *ts_present = 1; 2361 bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val)); 2362 NTOHL(*ts_val); 2363 bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr)); 2364 NTOHL(*ts_ecr); 2365 2366 /* 2367 * A timestamp received in a SYN makes 2368 * it ok to send timestamp requests and replies. 2369 */ 2370 if (th->th_flags & TH_SYN) { 2371 tp->t_flags |= TF_RCVD_TSTMP; 2372 tp->ts_recent = *ts_val; 2373 tp->ts_recent_age = tcp_now; 2374 } 2375 break; 2376 2377 #ifdef TCP_SACK 2378 case TCPOPT_SACK_PERMITTED: 2379 if (tp->sack_disable || optlen!=TCPOLEN_SACK_PERMITTED) 2380 continue; 2381 if (th->th_flags & TH_SYN) 2382 /* MUST only be set on SYN */ 2383 tp->t_flags |= TF_SACK_PERMIT; 2384 break; 2385 case TCPOPT_SACK: 2386 if (tcp_sack_option(tp, th, cp, optlen)) 2387 continue; 2388 break; 2389 #endif 2390 } 2391 } 2392 /* Update t_maxopd and t_maxseg after all options are processed */ 2393 if (th->th_flags & TH_SYN) { 2394 (void) tcp_mss(tp, mss); /* sets t_maxseg */ 2395 2396 if (mss) 2397 tcp_mss_update(tp); 2398 } 2399 } 2400 2401 #if defined(TCP_SACK) 2402 u_long 2403 tcp_seq_subtract(a, b) 2404 u_long a, b; 2405 { 2406 return ((long)(a - b)); 2407 } 2408 #endif 2409 2410 2411 #ifdef TCP_SACK 2412 /* 2413 * This function is called upon receipt of new valid data (while not in header 2414 * prediction mode), and it updates the ordered list of sacks. 2415 */ 2416 void 2417 tcp_update_sack_list(tp) 2418 struct tcpcb *tp; 2419 { 2420 /* 2421 * First reported block MUST be the most recent one. Subsequent 2422 * blocks SHOULD be in the order in which they arrived at the 2423 * receiver. These two conditions make the implementation fully 2424 * compliant with RFC 2018. 2425 */ 2426 int i, j = 0, count = 0, lastpos = -1; 2427 struct sackblk sack, firstsack, temp[MAX_SACK_BLKS]; 2428 2429 /* First clean up current list of sacks */ 2430 for (i = 0; i < tp->rcv_numsacks; i++) { 2431 sack = tp->sackblks[i]; 2432 if (sack.start == 0 && sack.end == 0) { 2433 count++; /* count = number of blocks to be discarded */ 2434 continue; 2435 } 2436 if (SEQ_LEQ(sack.end, tp->rcv_nxt)) { 2437 tp->sackblks[i].start = tp->sackblks[i].end = 0; 2438 count++; 2439 } else { 2440 temp[j].start = tp->sackblks[i].start; 2441 temp[j++].end = tp->sackblks[i].end; 2442 } 2443 } 2444 tp->rcv_numsacks -= count; 2445 if (tp->rcv_numsacks == 0) { /* no sack blocks currently (fast path) */ 2446 tcp_clean_sackreport(tp); 2447 if (SEQ_LT(tp->rcv_nxt, tp->rcv_laststart)) { 2448 /* ==> need first sack block */ 2449 tp->sackblks[0].start = tp->rcv_laststart; 2450 tp->sackblks[0].end = tp->rcv_lastend; 2451 tp->rcv_numsacks = 1; 2452 } 2453 return; 2454 } 2455 /* Otherwise, sack blocks are already present. */ 2456 for (i = 0; i < tp->rcv_numsacks; i++) 2457 tp->sackblks[i] = temp[i]; /* first copy back sack list */ 2458 if (SEQ_GEQ(tp->rcv_nxt, tp->rcv_lastend)) 2459 return; /* sack list remains unchanged */ 2460 /* 2461 * From here, segment just received should be (part of) the 1st sack. 2462 * Go through list, possibly coalescing sack block entries. 2463 */ 2464 firstsack.start = tp->rcv_laststart; 2465 firstsack.end = tp->rcv_lastend; 2466 for (i = 0; i < tp->rcv_numsacks; i++) { 2467 sack = tp->sackblks[i]; 2468 if (SEQ_LT(sack.end, firstsack.start) || 2469 SEQ_GT(sack.start, firstsack.end)) 2470 continue; /* no overlap */ 2471 if (sack.start == firstsack.start && sack.end == firstsack.end){ 2472 /* 2473 * identical block; delete it here since we will 2474 * move it to the front of the list. 2475 */ 2476 tp->sackblks[i].start = tp->sackblks[i].end = 0; 2477 lastpos = i; /* last posn with a zero entry */ 2478 continue; 2479 } 2480 if (SEQ_LEQ(sack.start, firstsack.start)) 2481 firstsack.start = sack.start; /* merge blocks */ 2482 if (SEQ_GEQ(sack.end, firstsack.end)) 2483 firstsack.end = sack.end; /* merge blocks */ 2484 tp->sackblks[i].start = tp->sackblks[i].end = 0; 2485 lastpos = i; /* last posn with a zero entry */ 2486 } 2487 if (lastpos != -1) { /* at least one merge */ 2488 for (i = 0, j = 1; i < tp->rcv_numsacks; i++) { 2489 sack = tp->sackblks[i]; 2490 if (sack.start == 0 && sack.end == 0) 2491 continue; 2492 temp[j++] = sack; 2493 } 2494 tp->rcv_numsacks = j; /* including first blk (added later) */ 2495 for (i = 1; i < tp->rcv_numsacks; i++) /* now copy back */ 2496 tp->sackblks[i] = temp[i]; 2497 } else { /* no merges -- shift sacks by 1 */ 2498 if (tp->rcv_numsacks < MAX_SACK_BLKS) 2499 tp->rcv_numsacks++; 2500 for (i = tp->rcv_numsacks-1; i > 0; i--) 2501 tp->sackblks[i] = tp->sackblks[i-1]; 2502 } 2503 tp->sackblks[0] = firstsack; 2504 return; 2505 } 2506 2507 /* 2508 * Process the TCP SACK option. Returns 1 if tcp_dooptions() should continue, 2509 * and 0 otherwise, if the option was fine. tp->snd_holes is an ordered list 2510 * of holes (oldest to newest, in terms of the sequence space). 2511 */ 2512 int 2513 tcp_sack_option(struct tcpcb *tp, struct tcphdr *th, u_char *cp, int optlen) 2514 { 2515 int tmp_olen; 2516 u_char *tmp_cp; 2517 struct sackhole *cur, *p, *temp; 2518 2519 if (tp->sack_disable) 2520 return (1); 2521 2522 /* Note: TCPOLEN_SACK must be 2*sizeof(tcp_seq) */ 2523 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 2524 return (1); 2525 tmp_cp = cp + 2; 2526 tmp_olen = optlen - 2; 2527 if (tp->snd_numholes < 0) 2528 tp->snd_numholes = 0; 2529 if (tp->t_maxseg == 0) 2530 panic("tcp_sack_option"); /* Should never happen */ 2531 while (tmp_olen > 0) { 2532 struct sackblk sack; 2533 2534 bcopy(tmp_cp, (char *) &(sack.start), sizeof(tcp_seq)); 2535 NTOHL(sack.start); 2536 bcopy(tmp_cp + sizeof(tcp_seq), 2537 (char *) &(sack.end), sizeof(tcp_seq)); 2538 NTOHL(sack.end); 2539 tmp_olen -= TCPOLEN_SACK; 2540 tmp_cp += TCPOLEN_SACK; 2541 if (SEQ_LEQ(sack.end, sack.start)) 2542 continue; /* bad SACK fields */ 2543 if (SEQ_LEQ(sack.end, tp->snd_una)) 2544 continue; /* old block */ 2545 #if defined(TCP_SACK) && defined(TCP_FACK) 2546 /* Updates snd_fack. */ 2547 if (SEQ_GT(sack.end, tp->snd_fack)) 2548 tp->snd_fack = sack.end; 2549 #endif /* TCP_FACK */ 2550 if (SEQ_GT(th->th_ack, tp->snd_una)) { 2551 if (SEQ_LT(sack.start, th->th_ack)) 2552 continue; 2553 } 2554 if (SEQ_GT(sack.end, tp->snd_max)) 2555 continue; 2556 if (tp->snd_holes == NULL) { /* first hole */ 2557 tp->snd_holes = (struct sackhole *) 2558 pool_get(&sackhl_pool, PR_NOWAIT); 2559 if (tp->snd_holes == NULL) { 2560 /* ENOBUFS, so ignore SACKed block for now*/ 2561 continue; 2562 } 2563 cur = tp->snd_holes; 2564 cur->start = th->th_ack; 2565 cur->end = sack.start; 2566 cur->rxmit = cur->start; 2567 cur->next = NULL; 2568 tp->snd_numholes = 1; 2569 tp->rcv_lastsack = sack.end; 2570 /* 2571 * dups is at least one. If more data has been 2572 * SACKed, it can be greater than one. 2573 */ 2574 cur->dups = min(tcprexmtthresh, 2575 ((sack.end - cur->end)/tp->t_maxseg)); 2576 if (cur->dups < 1) 2577 cur->dups = 1; 2578 continue; /* with next sack block */ 2579 } 2580 /* Go thru list of holes: p = previous, cur = current */ 2581 p = cur = tp->snd_holes; 2582 while (cur) { 2583 if (SEQ_LEQ(sack.end, cur->start)) 2584 /* SACKs data before the current hole */ 2585 break; /* no use going through more holes */ 2586 if (SEQ_GEQ(sack.start, cur->end)) { 2587 /* SACKs data beyond the current hole */ 2588 cur->dups++; 2589 if (((sack.end - cur->end)/tp->t_maxseg) >= 2590 tcprexmtthresh) 2591 cur->dups = tcprexmtthresh; 2592 p = cur; 2593 cur = cur->next; 2594 continue; 2595 } 2596 if (SEQ_LEQ(sack.start, cur->start)) { 2597 /* Data acks at least the beginning of hole */ 2598 #if defined(TCP_SACK) && defined(TCP_FACK) 2599 if (SEQ_GT(sack.end, cur->rxmit)) 2600 tp->retran_data -= 2601 tcp_seq_subtract(cur->rxmit, 2602 cur->start); 2603 else 2604 tp->retran_data -= 2605 tcp_seq_subtract(sack.end, 2606 cur->start); 2607 #endif /* TCP_FACK */ 2608 if (SEQ_GEQ(sack.end, cur->end)) { 2609 /* Acks entire hole, so delete hole */ 2610 if (p != cur) { 2611 p->next = cur->next; 2612 pool_put(&sackhl_pool, cur); 2613 cur = p->next; 2614 } else { 2615 cur = cur->next; 2616 pool_put(&sackhl_pool, p); 2617 p = cur; 2618 tp->snd_holes = p; 2619 } 2620 tp->snd_numholes--; 2621 continue; 2622 } 2623 /* otherwise, move start of hole forward */ 2624 cur->start = sack.end; 2625 cur->rxmit = max (cur->rxmit, cur->start); 2626 p = cur; 2627 cur = cur->next; 2628 continue; 2629 } 2630 /* move end of hole backward */ 2631 if (SEQ_GEQ(sack.end, cur->end)) { 2632 #if defined(TCP_SACK) && defined(TCP_FACK) 2633 if (SEQ_GT(cur->rxmit, sack.start)) 2634 tp->retran_data -= 2635 tcp_seq_subtract(cur->rxmit, 2636 sack.start); 2637 #endif /* TCP_FACK */ 2638 cur->end = sack.start; 2639 cur->rxmit = min(cur->rxmit, cur->end); 2640 cur->dups++; 2641 if (((sack.end - cur->end)/tp->t_maxseg) >= 2642 tcprexmtthresh) 2643 cur->dups = tcprexmtthresh; 2644 p = cur; 2645 cur = cur->next; 2646 continue; 2647 } 2648 if (SEQ_LT(cur->start, sack.start) && 2649 SEQ_GT(cur->end, sack.end)) { 2650 /* 2651 * ACKs some data in middle of a hole; need to 2652 * split current hole 2653 */ 2654 temp = (struct sackhole *) 2655 pool_get(&sackhl_pool, PR_NOWAIT); 2656 if (temp == NULL) 2657 continue; /* ENOBUFS */ 2658 #if defined(TCP_SACK) && defined(TCP_FACK) 2659 if (SEQ_GT(cur->rxmit, sack.end)) 2660 tp->retran_data -= 2661 tcp_seq_subtract(sack.end, 2662 sack.start); 2663 else if (SEQ_GT(cur->rxmit, sack.start)) 2664 tp->retran_data -= 2665 tcp_seq_subtract(cur->rxmit, 2666 sack.start); 2667 #endif /* TCP_FACK */ 2668 temp->next = cur->next; 2669 temp->start = sack.end; 2670 temp->end = cur->end; 2671 temp->dups = cur->dups; 2672 temp->rxmit = max(cur->rxmit, temp->start); 2673 cur->end = sack.start; 2674 cur->rxmit = min(cur->rxmit, cur->end); 2675 cur->dups++; 2676 if (((sack.end - cur->end)/tp->t_maxseg) >= 2677 tcprexmtthresh) 2678 cur->dups = tcprexmtthresh; 2679 cur->next = temp; 2680 p = temp; 2681 cur = p->next; 2682 tp->snd_numholes++; 2683 } 2684 } 2685 /* At this point, p points to the last hole on the list */ 2686 if (SEQ_LT(tp->rcv_lastsack, sack.start)) { 2687 /* 2688 * Need to append new hole at end. 2689 * Last hole is p (and it's not NULL). 2690 */ 2691 temp = (struct sackhole *) 2692 pool_get(&sackhl_pool, PR_NOWAIT); 2693 if (temp == NULL) 2694 continue; /* ENOBUFS */ 2695 temp->start = tp->rcv_lastsack; 2696 temp->end = sack.start; 2697 temp->dups = min(tcprexmtthresh, 2698 ((sack.end - sack.start)/tp->t_maxseg)); 2699 if (temp->dups < 1) 2700 temp->dups = 1; 2701 temp->rxmit = temp->start; 2702 temp->next = 0; 2703 p->next = temp; 2704 tp->rcv_lastsack = sack.end; 2705 tp->snd_numholes++; 2706 } 2707 } 2708 #if defined(TCP_SACK) && defined(TCP_FACK) 2709 /* 2710 * Update retran_data and snd_awnd. Go through the list of 2711 * holes. Increment retran_data by (hole->rxmit - hole->start). 2712 */ 2713 tp->retran_data = 0; 2714 cur = tp->snd_holes; 2715 while (cur) { 2716 tp->retran_data += cur->rxmit - cur->start; 2717 cur = cur->next; 2718 } 2719 tp->snd_awnd = tcp_seq_subtract(tp->snd_nxt, tp->snd_fack) + 2720 tp->retran_data; 2721 #endif /* TCP_FACK */ 2722 2723 return (0); 2724 } 2725 2726 /* 2727 * Delete stale (i.e, cumulatively ack'd) holes. Hole is deleted only if 2728 * it is completely acked; otherwise, tcp_sack_option(), called from 2729 * tcp_dooptions(), will fix up the hole. 2730 */ 2731 void 2732 tcp_del_sackholes(tp, th) 2733 struct tcpcb *tp; 2734 struct tcphdr *th; 2735 { 2736 if (!tp->sack_disable && tp->t_state != TCPS_LISTEN) { 2737 /* max because this could be an older ack just arrived */ 2738 tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ? 2739 th->th_ack : tp->snd_una; 2740 struct sackhole *cur = tp->snd_holes; 2741 struct sackhole *prev; 2742 while (cur) 2743 if (SEQ_LEQ(cur->end, lastack)) { 2744 prev = cur; 2745 cur = cur->next; 2746 pool_put(&sackhl_pool, prev); 2747 tp->snd_numholes--; 2748 } else if (SEQ_LT(cur->start, lastack)) { 2749 cur->start = lastack; 2750 if (SEQ_LT(cur->rxmit, cur->start)) 2751 cur->rxmit = cur->start; 2752 break; 2753 } else 2754 break; 2755 tp->snd_holes = cur; 2756 } 2757 } 2758 2759 /* 2760 * Delete all receiver-side SACK information. 2761 */ 2762 void 2763 tcp_clean_sackreport(tp) 2764 struct tcpcb *tp; 2765 { 2766 int i; 2767 2768 tp->rcv_numsacks = 0; 2769 for (i = 0; i < MAX_SACK_BLKS; i++) 2770 tp->sackblks[i].start = tp->sackblks[i].end=0; 2771 2772 } 2773 2774 /* 2775 * Checks for partial ack. If partial ack arrives, turn off retransmission 2776 * timer, deflate the window, do not clear tp->t_dupacks, and return 1. 2777 * If the ack advances at least to tp->snd_last, return 0. 2778 */ 2779 int 2780 tcp_sack_partialack(tp, th) 2781 struct tcpcb *tp; 2782 struct tcphdr *th; 2783 { 2784 if (SEQ_LT(th->th_ack, tp->snd_last)) { 2785 /* Turn off retx. timer (will start again next segment) */ 2786 TCP_TIMER_DISARM(tp, TCPT_REXMT); 2787 tp->t_rtttime = 0; 2788 #ifndef TCP_FACK 2789 /* 2790 * Partial window deflation. This statement relies on the 2791 * fact that tp->snd_una has not been updated yet. In FACK 2792 * hold snd_cwnd constant during fast recovery. 2793 */ 2794 if (tp->snd_cwnd > (th->th_ack - tp->snd_una)) { 2795 tp->snd_cwnd -= th->th_ack - tp->snd_una; 2796 tp->snd_cwnd += tp->t_maxseg; 2797 } else 2798 tp->snd_cwnd = tp->t_maxseg; 2799 #endif 2800 return (1); 2801 } 2802 return (0); 2803 } 2804 #endif /* TCP_SACK */ 2805 2806 /* 2807 * Pull out of band byte out of a segment so 2808 * it doesn't appear in the user's data queue. 2809 * It is still reflected in the segment length for 2810 * sequencing purposes. 2811 */ 2812 void 2813 tcp_pulloutofband(so, urgent, m, off) 2814 struct socket *so; 2815 u_int urgent; 2816 struct mbuf *m; 2817 int off; 2818 { 2819 int cnt = off + urgent - 1; 2820 2821 while (cnt >= 0) { 2822 if (m->m_len > cnt) { 2823 char *cp = mtod(m, caddr_t) + cnt; 2824 struct tcpcb *tp = sototcpcb(so); 2825 2826 tp->t_iobc = *cp; 2827 tp->t_oobflags |= TCPOOB_HAVEDATA; 2828 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2829 m->m_len--; 2830 return; 2831 } 2832 cnt -= m->m_len; 2833 m = m->m_next; 2834 if (m == 0) 2835 break; 2836 } 2837 panic("tcp_pulloutofband"); 2838 } 2839 2840 /* 2841 * Collect new round-trip time estimate 2842 * and update averages and current timeout. 2843 */ 2844 void 2845 tcp_xmit_timer(tp, rtt) 2846 struct tcpcb *tp; 2847 short rtt; 2848 { 2849 short delta; 2850 short rttmin; 2851 2852 tcpstat.tcps_rttupdated++; 2853 --rtt; 2854 if (tp->t_srtt != 0) { 2855 /* 2856 * srtt is stored as fixed point with 3 bits after the 2857 * binary point (i.e., scaled by 8). The following magic 2858 * is equivalent to the smoothing algorithm in rfc793 with 2859 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2860 * point). Adjust rtt to origin 0. 2861 */ 2862 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT); 2863 if ((tp->t_srtt += delta) <= 0) 2864 tp->t_srtt = 1; 2865 /* 2866 * We accumulate a smoothed rtt variance (actually, a 2867 * smoothed mean difference), then set the retransmit 2868 * timer to smoothed rtt + 4 times the smoothed variance. 2869 * rttvar is stored as fixed point with 2 bits after the 2870 * binary point (scaled by 4). The following is 2871 * equivalent to rfc793 smoothing with an alpha of .75 2872 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2873 * rfc793's wired-in beta. 2874 */ 2875 if (delta < 0) 2876 delta = -delta; 2877 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 2878 if ((tp->t_rttvar += delta) <= 0) 2879 tp->t_rttvar = 1; 2880 } else { 2881 /* 2882 * No rtt measurement yet - use the unsmoothed rtt. 2883 * Set the variance to half the rtt (so our first 2884 * retransmit happens at 3*rtt). 2885 */ 2886 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2); 2887 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1); 2888 } 2889 tp->t_rtttime = 0; 2890 tp->t_rxtshift = 0; 2891 2892 /* 2893 * the retransmit should happen at rtt + 4 * rttvar. 2894 * Because of the way we do the smoothing, srtt and rttvar 2895 * will each average +1/2 tick of bias. When we compute 2896 * the retransmit timer, we want 1/2 tick of rounding and 2897 * 1 extra tick because of +-1/2 tick uncertainty in the 2898 * firing of the timer. The bias will give us exactly the 2899 * 1.5 tick we need. But, because the bias is 2900 * statistical, we have to test that we don't drop below 2901 * the minimum feasible timer (which is 2 ticks). 2902 */ 2903 if (tp->t_rttmin > rtt + 2) 2904 rttmin = tp->t_rttmin; 2905 else 2906 rttmin = rtt + 2; 2907 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), rttmin, TCPTV_REXMTMAX); 2908 2909 /* 2910 * We received an ack for a packet that wasn't retransmitted; 2911 * it is probably safe to discard any error indications we've 2912 * received recently. This isn't quite right, but close enough 2913 * for now (a route might have failed after we sent a segment, 2914 * and the return path might not be symmetrical). 2915 */ 2916 tp->t_softerror = 0; 2917 } 2918 2919 /* 2920 * Determine a reasonable value for maxseg size. 2921 * If the route is known, check route for mtu. 2922 * If none, use an mss that can be handled on the outgoing 2923 * interface without forcing IP to fragment; if bigger than 2924 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2925 * to utilize large mbufs. If no route is found, route has no mtu, 2926 * or the destination isn't local, use a default, hopefully conservative 2927 * size (usually 512 or the default IP max size, but no more than the mtu 2928 * of the interface), as we can't discover anything about intervening 2929 * gateways or networks. We also initialize the congestion/slow start 2930 * window to be a single segment if the destination isn't local. 2931 * While looking at the routing entry, we also initialize other path-dependent 2932 * parameters from pre-set or cached values in the routing entry. 2933 * 2934 * Also take into account the space needed for options that we 2935 * send regularly. Make maxseg shorter by that amount to assure 2936 * that we can send maxseg amount of data even when the options 2937 * are present. Store the upper limit of the length of options plus 2938 * data in maxopd. 2939 * 2940 * NOTE: offer == -1 indicates that the maxseg size changed due to 2941 * Path MTU discovery. 2942 */ 2943 int 2944 tcp_mss(tp, offer) 2945 struct tcpcb *tp; 2946 int offer; 2947 { 2948 struct rtentry *rt; 2949 struct ifnet *ifp; 2950 int mss, mssopt; 2951 int iphlen; 2952 int is_ipv6 = 0; 2953 struct inpcb *inp; 2954 2955 inp = tp->t_inpcb; 2956 2957 mssopt = mss = tcp_mssdflt; 2958 2959 rt = in_pcbrtentry(inp); 2960 2961 if (rt == NULL) 2962 goto out; 2963 2964 ifp = rt->rt_ifp; 2965 2966 switch (tp->pf) { 2967 #ifdef INET6 2968 case AF_INET6: 2969 iphlen = sizeof(struct ip6_hdr); 2970 is_ipv6 = 1; 2971 break; 2972 #endif 2973 case AF_INET: 2974 iphlen = sizeof(struct ip); 2975 break; 2976 default: 2977 /* the family does not support path MTU discovery */ 2978 goto out; 2979 } 2980 2981 #ifdef RTV_MTU 2982 /* 2983 * if there's an mtu associated with the route and we support 2984 * path MTU discovery for the underlying protocol family, use it. 2985 */ 2986 if (rt->rt_rmx.rmx_mtu) { 2987 /* 2988 * One may wish to lower MSS to take into account options, 2989 * especially security-related options. 2990 */ 2991 mss = rt->rt_rmx.rmx_mtu - iphlen - sizeof(struct tcphdr); 2992 } else 2993 #endif /* RTV_MTU */ 2994 if (!ifp) 2995 /* 2996 * ifp may be null and rmx_mtu may be zero in certain 2997 * v6 cases (e.g., if ND wasn't able to resolve the 2998 * destination host. 2999 */ 3000 goto out; 3001 else if (ifp->if_flags & IFF_LOOPBACK) 3002 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 3003 else if (!is_ipv6) { 3004 if (ip_mtudisc) 3005 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 3006 else if (inp && in_localaddr(inp->inp_faddr)) 3007 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 3008 } 3009 #ifdef INET6 3010 else if (is_ipv6) { 3011 if (inp && IN6_IS_ADDR_V4MAPPED(&inp->inp_faddr6)) { 3012 /* mapped addr case */ 3013 struct in_addr d; 3014 bcopy(&inp->inp_faddr6.s6_addr32[3], &d, sizeof(d)); 3015 if (ip_mtudisc || in_localaddr(d)) 3016 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 3017 } else { 3018 /* 3019 * for IPv6, path MTU discovery is always turned on, 3020 * or the node must use packet size <= 1280. 3021 */ 3022 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 3023 } 3024 } 3025 #endif /* INET6 */ 3026 3027 /* Calculate the value that we offer in TCPOPT_MAXSEG */ 3028 if (offer != -1) { 3029 mssopt = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 3030 mssopt = max(tcp_mssdflt, mssopt); 3031 } 3032 3033 out: 3034 /* 3035 * The current mss, t_maxseg, is initialized to the default value. 3036 * If we compute a smaller value, reduce the current mss. 3037 * If we compute a larger value, return it for use in sending 3038 * a max seg size option, but don't store it for use 3039 * unless we received an offer at least that large from peer. 3040 * However, do not accept offers under 64 bytes. 3041 */ 3042 if (offer > 0) 3043 tp->t_peermss = offer; 3044 if (tp->t_peermss) 3045 mss = min(mss, tp->t_peermss); 3046 mss = max(mss, 64); /* sanity - at least max opt. space */ 3047 3048 /* 3049 * maxopd stores the maximum length of data AND options 3050 * in a segment; maxseg is the amount of data in a normal 3051 * segment. We need to store this value (maxopd) apart 3052 * from maxseg, because now every segment carries options 3053 * and thus we normally have somewhat less data in segments. 3054 */ 3055 tp->t_maxopd = mss; 3056 3057 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3058 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 3059 mss -= TCPOLEN_TSTAMP_APPA; 3060 3061 if (offer == -1) { 3062 /* mss changed due to Path MTU discovery */ 3063 if (mss < tp->t_maxseg) { 3064 /* 3065 * Follow suggestion in RFC 2414 to reduce the 3066 * congestion window by the ratio of the old 3067 * segment size to the new segment size. 3068 */ 3069 tp->snd_cwnd = ulmax((tp->snd_cwnd / tp->t_maxseg) * 3070 mss, mss); 3071 } 3072 } else 3073 tp->snd_cwnd = mss; 3074 3075 tp->t_maxseg = mss; 3076 3077 return (offer != -1 ? mssopt : mss); 3078 } 3079 3080 /* 3081 * Set connection variables based on the effective MSS. 3082 * We are passed the TCPCB for the actual connection. If we 3083 * are the server, we are called by the compressed state engine 3084 * when the 3-way handshake is complete. If we are the client, 3085 * we are called when we receive the SYN,ACK from the server. 3086 * 3087 * NOTE: The t_maxseg value must be initialized in the TCPCB 3088 * before this routine is called! 3089 */ 3090 void 3091 tcp_mss_update(tp) 3092 struct tcpcb *tp; 3093 { 3094 int mss, rtt; 3095 u_long bufsize; 3096 struct rtentry *rt; 3097 struct socket *so; 3098 3099 so = tp->t_inpcb->inp_socket; 3100 mss = tp->t_maxseg; 3101 3102 rt = in_pcbrtentry(tp->t_inpcb); 3103 3104 if (rt == NULL) 3105 return; 3106 3107 #ifdef RTV_MTU /* if route characteristics exist ... */ 3108 /* 3109 * While we're here, check if there's an initial rtt 3110 * or rttvar. Convert from the route-table units 3111 * to scaled multiples of the slow timeout timer. 3112 */ 3113 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 3114 /* 3115 * XXX the lock bit for MTU indicates that the value 3116 * is also a minimum value; this is subject to time. 3117 */ 3118 if (rt->rt_rmx.rmx_locks & RTV_RTT) 3119 TCPT_RANGESET(tp->t_rttmin, 3120 rtt / (RTM_RTTUNIT / PR_SLOWHZ), 3121 TCPTV_MIN, TCPTV_REXMTMAX); 3122 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 3123 if (rt->rt_rmx.rmx_rttvar) 3124 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 3125 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 3126 else 3127 /* default variation is +- 1 rtt */ 3128 tp->t_rttvar = 3129 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 3130 TCPT_RANGESET((long) tp->t_rxtcur, 3131 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 3132 tp->t_rttmin, TCPTV_REXMTMAX); 3133 } 3134 #endif 3135 3136 /* 3137 * If there's a pipesize, change the socket buffer 3138 * to that size. Make the socket buffers an integral 3139 * number of mss units; if the mss is larger than 3140 * the socket buffer, decrease the mss. 3141 */ 3142 #ifdef RTV_SPIPE 3143 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 3144 #endif 3145 bufsize = so->so_snd.sb_hiwat; 3146 if (bufsize < mss) { 3147 mss = bufsize; 3148 /* Update t_maxseg and t_maxopd */ 3149 tcp_mss(tp, mss); 3150 } else { 3151 bufsize = roundup(bufsize, mss); 3152 if (bufsize > sb_max) 3153 bufsize = sb_max; 3154 (void)sbreserve(&so->so_snd, bufsize); 3155 } 3156 3157 #ifdef RTV_RPIPE 3158 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 3159 #endif 3160 bufsize = so->so_rcv.sb_hiwat; 3161 if (bufsize > mss) { 3162 bufsize = roundup(bufsize, mss); 3163 if (bufsize > sb_max) 3164 bufsize = sb_max; 3165 (void)sbreserve(&so->so_rcv, bufsize); 3166 #ifdef RTV_RPIPE 3167 if (rt->rt_rmx.rmx_recvpipe > 0) 3168 tcp_rscale(tp, so->so_rcv.sb_hiwat); 3169 #endif 3170 } 3171 3172 #ifdef RTV_SSTHRESH 3173 if (rt->rt_rmx.rmx_ssthresh) { 3174 /* 3175 * There's some sort of gateway or interface 3176 * buffer limit on the path. Use this to set 3177 * the slow start threshhold, but set the 3178 * threshold to no less than 2*mss. 3179 */ 3180 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 3181 } 3182 #endif /* RTV_MTU */ 3183 } 3184 #endif /* TUBA_INCLUDE */ 3185 3186 #if defined (TCP_SACK) 3187 /* 3188 * Checks for partial ack. If partial ack arrives, force the retransmission 3189 * of the next unacknowledged segment, do not clear tp->t_dupacks, and return 3190 * 1. By setting snd_nxt to ti_ack, this forces retransmission timer to 3191 * be started again. If the ack advances at least to tp->snd_last, return 0. 3192 */ 3193 int 3194 tcp_newreno(tp, th) 3195 struct tcpcb *tp; 3196 struct tcphdr *th; 3197 { 3198 if (SEQ_LT(th->th_ack, tp->snd_last)) { 3199 /* 3200 * snd_una has not been updated and the socket send buffer 3201 * not yet drained of the acked data, so we have to leave 3202 * snd_una as it was to get the correct data offset in 3203 * tcp_output(). 3204 */ 3205 tcp_seq onxt = tp->snd_nxt; 3206 u_long ocwnd = tp->snd_cwnd; 3207 TCP_TIMER_DISARM(tp, TCPT_REXMT); 3208 tp->t_rtttime = 0; 3209 tp->snd_nxt = th->th_ack; 3210 /* 3211 * Set snd_cwnd to one segment beyond acknowledged offset 3212 * (tp->snd_una not yet updated when this function is called) 3213 */ 3214 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3215 (void) tcp_output(tp); 3216 tp->snd_cwnd = ocwnd; 3217 if (SEQ_GT(onxt, tp->snd_nxt)) 3218 tp->snd_nxt = onxt; 3219 /* 3220 * Partial window deflation. Relies on fact that tp->snd_una 3221 * not updated yet. 3222 */ 3223 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 3224 return 1; 3225 } 3226 return 0; 3227 } 3228 #endif /* TCP_SACK */ 3229