1 /* $OpenBSD: tcp_input.c,v 1.124 2002/09/11 03:27:03 itojun 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 sbappendstream(&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 u_int8_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 *ip6 = 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 ip6 = 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 ip6 = mtod(m, struct ip6_hdr *); 563 tlen = m->m_pkthdr.len - iphlen; 564 #ifdef TCP_ECN 565 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 566 #endif 567 568 /* Be proactive about malicious use of IPv4 mapped address */ 569 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 570 IN6_IS_ADDR_V4MAPPED(&ip6->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(&ip6->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 ip6 = 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, u_int8_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, &ip6->ip6_src, th->th_sport, 668 &ip6->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, &ip6->ip6_src, 682 th->th_sport, &ip6->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 #ifdef INET6 734 /* 735 * If deprecated address is forbidden, 736 * we do not accept SYN to deprecated interface 737 * address to prevent any new inbound connection from 738 * getting established. So drop the SYN packet. 739 * When we do not accept SYN, we send a TCP RST, 740 * with deprecated source address (instead of dropping 741 * it). We compromise it as it is much better for peer 742 * to send a RST, and RST will be the final packet 743 * for the exchange. 744 * 745 * If we do not forbid deprecated addresses, we accept 746 * the SYN packet. RFC2462 does not suggest dropping 747 * SYN in this case. 748 * If we decipher RFC2462 5.5.4, it says like this: 749 * 1. use of deprecated addr with existing 750 * communication is okay - "SHOULD continue to be 751 * used" 752 * 2. use of it with new communication: 753 * (2a) "SHOULD NOT be used if alternate address 754 * with sufficient scope is available" 755 * (2b) nothing mentioned otherwise. 756 * Here we fall into (2b) case as we have no choice in 757 * our source address selection - we must obey the peer. 758 * 759 * The wording in RFC2462 is confusing, and there are 760 * multiple description text for deprecated address 761 * handling - worse, they are not exactly the same. 762 * I believe 5.5.4 is the best one, so we follow 5.5.4. 763 */ 764 if (ip6 && !ip6_use_deprecated) { 765 struct in6_ifaddr *ia6; 766 767 if ((ia6 = in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) && 768 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 769 tp = NULL; 770 goto dropwithreset; 771 } 772 } 773 #endif 774 775 so1 = sonewconn(so, 0); 776 if (so1 == NULL) { 777 tcpdropoldhalfopen(tp, th->th_dport); 778 so1 = sonewconn(so, 0); 779 if (so1 == NULL) 780 goto drop; 781 } 782 so = so1; 783 /* 784 * This is ugly, but .... 785 * 786 * Mark socket as temporary until we're 787 * committed to keeping it. The code at 788 * ``drop'' and ``dropwithreset'' check the 789 * flag dropsocket to see if the temporary 790 * socket created here should be discarded. 791 * We mark the socket as discardable until 792 * we're committed to it below in TCPS_LISTEN. 793 */ 794 dropsocket++; 795 #ifdef IPSEC 796 /* 797 * We need to copy the required security levels 798 * from the old pcb. Ditto for any other 799 * IPsec-related information. 800 */ 801 { 802 struct inpcb *newinp = (struct inpcb *)so->so_pcb; 803 bcopy(inp->inp_seclevel, newinp->inp_seclevel, 804 sizeof(inp->inp_seclevel)); 805 newinp->inp_secrequire = inp->inp_secrequire; 806 if (inp->inp_ipo != NULL) { 807 newinp->inp_ipo = inp->inp_ipo; 808 inp->inp_ipo->ipo_ref_count++; 809 } 810 if (inp->inp_ipsec_remotecred != NULL) { 811 newinp->inp_ipsec_remotecred = inp->inp_ipsec_remotecred; 812 inp->inp_ipsec_remotecred->ref_count++; 813 } 814 if (inp->inp_ipsec_remoteauth != NULL) { 815 newinp->inp_ipsec_remoteauth 816 = inp->inp_ipsec_remoteauth; 817 inp->inp_ipsec_remoteauth->ref_count++; 818 } 819 } 820 #endif /* IPSEC */ 821 #ifdef INET6 822 /* 823 * inp still has the OLD in_pcb stuff, set the 824 * v6-related flags on the new guy, too. This is 825 * done particularly for the case where an AF_INET6 826 * socket is bound only to a port, and a v4 connection 827 * comes in on that port. 828 * we also copy the flowinfo from the original pcb 829 * to the new one. 830 */ 831 { 832 int flags = inp->inp_flags; 833 struct inpcb *oldinpcb = inp; 834 835 inp = (struct inpcb *)so->so_pcb; 836 inp->inp_flags |= (flags & INP_IPV6); 837 if ((inp->inp_flags & INP_IPV6) != 0) { 838 inp->inp_ipv6.ip6_hlim = 839 oldinpcb->inp_ipv6.ip6_hlim; 840 inp->inp_ipv6.ip6_flow = 841 oldinpcb->inp_ipv6.ip6_flow; 842 } 843 } 844 #else /* INET6 */ 845 inp = (struct inpcb *)so->so_pcb; 846 #endif /* INET6 */ 847 inp->inp_lport = th->th_dport; 848 switch (af) { 849 #ifdef INET6 850 case AF_INET6: 851 inp->inp_laddr6 = ip6->ip6_dst; 852 853 /*inp->inp_options = ip6_srcroute();*/ /* soon. */ 854 /* 855 * still need to tweak outbound options 856 * processing to include this mbuf in 857 * the right place and put the correct 858 * NextHdr values in the right places. 859 * XXX rja 860 */ 861 break; 862 #endif /* INET6 */ 863 case AF_INET: 864 inp->inp_laddr = ip->ip_dst; 865 inp->inp_options = ip_srcroute(); 866 break; 867 } 868 in_pcbrehash(inp); 869 tp = intotcpcb(inp); 870 tp->t_state = TCPS_LISTEN; 871 872 /* Compute proper scaling value from buffer space */ 873 tcp_rscale(tp, so->so_rcv.sb_hiwat); 874 } 875 } 876 877 #ifdef IPSEC 878 /* Find most recent IPsec tag */ 879 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 880 s = splnet(); 881 if (mtag != NULL) { 882 tdbi = (struct tdb_ident *)(mtag + 1); 883 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto); 884 } else 885 tdb = NULL; 886 ipsp_spd_lookup(m, af, iphlen, &error, IPSP_DIRECTION_IN, 887 tdb, inp); 888 if (error) { 889 splx(s); 890 goto drop; 891 } 892 893 /* Latch SA */ 894 if (inp->inp_tdb_in != tdb) { 895 if (tdb) { 896 tdb_add_inp(tdb, inp, 1); 897 if (inp->inp_ipo == NULL) { 898 inp->inp_ipo = ipsec_add_policy(inp, af, 899 IPSP_DIRECTION_OUT); 900 if (inp->inp_ipo == NULL) { 901 splx(s); 902 goto drop; 903 } 904 } 905 if (inp->inp_ipo->ipo_dstid == NULL && 906 tdb->tdb_srcid != NULL) { 907 inp->inp_ipo->ipo_dstid = tdb->tdb_srcid; 908 tdb->tdb_srcid->ref_count++; 909 } 910 if (inp->inp_ipsec_remotecred == NULL && 911 tdb->tdb_remote_cred != NULL) { 912 inp->inp_ipsec_remotecred = 913 tdb->tdb_remote_cred; 914 tdb->tdb_remote_cred->ref_count++; 915 } 916 if (inp->inp_ipsec_remoteauth == NULL && 917 tdb->tdb_remote_auth != NULL) { 918 inp->inp_ipsec_remoteauth = 919 tdb->tdb_remote_auth; 920 tdb->tdb_remote_auth->ref_count++; 921 } 922 } else { /* Just reset */ 923 TAILQ_REMOVE(&inp->inp_tdb_in->tdb_inp_in, inp, 924 inp_tdb_in_next); 925 inp->inp_tdb_in = NULL; 926 } 927 } 928 splx(s); 929 #endif /* IPSEC */ 930 931 /* 932 * Segment received on connection. 933 * Reset idle time and keep-alive timer. 934 */ 935 tp->t_rcvtime = tcp_now; 936 if (tp->t_state != TCPS_SYN_RECEIVED) 937 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 938 939 #ifdef TCP_SACK 940 if (!tp->sack_disable) 941 tcp_del_sackholes(tp, th); /* Delete stale SACK holes */ 942 #endif /* TCP_SACK */ 943 944 /* 945 * Process options if not in LISTEN state, 946 * else do it below (after getting remote address). 947 */ 948 if (optp && tp->t_state != TCPS_LISTEN) 949 tcp_dooptions(tp, optp, optlen, th, 950 &ts_present, &ts_val, &ts_ecr); 951 952 #ifdef TCP_SACK 953 if (!tp->sack_disable) { 954 tp->rcv_laststart = th->th_seq; /* last rec'vd segment*/ 955 tp->rcv_lastend = th->th_seq + tlen; 956 } 957 #endif /* TCP_SACK */ 958 #ifdef TCP_ECN 959 /* if congestion experienced, set ECE bit in subsequent packets. */ 960 if ((iptos & IPTOS_ECN_MASK) == IPTOS_ECN_CE) { 961 tp->t_flags |= TF_RCVD_CE; 962 tcpstat.tcps_ecn_rcvce++; 963 } 964 #endif 965 /* 966 * Header prediction: check for the two common cases 967 * of a uni-directional data xfer. If the packet has 968 * no control flags, is in-sequence, the window didn't 969 * change and we're not retransmitting, it's a 970 * candidate. If the length is zero and the ack moved 971 * forward, we're the sender side of the xfer. Just 972 * free the data acked & wake any higher level process 973 * that was blocked waiting for space. If the length 974 * is non-zero and the ack didn't move, we're the 975 * receiver side. If we're getting packets in-order 976 * (the reassembly queue is empty), add the data to 977 * the socket buffer and note that we need a delayed ack. 978 */ 979 if (tp->t_state == TCPS_ESTABLISHED && 980 #ifdef TCP_ECN 981 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ECE|TH_CWR|TH_ACK)) == TH_ACK && 982 #else 983 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 984 #endif 985 (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && 986 th->th_seq == tp->rcv_nxt && 987 tiwin && tiwin == tp->snd_wnd && 988 tp->snd_nxt == tp->snd_max) { 989 990 /* 991 * If last ACK falls within this segment's sequence numbers, 992 * record the timestamp. 993 * Fix from Braden, see Stevens p. 870 994 */ 995 if (ts_present && SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 996 tp->ts_recent_age = tcp_now; 997 tp->ts_recent = ts_val; 998 } 999 1000 if (tlen == 0) { 1001 if (SEQ_GT(th->th_ack, tp->snd_una) && 1002 SEQ_LEQ(th->th_ack, tp->snd_max) && 1003 tp->snd_cwnd >= tp->snd_wnd && 1004 tp->t_dupacks == 0) { 1005 /* 1006 * this is a pure ack for outstanding data. 1007 */ 1008 ++tcpstat.tcps_predack; 1009 if (ts_present) 1010 tcp_xmit_timer(tp, tcp_now-ts_ecr+1); 1011 else if (tp->t_rtttime && 1012 SEQ_GT(th->th_ack, tp->t_rtseq)) 1013 tcp_xmit_timer(tp, 1014 tcp_now - tp->t_rtttime); 1015 acked = th->th_ack - tp->snd_una; 1016 tcpstat.tcps_rcvackpack++; 1017 tcpstat.tcps_rcvackbyte += acked; 1018 ND6_HINT(tp); 1019 sbdrop(&so->so_snd, acked); 1020 tp->snd_una = th->th_ack; 1021 #if defined(TCP_SACK) || defined(TCP_ECN) 1022 /* 1023 * We want snd_last to track snd_una so 1024 * as to avoid sequence wraparound problems 1025 * for very large transfers. 1026 */ 1027 #ifdef TCP_ECN 1028 if (SEQ_GT(tp->snd_una, tp->snd_last)) 1029 #endif 1030 tp->snd_last = tp->snd_una; 1031 #endif /* TCP_SACK */ 1032 #if defined(TCP_SACK) && defined(TCP_FACK) 1033 tp->snd_fack = tp->snd_una; 1034 tp->retran_data = 0; 1035 #endif /* TCP_FACK */ 1036 m_freem(m); 1037 1038 /* 1039 * If all outstanding data are acked, stop 1040 * retransmit timer, otherwise restart timer 1041 * using current (possibly backed-off) value. 1042 * If process is waiting for space, 1043 * wakeup/selwakeup/signal. If data 1044 * are ready to send, let tcp_output 1045 * decide between more output or persist. 1046 */ 1047 if (tp->snd_una == tp->snd_max) 1048 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1049 else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 1050 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1051 1052 if (sb_notify(&so->so_snd)) 1053 sowwakeup(so); 1054 if (so->so_snd.sb_cc) 1055 (void) tcp_output(tp); 1056 return; 1057 } 1058 } else if (th->th_ack == tp->snd_una && 1059 tp->segq.lh_first == NULL && 1060 tlen <= sbspace(&so->so_rcv)) { 1061 /* 1062 * This is a pure, in-sequence data packet 1063 * with nothing on the reassembly queue and 1064 * we have enough buffer space to take it. 1065 */ 1066 #ifdef TCP_SACK 1067 /* Clean receiver SACK report if present */ 1068 if (!tp->sack_disable && tp->rcv_numsacks) 1069 tcp_clean_sackreport(tp); 1070 #endif /* TCP_SACK */ 1071 ++tcpstat.tcps_preddat; 1072 tp->rcv_nxt += tlen; 1073 tcpstat.tcps_rcvpack++; 1074 tcpstat.tcps_rcvbyte += tlen; 1075 ND6_HINT(tp); 1076 /* 1077 * Drop TCP, IP headers and TCP options then add data 1078 * to socket buffer. 1079 */ 1080 if (so->so_state & SS_CANTRCVMORE) 1081 m_freem(m); 1082 else { 1083 m_adj(m, iphlen + off); 1084 sbappendstream(&so->so_rcv, m); 1085 } 1086 sorwakeup(so); 1087 TCP_SETUP_ACK(tp, tiflags); 1088 if (tp->t_flags & TF_ACKNOW) 1089 (void) tcp_output(tp); 1090 return; 1091 } 1092 } 1093 1094 /* 1095 * Compute mbuf offset to TCP data segment. 1096 */ 1097 hdroptlen = iphlen + off; 1098 1099 /* 1100 * Calculate amount of space in receive window, 1101 * and then do TCP input processing. 1102 * Receive window is amount of space in rcv queue, 1103 * but not less than advertised window. 1104 */ 1105 { int win; 1106 1107 win = sbspace(&so->so_rcv); 1108 if (win < 0) 1109 win = 0; 1110 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1111 } 1112 1113 switch (tp->t_state) { 1114 1115 /* 1116 * If the state is LISTEN then ignore segment if it contains an RST. 1117 * If the segment contains an ACK then it is bad and send a RST. 1118 * If it does not contain a SYN then it is not interesting; drop it. 1119 * If it is from this socket, drop it, it must be forged. 1120 * Don't bother responding if the destination was a broadcast. 1121 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1122 * tp->iss, and send a segment: 1123 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1124 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1125 * Fill in remote peer address fields if not previously specified. 1126 * Enter SYN_RECEIVED state, and process any other fields of this 1127 * segment in this state. 1128 */ 1129 case TCPS_LISTEN: { 1130 struct mbuf *am; 1131 struct sockaddr_in *sin; 1132 #ifdef INET6 1133 struct sockaddr_in6 *sin6; 1134 #endif /* INET6 */ 1135 1136 if (tiflags & TH_RST) 1137 goto drop; 1138 if (tiflags & TH_ACK) 1139 goto dropwithreset; 1140 if ((tiflags & TH_SYN) == 0) 1141 goto drop; 1142 if (th->th_dport == th->th_sport) { 1143 switch (af) { 1144 #ifdef INET6 1145 case AF_INET6: 1146 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, 1147 &ip6->ip6_dst)) 1148 goto drop; 1149 break; 1150 #endif /* INET6 */ 1151 case AF_INET: 1152 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 1153 goto drop; 1154 break; 1155 } 1156 } 1157 1158 /* 1159 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 1160 */ 1161 if (m->m_flags & (M_BCAST|M_MCAST)) 1162 goto drop; 1163 switch (af) { 1164 #ifdef INET6 1165 case AF_INET6: 1166 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 1167 goto drop; 1168 break; 1169 #endif /* INET6 */ 1170 case AF_INET: 1171 if (IN_MULTICAST(ip->ip_dst.s_addr) || 1172 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 1173 goto drop; 1174 break; 1175 } 1176 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 1177 if (am == NULL) 1178 goto drop; 1179 switch (af) { 1180 #ifdef INET6 1181 case AF_INET6: 1182 /* 1183 * This is probably the place to set the tp->pf value. 1184 * (Don't forget to do it in the v4 code as well!) 1185 * 1186 * Also, remember to blank out things like flowlabel, or 1187 * set flowlabel for accepted sockets in v6. 1188 * 1189 * FURTHERMORE, this is PROBABLY the place where the 1190 * whole business of key munging is set up for passive 1191 * connections. 1192 */ 1193 am->m_len = sizeof(struct sockaddr_in6); 1194 sin6 = mtod(am, struct sockaddr_in6 *); 1195 bzero(sin6, sizeof(*sin6)); 1196 sin6->sin6_family = AF_INET6; 1197 sin6->sin6_len = sizeof(struct sockaddr_in6); 1198 sin6->sin6_addr = ip6->ip6_src; 1199 sin6->sin6_port = th->th_sport; 1200 sin6->sin6_flowinfo = htonl(0x0fffffff) & 1201 inp->inp_ipv6.ip6_flow; 1202 laddr6 = inp->inp_laddr6; 1203 if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6)) 1204 inp->inp_laddr6 = ip6->ip6_dst; 1205 /* This is a good optimization. */ 1206 if (in6_pcbconnect(inp, am)) { 1207 inp->inp_laddr6 = laddr6; 1208 (void) m_free(am); 1209 goto drop; 1210 } 1211 break; 1212 #endif 1213 case AF_INET: 1214 /* drop IPv4 packet to AF_INET6 socket */ 1215 if (inp->inp_flags & INP_IPV6) { 1216 (void) m_free(am); 1217 goto drop; 1218 } 1219 am->m_len = sizeof(struct sockaddr_in); 1220 sin = mtod(am, struct sockaddr_in *); 1221 bzero(sin, sizeof(*sin)); 1222 sin->sin_family = AF_INET; 1223 sin->sin_len = sizeof(*sin); 1224 sin->sin_addr = ip->ip_src; 1225 sin->sin_port = th->th_sport; 1226 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 1227 laddr = inp->inp_laddr; 1228 if (inp->inp_laddr.s_addr == INADDR_ANY) 1229 inp->inp_laddr = ip->ip_dst; 1230 if (in_pcbconnect(inp, am)) { 1231 inp->inp_laddr = laddr; 1232 (void) m_free(am); 1233 goto drop; 1234 } 1235 (void) m_free(am); 1236 break; 1237 } 1238 tp->t_template = tcp_template(tp); 1239 if (tp->t_template == 0) { 1240 tp = tcp_drop(tp, ENOBUFS); 1241 dropsocket = 0; /* socket is already gone */ 1242 goto drop; 1243 } 1244 if (optp) 1245 tcp_dooptions(tp, optp, optlen, th, 1246 &ts_present, &ts_val, &ts_ecr); 1247 #ifdef TCP_SACK 1248 /* 1249 * If peer did not send a SACK_PERMITTED option (i.e., if 1250 * tcp_dooptions() did not set TF_SACK_PERMIT), set 1251 * sack_disable to 1 if it is currently 0. 1252 */ 1253 if (!tp->sack_disable) 1254 if ((tp->t_flags & TF_SACK_PERMIT) == 0) 1255 tp->sack_disable = 1; 1256 #endif 1257 1258 if (iss) 1259 tp->iss = iss; 1260 else { 1261 #ifdef TCP_COMPAT_42 1262 tcp_iss += TCP_ISSINCR/2; 1263 tp->iss = tcp_iss; 1264 #else /* TCP_COMPAT_42 */ 1265 tp->iss = tcp_rndiss_next(); 1266 #endif /* !TCP_COMPAT_42 */ 1267 } 1268 tp->irs = th->th_seq; 1269 tcp_sendseqinit(tp); 1270 #if defined (TCP_SACK) || defined(TCP_ECN) 1271 tp->snd_last = tp->snd_una; 1272 #endif /* TCP_SACK */ 1273 #if defined(TCP_SACK) && defined(TCP_FACK) 1274 tp->snd_fack = tp->snd_una; 1275 tp->retran_data = 0; 1276 tp->snd_awnd = 0; 1277 #endif /* TCP_FACK */ 1278 #ifdef TCP_ECN 1279 /* 1280 * if both ECE and CWR flag bits are set, peer is ECN capable. 1281 */ 1282 if (tcp_do_ecn && 1283 (tiflags & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 1284 tp->t_flags |= TF_ECN_PERMIT; 1285 tcpstat.tcps_ecn_accepts++; 1286 } 1287 #endif 1288 tcp_rcvseqinit(tp); 1289 tp->t_flags |= TF_ACKNOW; 1290 tp->t_state = TCPS_SYN_RECEIVED; 1291 TCP_TIMER_ARM(tp, TCPT_KEEP, tcptv_keep_init); 1292 dropsocket = 0; /* committed to socket */ 1293 tcpstat.tcps_accepts++; 1294 goto trimthenstep6; 1295 } 1296 1297 /* 1298 * If the state is SYN_RECEIVED: 1299 * if seg contains SYN/ACK, send an RST. 1300 * if seg contains an ACK, but not for our SYN/ACK, send an RST 1301 */ 1302 1303 case TCPS_SYN_RECEIVED: 1304 if (tiflags & TH_ACK) { 1305 if (tiflags & TH_SYN) { 1306 tcpstat.tcps_badsyn++; 1307 goto dropwithreset; 1308 } 1309 if (SEQ_LEQ(th->th_ack, tp->snd_una) || 1310 SEQ_GT(th->th_ack, tp->snd_max)) 1311 goto dropwithreset; 1312 } 1313 break; 1314 1315 /* 1316 * If the state is SYN_SENT: 1317 * if seg contains an ACK, but not for our SYN, drop the input. 1318 * if seg contains a RST, then drop the connection. 1319 * if seg does not contain SYN, then drop it. 1320 * Otherwise this is an acceptable SYN segment 1321 * initialize tp->rcv_nxt and tp->irs 1322 * if seg contains ack then advance tp->snd_una 1323 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1324 * arrange for segment to be acked (eventually) 1325 * continue processing rest of data/controls, beginning with URG 1326 */ 1327 case TCPS_SYN_SENT: 1328 if ((tiflags & TH_ACK) && 1329 (SEQ_LEQ(th->th_ack, tp->iss) || 1330 SEQ_GT(th->th_ack, tp->snd_max))) 1331 goto dropwithreset; 1332 if (tiflags & TH_RST) { 1333 #ifdef TCP_ECN 1334 /* if ECN is enabled, fall back to non-ecn at rexmit */ 1335 if (tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN)) 1336 goto drop; 1337 #endif 1338 if (tiflags & TH_ACK) 1339 tp = tcp_drop(tp, ECONNREFUSED); 1340 goto drop; 1341 } 1342 if ((tiflags & TH_SYN) == 0) 1343 goto drop; 1344 if (tiflags & TH_ACK) { 1345 tp->snd_una = th->th_ack; 1346 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1347 tp->snd_nxt = tp->snd_una; 1348 } 1349 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1350 tp->irs = th->th_seq; 1351 tcp_rcvseqinit(tp); 1352 tp->t_flags |= TF_ACKNOW; 1353 #ifdef TCP_SACK 1354 /* 1355 * If we've sent a SACK_PERMITTED option, and the peer 1356 * also replied with one, then TF_SACK_PERMIT should have 1357 * been set in tcp_dooptions(). If it was not, disable SACKs. 1358 */ 1359 if (!tp->sack_disable) 1360 if ((tp->t_flags & TF_SACK_PERMIT) == 0) 1361 tp->sack_disable = 1; 1362 #endif 1363 #ifdef TCP_ECN 1364 /* 1365 * if ECE is set but CWR is not set for SYN-ACK, or 1366 * both ECE and CWR are set for simultaneous open, 1367 * peer is ECN capable. 1368 */ 1369 if (tcp_do_ecn) { 1370 if ((tiflags & (TH_ACK|TH_ECE|TH_CWR)) 1371 == (TH_ACK|TH_ECE) || 1372 (tiflags & (TH_ACK|TH_ECE|TH_CWR)) 1373 == (TH_ECE|TH_CWR)) { 1374 tp->t_flags |= TF_ECN_PERMIT; 1375 tiflags &= ~(TH_ECE|TH_CWR); 1376 tcpstat.tcps_ecn_accepts++; 1377 } 1378 } 1379 #endif 1380 1381 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 1382 tcpstat.tcps_connects++; 1383 soisconnected(so); 1384 tp->t_state = TCPS_ESTABLISHED; 1385 /* Do window scaling on this connection? */ 1386 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1387 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1388 tp->snd_scale = tp->requested_s_scale; 1389 tp->rcv_scale = tp->request_r_scale; 1390 } 1391 (void) tcp_reass(tp, (struct tcphdr *)0, 1392 (struct mbuf *)0, &tlen); 1393 /* 1394 * if we didn't have to retransmit the SYN, 1395 * use its rtt as our initial srtt & rtt var. 1396 */ 1397 if (tp->t_rtttime) 1398 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 1399 /* 1400 * Since new data was acked (the SYN), open the 1401 * congestion window by one MSS. We do this 1402 * here, because we won't go through the normal 1403 * ACK processing below. And since this is the 1404 * start of the connection, we know we are in 1405 * the exponential phase of slow-start. 1406 */ 1407 tp->snd_cwnd += tp->t_maxseg; 1408 } else 1409 tp->t_state = TCPS_SYN_RECEIVED; 1410 1411 trimthenstep6: 1412 /* 1413 * Advance th->th_seq to correspond to first data byte. 1414 * If data, trim to stay within window, 1415 * dropping FIN if necessary. 1416 */ 1417 th->th_seq++; 1418 if (tlen > tp->rcv_wnd) { 1419 todrop = tlen - tp->rcv_wnd; 1420 m_adj(m, -todrop); 1421 tlen = tp->rcv_wnd; 1422 tiflags &= ~TH_FIN; 1423 tcpstat.tcps_rcvpackafterwin++; 1424 tcpstat.tcps_rcvbyteafterwin += todrop; 1425 } 1426 tp->snd_wl1 = th->th_seq - 1; 1427 tp->rcv_up = th->th_seq; 1428 goto step6; 1429 } 1430 1431 /* 1432 * States other than LISTEN or SYN_SENT. 1433 * First check timestamp, if present. 1434 * Then check that at least some bytes of segment are within 1435 * receive window. If segment begins before rcv_nxt, 1436 * drop leading data (and SYN); if nothing left, just ack. 1437 * 1438 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1439 * and it's less than ts_recent, drop it. 1440 */ 1441 if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && 1442 TSTMP_LT(ts_val, tp->ts_recent)) { 1443 1444 /* Check to see if ts_recent is over 24 days old. */ 1445 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1446 /* 1447 * Invalidate ts_recent. If this segment updates 1448 * ts_recent, the age will be reset later and ts_recent 1449 * will get a valid value. If it does not, setting 1450 * ts_recent to zero will at least satisfy the 1451 * requirement that zero be placed in the timestamp 1452 * echo reply when ts_recent isn't valid. The 1453 * age isn't reset until we get a valid ts_recent 1454 * because we don't want out-of-order segments to be 1455 * dropped when ts_recent is old. 1456 */ 1457 tp->ts_recent = 0; 1458 } else { 1459 tcpstat.tcps_rcvduppack++; 1460 tcpstat.tcps_rcvdupbyte += tlen; 1461 tcpstat.tcps_pawsdrop++; 1462 goto dropafterack; 1463 } 1464 } 1465 1466 todrop = tp->rcv_nxt - th->th_seq; 1467 if (todrop > 0) { 1468 if (tiflags & TH_SYN) { 1469 tiflags &= ~TH_SYN; 1470 th->th_seq++; 1471 if (th->th_urp > 1) 1472 th->th_urp--; 1473 else 1474 tiflags &= ~TH_URG; 1475 todrop--; 1476 } 1477 if (todrop >= tlen || 1478 (todrop == tlen && (tiflags & TH_FIN) == 0)) { 1479 /* 1480 * Any valid FIN must be to the left of the 1481 * window. At this point, FIN must be a 1482 * duplicate or out-of-sequence, so drop it. 1483 */ 1484 tiflags &= ~TH_FIN; 1485 /* 1486 * Send ACK to resynchronize, and drop any data, 1487 * but keep on processing for RST or ACK. 1488 */ 1489 tp->t_flags |= TF_ACKNOW; 1490 tcpstat.tcps_rcvdupbyte += todrop = tlen; 1491 tcpstat.tcps_rcvduppack++; 1492 } else { 1493 tcpstat.tcps_rcvpartduppack++; 1494 tcpstat.tcps_rcvpartdupbyte += todrop; 1495 } 1496 hdroptlen += todrop; /* drop from head afterwards */ 1497 th->th_seq += todrop; 1498 tlen -= todrop; 1499 if (th->th_urp > todrop) 1500 th->th_urp -= todrop; 1501 else { 1502 tiflags &= ~TH_URG; 1503 th->th_urp = 0; 1504 } 1505 } 1506 1507 /* 1508 * If new data are received on a connection after the 1509 * user processes are gone, then RST the other end. 1510 */ 1511 if ((so->so_state & SS_NOFDREF) && 1512 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1513 tp = tcp_close(tp); 1514 tcpstat.tcps_rcvafterclose++; 1515 goto dropwithreset; 1516 } 1517 1518 /* 1519 * If segment ends after window, drop trailing data 1520 * (and PUSH and FIN); if nothing left, just ACK. 1521 */ 1522 todrop = (th->th_seq + tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1523 if (todrop > 0) { 1524 tcpstat.tcps_rcvpackafterwin++; 1525 if (todrop >= tlen) { 1526 tcpstat.tcps_rcvbyteafterwin += tlen; 1527 /* 1528 * If a new connection request is received 1529 * while in TIME_WAIT, drop the old connection 1530 * and start over if the sequence numbers 1531 * are above the previous ones. 1532 */ 1533 if (tiflags & TH_SYN && 1534 tp->t_state == TCPS_TIME_WAIT && 1535 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1536 iss = tp->snd_nxt + TCP_ISSINCR; 1537 tp = tcp_close(tp); 1538 goto findpcb; 1539 } 1540 /* 1541 * If window is closed can only take segments at 1542 * window edge, and have to drop data and PUSH from 1543 * incoming segments. Continue processing, but 1544 * remember to ack. Otherwise, drop segment 1545 * and ack. 1546 */ 1547 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1548 tp->t_flags |= TF_ACKNOW; 1549 tcpstat.tcps_rcvwinprobe++; 1550 } else 1551 goto dropafterack; 1552 } else 1553 tcpstat.tcps_rcvbyteafterwin += todrop; 1554 m_adj(m, -todrop); 1555 tlen -= todrop; 1556 tiflags &= ~(TH_PUSH|TH_FIN); 1557 } 1558 1559 /* 1560 * If last ACK falls within this segment's sequence numbers, 1561 * record its timestamp. 1562 * Fix from Braden, see Stevens p. 870 1563 */ 1564 if (ts_present && TSTMP_GEQ(ts_val, tp->ts_recent) && 1565 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1566 tp->ts_recent_age = tcp_now; 1567 tp->ts_recent = ts_val; 1568 } 1569 1570 /* 1571 * If the RST bit is set examine the state: 1572 * SYN_RECEIVED STATE: 1573 * If passive open, return to LISTEN state. 1574 * If active open, inform user that connection was refused. 1575 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 1576 * Inform user that connection was reset, and close tcb. 1577 * CLOSING, LAST_ACK, TIME_WAIT STATES 1578 * Close the tcb. 1579 */ 1580 if (tiflags & TH_RST) { 1581 if (th->th_seq != tp->last_ack_sent) 1582 goto drop; 1583 1584 switch (tp->t_state) { 1585 case TCPS_SYN_RECEIVED: 1586 #ifdef TCP_ECN 1587 /* if ECN is enabled, fall back to non-ecn at rexmit */ 1588 if (tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN)) 1589 goto drop; 1590 #endif 1591 so->so_error = ECONNREFUSED; 1592 goto close; 1593 1594 case TCPS_ESTABLISHED: 1595 case TCPS_FIN_WAIT_1: 1596 case TCPS_FIN_WAIT_2: 1597 case TCPS_CLOSE_WAIT: 1598 so->so_error = ECONNRESET; 1599 close: 1600 tp->t_state = TCPS_CLOSED; 1601 tcpstat.tcps_drops++; 1602 tp = tcp_close(tp); 1603 goto drop; 1604 case TCPS_CLOSING: 1605 case TCPS_LAST_ACK: 1606 case TCPS_TIME_WAIT: 1607 tp = tcp_close(tp); 1608 goto drop; 1609 } 1610 } 1611 1612 /* 1613 * If a SYN is in the window, then this is an 1614 * error and we send an RST and drop the connection. 1615 */ 1616 if (tiflags & TH_SYN) { 1617 tp = tcp_drop(tp, ECONNRESET); 1618 goto dropwithreset; 1619 } 1620 1621 /* 1622 * If the ACK bit is off we drop the segment and return. 1623 */ 1624 if ((tiflags & TH_ACK) == 0) { 1625 if (tp->t_flags & TF_ACKNOW) 1626 goto dropafterack; 1627 else 1628 goto drop; 1629 } 1630 1631 /* 1632 * Ack processing. 1633 */ 1634 switch (tp->t_state) { 1635 1636 /* 1637 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1638 * ESTABLISHED state and continue processing. 1639 * The ACK was checked above. 1640 */ 1641 case TCPS_SYN_RECEIVED: 1642 tcpstat.tcps_connects++; 1643 soisconnected(so); 1644 tp->t_state = TCPS_ESTABLISHED; 1645 /* Do window scaling? */ 1646 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1647 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1648 tp->snd_scale = tp->requested_s_scale; 1649 tp->rcv_scale = tp->request_r_scale; 1650 } 1651 (void) tcp_reass(tp, (struct tcphdr *)0, (struct mbuf *)0, 1652 &tlen); 1653 tp->snd_wl1 = th->th_seq - 1; 1654 /* fall into ... */ 1655 1656 /* 1657 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1658 * ACKs. If the ack is in the range 1659 * tp->snd_una < th->th_ack <= tp->snd_max 1660 * then advance tp->snd_una to th->th_ack and drop 1661 * data from the retransmission queue. If this ACK reflects 1662 * more up to date window information we update our window information. 1663 */ 1664 case TCPS_ESTABLISHED: 1665 case TCPS_FIN_WAIT_1: 1666 case TCPS_FIN_WAIT_2: 1667 case TCPS_CLOSE_WAIT: 1668 case TCPS_CLOSING: 1669 case TCPS_LAST_ACK: 1670 case TCPS_TIME_WAIT: 1671 #ifdef TCP_ECN 1672 /* 1673 * if we receive ECE and are not already in recovery phase, 1674 * reduce cwnd by half but don't slow-start. 1675 * advance snd_last to snd_max not to reduce cwnd again 1676 * until all outstanding packets are acked. 1677 */ 1678 if (tcp_do_ecn && (tiflags & TH_ECE)) { 1679 if ((tp->t_flags & TF_ECN_PERMIT) && 1680 SEQ_GEQ(tp->snd_una, tp->snd_last)) { 1681 u_int win; 1682 1683 win = min(tp->snd_wnd, tp->snd_cwnd) / tp->t_maxseg; 1684 if (win > 1) { 1685 tp->snd_ssthresh = win / 2 * tp->t_maxseg; 1686 tp->snd_cwnd = tp->snd_ssthresh; 1687 tp->snd_last = tp->snd_max; 1688 tp->t_flags |= TF_SEND_CWR; 1689 tcpstat.tcps_cwr_ecn++; 1690 } 1691 } 1692 tcpstat.tcps_ecn_rcvece++; 1693 } 1694 /* 1695 * if we receive CWR, we know that the peer has reduced 1696 * its congestion window. stop sending ecn-echo. 1697 */ 1698 if ((tiflags & TH_CWR)) { 1699 tp->t_flags &= ~TF_RCVD_CE; 1700 tcpstat.tcps_ecn_rcvcwr++; 1701 } 1702 #endif /* TCP_ECN */ 1703 1704 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1705 /* 1706 * Duplicate/old ACK processing. 1707 * Increments t_dupacks: 1708 * Pure duplicate (same seq/ack/window, no data) 1709 * Doesn't affect t_dupacks: 1710 * Data packets. 1711 * Normal window updates (window opens) 1712 * Resets t_dupacks: 1713 * New data ACKed. 1714 * Window shrinks 1715 * Old ACK 1716 */ 1717 if (tlen) 1718 break; 1719 /* 1720 * If we get an old ACK, there is probably packet 1721 * reordering going on. Be conservative and reset 1722 * t_dupacks so that we are less agressive in 1723 * doing a fast retransmit. 1724 */ 1725 if (th->th_ack != tp->snd_una) { 1726 tp->t_dupacks = 0; 1727 break; 1728 } 1729 if (tiwin == tp->snd_wnd) { 1730 tcpstat.tcps_rcvdupack++; 1731 /* 1732 * If we have outstanding data (other than 1733 * a window probe), this is a completely 1734 * duplicate ack (ie, window info didn't 1735 * change), the ack is the biggest we've 1736 * seen and we've seen exactly our rexmt 1737 * threshhold of them, assume a packet 1738 * has been dropped and retransmit it. 1739 * Kludge snd_nxt & the congestion 1740 * window so we send only this one 1741 * packet. 1742 * 1743 * We know we're losing at the current 1744 * window size so do congestion avoidance 1745 * (set ssthresh to half the current window 1746 * and pull our congestion window back to 1747 * the new ssthresh). 1748 * 1749 * Dup acks mean that packets have left the 1750 * network (they're now cached at the receiver) 1751 * so bump cwnd by the amount in the receiver 1752 * to keep a constant cwnd packets in the 1753 * network. 1754 */ 1755 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0) 1756 tp->t_dupacks = 0; 1757 #if defined(TCP_SACK) && defined(TCP_FACK) 1758 /* 1759 * In FACK, can enter fast rec. if the receiver 1760 * reports a reass. queue longer than 3 segs. 1761 */ 1762 else if (++tp->t_dupacks == tcprexmtthresh || 1763 ((SEQ_GT(tp->snd_fack, tcprexmtthresh * 1764 tp->t_maxseg + tp->snd_una)) && 1765 SEQ_GT(tp->snd_una, tp->snd_last))) { 1766 #else 1767 else if (++tp->t_dupacks == tcprexmtthresh) { 1768 #endif /* TCP_FACK */ 1769 tcp_seq onxt = tp->snd_nxt; 1770 u_long win = 1771 ulmin(tp->snd_wnd, tp->snd_cwnd) / 1772 2 / tp->t_maxseg; 1773 1774 #if defined(TCP_SACK) || defined(TCP_ECN) 1775 if (SEQ_LT(th->th_ack, tp->snd_last)){ 1776 /* 1777 * False fast retx after 1778 * timeout. Do not cut window. 1779 */ 1780 tp->t_dupacks = 0; 1781 goto drop; 1782 } 1783 #endif 1784 if (win < 2) 1785 win = 2; 1786 tp->snd_ssthresh = win * tp->t_maxseg; 1787 #if defined(TCP_SACK) 1788 tp->snd_last = tp->snd_max; 1789 #endif 1790 #ifdef TCP_SACK 1791 if (!tp->sack_disable) { 1792 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1793 tp->t_rtttime = 0; 1794 #ifdef TCP_ECN 1795 tp->t_flags |= TF_SEND_CWR; 1796 #endif 1797 #if 1 /* TCP_ECN */ 1798 tcpstat.tcps_cwr_frecovery++; 1799 #endif 1800 tcpstat.tcps_sndrexmitfast++; 1801 #if defined(TCP_SACK) && defined(TCP_FACK) 1802 tp->t_dupacks = tcprexmtthresh; 1803 (void) tcp_output(tp); 1804 /* 1805 * During FR, snd_cwnd is held 1806 * constant for FACK. 1807 */ 1808 tp->snd_cwnd = tp->snd_ssthresh; 1809 #else 1810 /* 1811 * tcp_output() will send 1812 * oldest SACK-eligible rtx. 1813 */ 1814 (void) tcp_output(tp); 1815 tp->snd_cwnd = tp->snd_ssthresh+ 1816 tp->t_maxseg * tp->t_dupacks; 1817 #endif /* TCP_FACK */ 1818 goto drop; 1819 } 1820 #endif /* TCP_SACK */ 1821 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1822 tp->t_rtttime = 0; 1823 tp->snd_nxt = th->th_ack; 1824 tp->snd_cwnd = tp->t_maxseg; 1825 #ifdef TCP_ECN 1826 tp->t_flags |= TF_SEND_CWR; 1827 #endif 1828 #if 1 /* TCP_ECN */ 1829 tcpstat.tcps_cwr_frecovery++; 1830 #endif 1831 tcpstat.tcps_sndrexmitfast++; 1832 (void) tcp_output(tp); 1833 1834 tp->snd_cwnd = tp->snd_ssthresh + 1835 tp->t_maxseg * tp->t_dupacks; 1836 if (SEQ_GT(onxt, tp->snd_nxt)) 1837 tp->snd_nxt = onxt; 1838 goto drop; 1839 } else if (tp->t_dupacks > tcprexmtthresh) { 1840 #if defined(TCP_SACK) && defined(TCP_FACK) 1841 /* 1842 * while (awnd < cwnd) 1843 * sendsomething(); 1844 */ 1845 if (!tp->sack_disable) { 1846 if (tp->snd_awnd < tp->snd_cwnd) 1847 tcp_output(tp); 1848 goto drop; 1849 } 1850 #endif /* TCP_FACK */ 1851 tp->snd_cwnd += tp->t_maxseg; 1852 (void) tcp_output(tp); 1853 goto drop; 1854 } 1855 } else if (tiwin < tp->snd_wnd) { 1856 /* 1857 * The window was retracted! Previous dup 1858 * ACKs may have been due to packets arriving 1859 * after the shrunken window, not a missing 1860 * packet, so play it safe and reset t_dupacks 1861 */ 1862 tp->t_dupacks = 0; 1863 } 1864 break; 1865 } 1866 /* 1867 * If the congestion window was inflated to account 1868 * for the other side's cached packets, retract it. 1869 */ 1870 #if defined(TCP_SACK) 1871 if (!tp->sack_disable) { 1872 if (tp->t_dupacks >= tcprexmtthresh) { 1873 /* Check for a partial ACK */ 1874 if (tcp_sack_partialack(tp, th)) { 1875 #if defined(TCP_SACK) && defined(TCP_FACK) 1876 /* Force call to tcp_output */ 1877 if (tp->snd_awnd < tp->snd_cwnd) 1878 needoutput = 1; 1879 #else 1880 tp->snd_cwnd += tp->t_maxseg; 1881 needoutput = 1; 1882 #endif /* TCP_FACK */ 1883 } else { 1884 /* Out of fast recovery */ 1885 tp->snd_cwnd = tp->snd_ssthresh; 1886 if (tcp_seq_subtract(tp->snd_max, 1887 th->th_ack) < tp->snd_ssthresh) 1888 tp->snd_cwnd = 1889 tcp_seq_subtract(tp->snd_max, 1890 th->th_ack); 1891 tp->t_dupacks = 0; 1892 #if defined(TCP_SACK) && defined(TCP_FACK) 1893 if (SEQ_GT(th->th_ack, tp->snd_fack)) 1894 tp->snd_fack = th->th_ack; 1895 #endif /* TCP_FACK */ 1896 } 1897 } 1898 } else { 1899 if (tp->t_dupacks >= tcprexmtthresh && 1900 !tcp_newreno(tp, th)) { 1901 /* Out of fast recovery */ 1902 tp->snd_cwnd = tp->snd_ssthresh; 1903 if (tcp_seq_subtract(tp->snd_max, th->th_ack) < 1904 tp->snd_ssthresh) 1905 tp->snd_cwnd = 1906 tcp_seq_subtract(tp->snd_max, 1907 th->th_ack); 1908 tp->t_dupacks = 0; 1909 } 1910 } 1911 if (tp->t_dupacks < tcprexmtthresh) 1912 tp->t_dupacks = 0; 1913 #else /* else no TCP_SACK */ 1914 if (tp->t_dupacks >= tcprexmtthresh && 1915 tp->snd_cwnd > tp->snd_ssthresh) 1916 tp->snd_cwnd = tp->snd_ssthresh; 1917 tp->t_dupacks = 0; 1918 #endif 1919 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1920 tcpstat.tcps_rcvacktoomuch++; 1921 goto dropafterack; 1922 } 1923 acked = th->th_ack - tp->snd_una; 1924 tcpstat.tcps_rcvackpack++; 1925 tcpstat.tcps_rcvackbyte += acked; 1926 1927 /* 1928 * If we have a timestamp reply, update smoothed 1929 * round trip time. If no timestamp is present but 1930 * transmit timer is running and timed sequence 1931 * number was acked, update smoothed round trip time. 1932 * Since we now have an rtt measurement, cancel the 1933 * timer backoff (cf., Phil Karn's retransmit alg.). 1934 * Recompute the initial retransmit timer. 1935 */ 1936 if (ts_present) 1937 tcp_xmit_timer(tp, tcp_now-ts_ecr+1); 1938 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 1939 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 1940 1941 /* 1942 * If all outstanding data is acked, stop retransmit 1943 * timer and remember to restart (more output or persist). 1944 * If there is more data to be acked, restart retransmit 1945 * timer, using current (possibly backed-off) value. 1946 */ 1947 if (th->th_ack == tp->snd_max) { 1948 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1949 needoutput = 1; 1950 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 1951 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1952 /* 1953 * When new data is acked, open the congestion window. 1954 * If the window gives us less than ssthresh packets 1955 * in flight, open exponentially (maxseg per packet). 1956 * Otherwise open linearly: maxseg per window 1957 * (maxseg^2 / cwnd per packet). 1958 */ 1959 { 1960 u_int cw = tp->snd_cwnd; 1961 u_int incr = tp->t_maxseg; 1962 1963 if (cw > tp->snd_ssthresh) 1964 incr = incr * incr / cw; 1965 #if defined (TCP_SACK) 1966 if (tp->t_dupacks < tcprexmtthresh) 1967 #endif 1968 tp->snd_cwnd = ulmin(cw + incr, TCP_MAXWIN<<tp->snd_scale); 1969 } 1970 ND6_HINT(tp); 1971 if (acked > so->so_snd.sb_cc) { 1972 tp->snd_wnd -= so->so_snd.sb_cc; 1973 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1974 ourfinisacked = 1; 1975 } else { 1976 sbdrop(&so->so_snd, acked); 1977 tp->snd_wnd -= acked; 1978 ourfinisacked = 0; 1979 } 1980 if (sb_notify(&so->so_snd)) 1981 sowwakeup(so); 1982 tp->snd_una = th->th_ack; 1983 #ifdef TCP_ECN 1984 /* sync snd_last with snd_una */ 1985 if (SEQ_GT(tp->snd_una, tp->snd_last)) 1986 tp->snd_last = tp->snd_una; 1987 #endif 1988 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1989 tp->snd_nxt = tp->snd_una; 1990 #if defined (TCP_SACK) && defined (TCP_FACK) 1991 if (SEQ_GT(tp->snd_una, tp->snd_fack)) { 1992 tp->snd_fack = tp->snd_una; 1993 /* Update snd_awnd for partial ACK 1994 * without any SACK blocks. 1995 */ 1996 tp->snd_awnd = tcp_seq_subtract(tp->snd_nxt, 1997 tp->snd_fack) + tp->retran_data; 1998 } 1999 #endif 2000 2001 switch (tp->t_state) { 2002 2003 /* 2004 * In FIN_WAIT_1 STATE in addition to the processing 2005 * for the ESTABLISHED state if our FIN is now acknowledged 2006 * then enter FIN_WAIT_2. 2007 */ 2008 case TCPS_FIN_WAIT_1: 2009 if (ourfinisacked) { 2010 /* 2011 * If we can't receive any more 2012 * data, then closing user can proceed. 2013 * Starting the timer is contrary to the 2014 * specification, but if we don't get a FIN 2015 * we'll hang forever. 2016 */ 2017 if (so->so_state & SS_CANTRCVMORE) { 2018 soisdisconnected(so); 2019 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle); 2020 } 2021 tp->t_state = TCPS_FIN_WAIT_2; 2022 } 2023 break; 2024 2025 /* 2026 * In CLOSING STATE in addition to the processing for 2027 * the ESTABLISHED state if the ACK acknowledges our FIN 2028 * then enter the TIME-WAIT state, otherwise ignore 2029 * the segment. 2030 */ 2031 case TCPS_CLOSING: 2032 if (ourfinisacked) { 2033 tp->t_state = TCPS_TIME_WAIT; 2034 tcp_canceltimers(tp); 2035 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2036 soisdisconnected(so); 2037 } 2038 break; 2039 2040 /* 2041 * In LAST_ACK, we may still be waiting for data to drain 2042 * and/or to be acked, as well as for the ack of our FIN. 2043 * If our FIN is now acknowledged, delete the TCB, 2044 * enter the closed state and return. 2045 */ 2046 case TCPS_LAST_ACK: 2047 if (ourfinisacked) { 2048 tp = tcp_close(tp); 2049 goto drop; 2050 } 2051 break; 2052 2053 /* 2054 * In TIME_WAIT state the only thing that should arrive 2055 * is a retransmission of the remote FIN. Acknowledge 2056 * it and restart the finack timer. 2057 */ 2058 case TCPS_TIME_WAIT: 2059 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2060 goto dropafterack; 2061 } 2062 } 2063 2064 step6: 2065 /* 2066 * Update window information. 2067 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2068 */ 2069 if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) || 2070 (tp->snd_wl1 == th->th_seq && SEQ_LT(tp->snd_wl2, th->th_ack)) || 2071 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))) { 2072 /* keep track of pure window updates */ 2073 if (tlen == 0 && 2074 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2075 tcpstat.tcps_rcvwinupd++; 2076 tp->snd_wnd = tiwin; 2077 tp->snd_wl1 = th->th_seq; 2078 tp->snd_wl2 = th->th_ack; 2079 if (tp->snd_wnd > tp->max_sndwnd) 2080 tp->max_sndwnd = tp->snd_wnd; 2081 needoutput = 1; 2082 } 2083 2084 /* 2085 * Process segments with URG. 2086 */ 2087 if ((tiflags & TH_URG) && th->th_urp && 2088 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2089 /* 2090 * This is a kludge, but if we receive and accept 2091 * random urgent pointers, we'll crash in 2092 * soreceive. It's hard to imagine someone 2093 * actually wanting to send this much urgent data. 2094 */ 2095 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2096 th->th_urp = 0; /* XXX */ 2097 tiflags &= ~TH_URG; /* XXX */ 2098 goto dodata; /* XXX */ 2099 } 2100 /* 2101 * If this segment advances the known urgent pointer, 2102 * then mark the data stream. This should not happen 2103 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2104 * a FIN has been received from the remote side. 2105 * In these states we ignore the URG. 2106 * 2107 * According to RFC961 (Assigned Protocols), 2108 * the urgent pointer points to the last octet 2109 * of urgent data. We continue, however, 2110 * to consider it to indicate the first octet 2111 * of data past the urgent section as the original 2112 * spec states (in one of two places). 2113 */ 2114 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2115 tp->rcv_up = th->th_seq + th->th_urp; 2116 so->so_oobmark = so->so_rcv.sb_cc + 2117 (tp->rcv_up - tp->rcv_nxt) - 1; 2118 if (so->so_oobmark == 0) 2119 so->so_state |= SS_RCVATMARK; 2120 sohasoutofband(so); 2121 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2122 } 2123 /* 2124 * Remove out of band data so doesn't get presented to user. 2125 * This can happen independent of advancing the URG pointer, 2126 * but if two URG's are pending at once, some out-of-band 2127 * data may creep in... ick. 2128 */ 2129 if (th->th_urp <= (u_int16_t) tlen 2130 #ifdef SO_OOBINLINE 2131 && (so->so_options & SO_OOBINLINE) == 0 2132 #endif 2133 ) 2134 tcp_pulloutofband(so, th->th_urp, m, hdroptlen); 2135 } else 2136 /* 2137 * If no out of band data is expected, 2138 * pull receive urgent pointer along 2139 * with the receive window. 2140 */ 2141 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2142 tp->rcv_up = tp->rcv_nxt; 2143 dodata: /* XXX */ 2144 2145 /* 2146 * Process the segment text, merging it into the TCP sequencing queue, 2147 * and arranging for acknowledgment of receipt if necessary. 2148 * This process logically involves adjusting tp->rcv_wnd as data 2149 * is presented to the user (this happens in tcp_usrreq.c, 2150 * case PRU_RCVD). If a FIN has already been received on this 2151 * connection then we just ignore the text. 2152 */ 2153 if ((tlen || (tiflags & TH_FIN)) && 2154 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2155 if (th->th_seq == tp->rcv_nxt && tp->segq.lh_first == NULL && 2156 tp->t_state == TCPS_ESTABLISHED) { 2157 TCP_SETUP_ACK(tp, tiflags); 2158 tp->rcv_nxt += tlen; 2159 tiflags = th->th_flags & TH_FIN; 2160 tcpstat.tcps_rcvpack++; 2161 tcpstat.tcps_rcvbyte += tlen; 2162 ND6_HINT(tp); 2163 if (so->so_state & SS_CANTRCVMORE) 2164 m_freem(m); 2165 else { 2166 m_adj(m, hdroptlen); 2167 sbappendstream(&so->so_rcv, m); 2168 } 2169 sorwakeup(so); 2170 } else { 2171 m_adj(m, hdroptlen); 2172 tiflags = tcp_reass(tp, th, m, &tlen); 2173 tp->t_flags |= TF_ACKNOW; 2174 } 2175 #ifdef TCP_SACK 2176 if (!tp->sack_disable) 2177 tcp_update_sack_list(tp); 2178 #endif 2179 2180 /* 2181 * variable len never referenced again in modern BSD, 2182 * so why bother computing it ?? 2183 */ 2184 #if 0 2185 /* 2186 * Note the amount of data that peer has sent into 2187 * our window, in order to estimate the sender's 2188 * buffer size. 2189 */ 2190 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2191 #endif /* 0 */ 2192 } else { 2193 m_freem(m); 2194 tiflags &= ~TH_FIN; 2195 } 2196 2197 /* 2198 * If FIN is received ACK the FIN and let the user know 2199 * that the connection is closing. Ignore a FIN received before 2200 * the connection is fully established. 2201 */ 2202 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) { 2203 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2204 socantrcvmore(so); 2205 tp->t_flags |= TF_ACKNOW; 2206 tp->rcv_nxt++; 2207 } 2208 switch (tp->t_state) { 2209 2210 /* 2211 * In ESTABLISHED STATE enter the CLOSE_WAIT state. 2212 */ 2213 case TCPS_ESTABLISHED: 2214 tp->t_state = TCPS_CLOSE_WAIT; 2215 break; 2216 2217 /* 2218 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2219 * enter the CLOSING state. 2220 */ 2221 case TCPS_FIN_WAIT_1: 2222 tp->t_state = TCPS_CLOSING; 2223 break; 2224 2225 /* 2226 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2227 * starting the time-wait timer, turning off the other 2228 * standard timers. 2229 */ 2230 case TCPS_FIN_WAIT_2: 2231 tp->t_state = TCPS_TIME_WAIT; 2232 tcp_canceltimers(tp); 2233 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2234 soisdisconnected(so); 2235 break; 2236 2237 /* 2238 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2239 */ 2240 case TCPS_TIME_WAIT: 2241 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2242 break; 2243 } 2244 } 2245 if (so->so_options & SO_DEBUG) { 2246 switch (tp->pf == PF_INET6) { 2247 #ifdef INET6 2248 case PF_INET6: 2249 tcp_trace(TA_INPUT, ostate, tp, (caddr_t) &tcp_saveti6, 2250 0, tlen); 2251 break; 2252 #endif /* INET6 */ 2253 case PF_INET: 2254 tcp_trace(TA_INPUT, ostate, tp, (caddr_t) &tcp_saveti, 2255 0, tlen); 2256 break; 2257 } 2258 } 2259 2260 /* 2261 * Return any desired output. 2262 */ 2263 if (needoutput || (tp->t_flags & TF_ACKNOW)) { 2264 (void) tcp_output(tp); 2265 } 2266 return; 2267 2268 dropafterack: 2269 /* 2270 * Generate an ACK dropping incoming segment if it occupies 2271 * sequence space, where the ACK reflects our state. 2272 */ 2273 if (tiflags & TH_RST) 2274 goto drop; 2275 m_freem(m); 2276 tp->t_flags |= TF_ACKNOW; 2277 (void) tcp_output(tp); 2278 return; 2279 2280 dropwithreset_ratelim: 2281 /* 2282 * We may want to rate-limit RSTs in certain situations, 2283 * particularly if we are sending an RST in response to 2284 * an attempt to connect to or otherwise communicate with 2285 * a port for which we have no socket. 2286 */ 2287 if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count, 2288 tcp_rst_ppslim) == 0) { 2289 /* XXX stat */ 2290 goto drop; 2291 } 2292 /* ...fall into dropwithreset... */ 2293 2294 dropwithreset: 2295 /* 2296 * Generate a RST, dropping incoming segment. 2297 * Make ACK acceptable to originator of segment. 2298 * Don't bother to respond if destination was broadcast/multicast. 2299 */ 2300 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2301 goto drop; 2302 switch (af) { 2303 #ifdef INET6 2304 case AF_INET6: 2305 /* For following calls to tcp_respond */ 2306 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 2307 goto drop; 2308 break; 2309 #endif /* INET6 */ 2310 case AF_INET: 2311 if (IN_MULTICAST(ip->ip_dst.s_addr) || 2312 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2313 goto drop; 2314 } 2315 if (tiflags & TH_ACK) { 2316 tcp_respond(tp, mtod(m, caddr_t), m, (tcp_seq)0, th->th_ack, 2317 TH_RST); 2318 } else { 2319 if (tiflags & TH_SYN) 2320 tlen++; 2321 tcp_respond(tp, mtod(m, caddr_t), m, th->th_seq + tlen, 2322 (tcp_seq)0, TH_RST|TH_ACK); 2323 } 2324 /* destroy temporarily created socket */ 2325 if (dropsocket) 2326 (void) soabort(so); 2327 return; 2328 2329 drop: 2330 /* 2331 * Drop space held by incoming segment and return. 2332 */ 2333 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) { 2334 switch (tp->pf) { 2335 #ifdef INET6 2336 case PF_INET6: 2337 tcp_trace(TA_DROP, ostate, tp, (caddr_t) &tcp_saveti6, 2338 0, tlen); 2339 break; 2340 #endif /* INET6 */ 2341 case PF_INET: 2342 tcp_trace(TA_DROP, ostate, tp, (caddr_t) &tcp_saveti, 2343 0, tlen); 2344 break; 2345 } 2346 } 2347 2348 m_freem(m); 2349 /* destroy temporarily created socket */ 2350 if (dropsocket) 2351 (void) soabort(so); 2352 return; 2353 #ifndef TUBA_INCLUDE 2354 } 2355 2356 void 2357 tcp_dooptions(tp, cp, cnt, th, ts_present, ts_val, ts_ecr) 2358 struct tcpcb *tp; 2359 u_char *cp; 2360 int cnt; 2361 struct tcphdr *th; 2362 int *ts_present; 2363 u_int32_t *ts_val, *ts_ecr; 2364 { 2365 u_int16_t mss = 0; 2366 int opt, optlen; 2367 2368 for (; cnt > 0; cnt -= optlen, cp += optlen) { 2369 opt = cp[0]; 2370 if (opt == TCPOPT_EOL) 2371 break; 2372 if (opt == TCPOPT_NOP) 2373 optlen = 1; 2374 else { 2375 if (cnt < 2) 2376 break; 2377 optlen = cp[1]; 2378 if (optlen < 2 || optlen > cnt) 2379 break; 2380 } 2381 switch (opt) { 2382 2383 default: 2384 continue; 2385 2386 case TCPOPT_MAXSEG: 2387 if (optlen != TCPOLEN_MAXSEG) 2388 continue; 2389 if (!(th->th_flags & TH_SYN)) 2390 continue; 2391 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 2392 NTOHS(mss); 2393 break; 2394 2395 case TCPOPT_WINDOW: 2396 if (optlen != TCPOLEN_WINDOW) 2397 continue; 2398 if (!(th->th_flags & TH_SYN)) 2399 continue; 2400 tp->t_flags |= TF_RCVD_SCALE; 2401 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2402 break; 2403 2404 case TCPOPT_TIMESTAMP: 2405 if (optlen != TCPOLEN_TIMESTAMP) 2406 continue; 2407 *ts_present = 1; 2408 bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val)); 2409 NTOHL(*ts_val); 2410 bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr)); 2411 NTOHL(*ts_ecr); 2412 2413 /* 2414 * A timestamp received in a SYN makes 2415 * it ok to send timestamp requests and replies. 2416 */ 2417 if (th->th_flags & TH_SYN) { 2418 tp->t_flags |= TF_RCVD_TSTMP; 2419 tp->ts_recent = *ts_val; 2420 tp->ts_recent_age = tcp_now; 2421 } 2422 break; 2423 2424 #ifdef TCP_SACK 2425 case TCPOPT_SACK_PERMITTED: 2426 if (tp->sack_disable || optlen!=TCPOLEN_SACK_PERMITTED) 2427 continue; 2428 if (th->th_flags & TH_SYN) 2429 /* MUST only be set on SYN */ 2430 tp->t_flags |= TF_SACK_PERMIT; 2431 break; 2432 case TCPOPT_SACK: 2433 if (tcp_sack_option(tp, th, cp, optlen)) 2434 continue; 2435 break; 2436 #endif 2437 } 2438 } 2439 /* Update t_maxopd and t_maxseg after all options are processed */ 2440 if (th->th_flags & TH_SYN) { 2441 (void) tcp_mss(tp, mss); /* sets t_maxseg */ 2442 2443 if (mss) 2444 tcp_mss_update(tp); 2445 } 2446 } 2447 2448 #if defined(TCP_SACK) 2449 u_long 2450 tcp_seq_subtract(a, b) 2451 u_long a, b; 2452 { 2453 return ((long)(a - b)); 2454 } 2455 #endif 2456 2457 2458 #ifdef TCP_SACK 2459 /* 2460 * This function is called upon receipt of new valid data (while not in header 2461 * prediction mode), and it updates the ordered list of sacks. 2462 */ 2463 void 2464 tcp_update_sack_list(tp) 2465 struct tcpcb *tp; 2466 { 2467 /* 2468 * First reported block MUST be the most recent one. Subsequent 2469 * blocks SHOULD be in the order in which they arrived at the 2470 * receiver. These two conditions make the implementation fully 2471 * compliant with RFC 2018. 2472 */ 2473 int i, j = 0, count = 0, lastpos = -1; 2474 struct sackblk sack, firstsack, temp[MAX_SACK_BLKS]; 2475 2476 /* First clean up current list of sacks */ 2477 for (i = 0; i < tp->rcv_numsacks; i++) { 2478 sack = tp->sackblks[i]; 2479 if (sack.start == 0 && sack.end == 0) { 2480 count++; /* count = number of blocks to be discarded */ 2481 continue; 2482 } 2483 if (SEQ_LEQ(sack.end, tp->rcv_nxt)) { 2484 tp->sackblks[i].start = tp->sackblks[i].end = 0; 2485 count++; 2486 } else { 2487 temp[j].start = tp->sackblks[i].start; 2488 temp[j++].end = tp->sackblks[i].end; 2489 } 2490 } 2491 tp->rcv_numsacks -= count; 2492 if (tp->rcv_numsacks == 0) { /* no sack blocks currently (fast path) */ 2493 tcp_clean_sackreport(tp); 2494 if (SEQ_LT(tp->rcv_nxt, tp->rcv_laststart)) { 2495 /* ==> need first sack block */ 2496 tp->sackblks[0].start = tp->rcv_laststart; 2497 tp->sackblks[0].end = tp->rcv_lastend; 2498 tp->rcv_numsacks = 1; 2499 } 2500 return; 2501 } 2502 /* Otherwise, sack blocks are already present. */ 2503 for (i = 0; i < tp->rcv_numsacks; i++) 2504 tp->sackblks[i] = temp[i]; /* first copy back sack list */ 2505 if (SEQ_GEQ(tp->rcv_nxt, tp->rcv_lastend)) 2506 return; /* sack list remains unchanged */ 2507 /* 2508 * From here, segment just received should be (part of) the 1st sack. 2509 * Go through list, possibly coalescing sack block entries. 2510 */ 2511 firstsack.start = tp->rcv_laststart; 2512 firstsack.end = tp->rcv_lastend; 2513 for (i = 0; i < tp->rcv_numsacks; i++) { 2514 sack = tp->sackblks[i]; 2515 if (SEQ_LT(sack.end, firstsack.start) || 2516 SEQ_GT(sack.start, firstsack.end)) 2517 continue; /* no overlap */ 2518 if (sack.start == firstsack.start && sack.end == firstsack.end){ 2519 /* 2520 * identical block; delete it here since we will 2521 * move it to the front of the list. 2522 */ 2523 tp->sackblks[i].start = tp->sackblks[i].end = 0; 2524 lastpos = i; /* last posn with a zero entry */ 2525 continue; 2526 } 2527 if (SEQ_LEQ(sack.start, firstsack.start)) 2528 firstsack.start = sack.start; /* merge blocks */ 2529 if (SEQ_GEQ(sack.end, firstsack.end)) 2530 firstsack.end = sack.end; /* merge blocks */ 2531 tp->sackblks[i].start = tp->sackblks[i].end = 0; 2532 lastpos = i; /* last posn with a zero entry */ 2533 } 2534 if (lastpos != -1) { /* at least one merge */ 2535 for (i = 0, j = 1; i < tp->rcv_numsacks; i++) { 2536 sack = tp->sackblks[i]; 2537 if (sack.start == 0 && sack.end == 0) 2538 continue; 2539 temp[j++] = sack; 2540 } 2541 tp->rcv_numsacks = j; /* including first blk (added later) */ 2542 for (i = 1; i < tp->rcv_numsacks; i++) /* now copy back */ 2543 tp->sackblks[i] = temp[i]; 2544 } else { /* no merges -- shift sacks by 1 */ 2545 if (tp->rcv_numsacks < MAX_SACK_BLKS) 2546 tp->rcv_numsacks++; 2547 for (i = tp->rcv_numsacks-1; i > 0; i--) 2548 tp->sackblks[i] = tp->sackblks[i-1]; 2549 } 2550 tp->sackblks[0] = firstsack; 2551 return; 2552 } 2553 2554 /* 2555 * Process the TCP SACK option. Returns 1 if tcp_dooptions() should continue, 2556 * and 0 otherwise, if the option was fine. tp->snd_holes is an ordered list 2557 * of holes (oldest to newest, in terms of the sequence space). 2558 */ 2559 int 2560 tcp_sack_option(struct tcpcb *tp, struct tcphdr *th, u_char *cp, int optlen) 2561 { 2562 int tmp_olen; 2563 u_char *tmp_cp; 2564 struct sackhole *cur, *p, *temp; 2565 2566 if (tp->sack_disable) 2567 return (1); 2568 2569 /* Note: TCPOLEN_SACK must be 2*sizeof(tcp_seq) */ 2570 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 2571 return (1); 2572 tmp_cp = cp + 2; 2573 tmp_olen = optlen - 2; 2574 if (tp->snd_numholes < 0) 2575 tp->snd_numholes = 0; 2576 if (tp->t_maxseg == 0) 2577 panic("tcp_sack_option"); /* Should never happen */ 2578 while (tmp_olen > 0) { 2579 struct sackblk sack; 2580 2581 bcopy(tmp_cp, (char *) &(sack.start), sizeof(tcp_seq)); 2582 NTOHL(sack.start); 2583 bcopy(tmp_cp + sizeof(tcp_seq), 2584 (char *) &(sack.end), sizeof(tcp_seq)); 2585 NTOHL(sack.end); 2586 tmp_olen -= TCPOLEN_SACK; 2587 tmp_cp += TCPOLEN_SACK; 2588 if (SEQ_LEQ(sack.end, sack.start)) 2589 continue; /* bad SACK fields */ 2590 if (SEQ_LEQ(sack.end, tp->snd_una)) 2591 continue; /* old block */ 2592 #if defined(TCP_SACK) && defined(TCP_FACK) 2593 /* Updates snd_fack. */ 2594 if (SEQ_GT(sack.end, tp->snd_fack)) 2595 tp->snd_fack = sack.end; 2596 #endif /* TCP_FACK */ 2597 if (SEQ_GT(th->th_ack, tp->snd_una)) { 2598 if (SEQ_LT(sack.start, th->th_ack)) 2599 continue; 2600 } 2601 if (SEQ_GT(sack.end, tp->snd_max)) 2602 continue; 2603 if (tp->snd_holes == NULL) { /* first hole */ 2604 tp->snd_holes = (struct sackhole *) 2605 pool_get(&sackhl_pool, PR_NOWAIT); 2606 if (tp->snd_holes == NULL) { 2607 /* ENOBUFS, so ignore SACKed block for now*/ 2608 continue; 2609 } 2610 cur = tp->snd_holes; 2611 cur->start = th->th_ack; 2612 cur->end = sack.start; 2613 cur->rxmit = cur->start; 2614 cur->next = NULL; 2615 tp->snd_numholes = 1; 2616 tp->rcv_lastsack = sack.end; 2617 /* 2618 * dups is at least one. If more data has been 2619 * SACKed, it can be greater than one. 2620 */ 2621 cur->dups = min(tcprexmtthresh, 2622 ((sack.end - cur->end)/tp->t_maxseg)); 2623 if (cur->dups < 1) 2624 cur->dups = 1; 2625 continue; /* with next sack block */ 2626 } 2627 /* Go thru list of holes: p = previous, cur = current */ 2628 p = cur = tp->snd_holes; 2629 while (cur) { 2630 if (SEQ_LEQ(sack.end, cur->start)) 2631 /* SACKs data before the current hole */ 2632 break; /* no use going through more holes */ 2633 if (SEQ_GEQ(sack.start, cur->end)) { 2634 /* SACKs data beyond the current hole */ 2635 cur->dups++; 2636 if (((sack.end - cur->end)/tp->t_maxseg) >= 2637 tcprexmtthresh) 2638 cur->dups = tcprexmtthresh; 2639 p = cur; 2640 cur = cur->next; 2641 continue; 2642 } 2643 if (SEQ_LEQ(sack.start, cur->start)) { 2644 /* Data acks at least the beginning of hole */ 2645 #if defined(TCP_SACK) && defined(TCP_FACK) 2646 if (SEQ_GT(sack.end, cur->rxmit)) 2647 tp->retran_data -= 2648 tcp_seq_subtract(cur->rxmit, 2649 cur->start); 2650 else 2651 tp->retran_data -= 2652 tcp_seq_subtract(sack.end, 2653 cur->start); 2654 #endif /* TCP_FACK */ 2655 if (SEQ_GEQ(sack.end, cur->end)) { 2656 /* Acks entire hole, so delete hole */ 2657 if (p != cur) { 2658 p->next = cur->next; 2659 pool_put(&sackhl_pool, cur); 2660 cur = p->next; 2661 } else { 2662 cur = cur->next; 2663 pool_put(&sackhl_pool, p); 2664 p = cur; 2665 tp->snd_holes = p; 2666 } 2667 tp->snd_numholes--; 2668 continue; 2669 } 2670 /* otherwise, move start of hole forward */ 2671 cur->start = sack.end; 2672 cur->rxmit = max (cur->rxmit, cur->start); 2673 p = cur; 2674 cur = cur->next; 2675 continue; 2676 } 2677 /* move end of hole backward */ 2678 if (SEQ_GEQ(sack.end, cur->end)) { 2679 #if defined(TCP_SACK) && defined(TCP_FACK) 2680 if (SEQ_GT(cur->rxmit, sack.start)) 2681 tp->retran_data -= 2682 tcp_seq_subtract(cur->rxmit, 2683 sack.start); 2684 #endif /* TCP_FACK */ 2685 cur->end = sack.start; 2686 cur->rxmit = min(cur->rxmit, cur->end); 2687 cur->dups++; 2688 if (((sack.end - cur->end)/tp->t_maxseg) >= 2689 tcprexmtthresh) 2690 cur->dups = tcprexmtthresh; 2691 p = cur; 2692 cur = cur->next; 2693 continue; 2694 } 2695 if (SEQ_LT(cur->start, sack.start) && 2696 SEQ_GT(cur->end, sack.end)) { 2697 /* 2698 * ACKs some data in middle of a hole; need to 2699 * split current hole 2700 */ 2701 temp = (struct sackhole *) 2702 pool_get(&sackhl_pool, PR_NOWAIT); 2703 if (temp == NULL) 2704 continue; /* ENOBUFS */ 2705 #if defined(TCP_SACK) && defined(TCP_FACK) 2706 if (SEQ_GT(cur->rxmit, sack.end)) 2707 tp->retran_data -= 2708 tcp_seq_subtract(sack.end, 2709 sack.start); 2710 else if (SEQ_GT(cur->rxmit, sack.start)) 2711 tp->retran_data -= 2712 tcp_seq_subtract(cur->rxmit, 2713 sack.start); 2714 #endif /* TCP_FACK */ 2715 temp->next = cur->next; 2716 temp->start = sack.end; 2717 temp->end = cur->end; 2718 temp->dups = cur->dups; 2719 temp->rxmit = max(cur->rxmit, temp->start); 2720 cur->end = sack.start; 2721 cur->rxmit = min(cur->rxmit, cur->end); 2722 cur->dups++; 2723 if (((sack.end - cur->end)/tp->t_maxseg) >= 2724 tcprexmtthresh) 2725 cur->dups = tcprexmtthresh; 2726 cur->next = temp; 2727 p = temp; 2728 cur = p->next; 2729 tp->snd_numholes++; 2730 } 2731 } 2732 /* At this point, p points to the last hole on the list */ 2733 if (SEQ_LT(tp->rcv_lastsack, sack.start)) { 2734 /* 2735 * Need to append new hole at end. 2736 * Last hole is p (and it's not NULL). 2737 */ 2738 temp = (struct sackhole *) 2739 pool_get(&sackhl_pool, PR_NOWAIT); 2740 if (temp == NULL) 2741 continue; /* ENOBUFS */ 2742 temp->start = tp->rcv_lastsack; 2743 temp->end = sack.start; 2744 temp->dups = min(tcprexmtthresh, 2745 ((sack.end - sack.start)/tp->t_maxseg)); 2746 if (temp->dups < 1) 2747 temp->dups = 1; 2748 temp->rxmit = temp->start; 2749 temp->next = 0; 2750 p->next = temp; 2751 tp->rcv_lastsack = sack.end; 2752 tp->snd_numholes++; 2753 } 2754 } 2755 #if defined(TCP_SACK) && defined(TCP_FACK) 2756 /* 2757 * Update retran_data and snd_awnd. Go through the list of 2758 * holes. Increment retran_data by (hole->rxmit - hole->start). 2759 */ 2760 tp->retran_data = 0; 2761 cur = tp->snd_holes; 2762 while (cur) { 2763 tp->retran_data += cur->rxmit - cur->start; 2764 cur = cur->next; 2765 } 2766 tp->snd_awnd = tcp_seq_subtract(tp->snd_nxt, tp->snd_fack) + 2767 tp->retran_data; 2768 #endif /* TCP_FACK */ 2769 2770 return (0); 2771 } 2772 2773 /* 2774 * Delete stale (i.e, cumulatively ack'd) holes. Hole is deleted only if 2775 * it is completely acked; otherwise, tcp_sack_option(), called from 2776 * tcp_dooptions(), will fix up the hole. 2777 */ 2778 void 2779 tcp_del_sackholes(tp, th) 2780 struct tcpcb *tp; 2781 struct tcphdr *th; 2782 { 2783 if (!tp->sack_disable && tp->t_state != TCPS_LISTEN) { 2784 /* max because this could be an older ack just arrived */ 2785 tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ? 2786 th->th_ack : tp->snd_una; 2787 struct sackhole *cur = tp->snd_holes; 2788 struct sackhole *prev; 2789 while (cur) 2790 if (SEQ_LEQ(cur->end, lastack)) { 2791 prev = cur; 2792 cur = cur->next; 2793 pool_put(&sackhl_pool, prev); 2794 tp->snd_numholes--; 2795 } else if (SEQ_LT(cur->start, lastack)) { 2796 cur->start = lastack; 2797 if (SEQ_LT(cur->rxmit, cur->start)) 2798 cur->rxmit = cur->start; 2799 break; 2800 } else 2801 break; 2802 tp->snd_holes = cur; 2803 } 2804 } 2805 2806 /* 2807 * Delete all receiver-side SACK information. 2808 */ 2809 void 2810 tcp_clean_sackreport(tp) 2811 struct tcpcb *tp; 2812 { 2813 int i; 2814 2815 tp->rcv_numsacks = 0; 2816 for (i = 0; i < MAX_SACK_BLKS; i++) 2817 tp->sackblks[i].start = tp->sackblks[i].end=0; 2818 2819 } 2820 2821 /* 2822 * Checks for partial ack. If partial ack arrives, turn off retransmission 2823 * timer, deflate the window, do not clear tp->t_dupacks, and return 1. 2824 * If the ack advances at least to tp->snd_last, return 0. 2825 */ 2826 int 2827 tcp_sack_partialack(tp, th) 2828 struct tcpcb *tp; 2829 struct tcphdr *th; 2830 { 2831 if (SEQ_LT(th->th_ack, tp->snd_last)) { 2832 /* Turn off retx. timer (will start again next segment) */ 2833 TCP_TIMER_DISARM(tp, TCPT_REXMT); 2834 tp->t_rtttime = 0; 2835 #ifndef TCP_FACK 2836 /* 2837 * Partial window deflation. This statement relies on the 2838 * fact that tp->snd_una has not been updated yet. In FACK 2839 * hold snd_cwnd constant during fast recovery. 2840 */ 2841 if (tp->snd_cwnd > (th->th_ack - tp->snd_una)) { 2842 tp->snd_cwnd -= th->th_ack - tp->snd_una; 2843 tp->snd_cwnd += tp->t_maxseg; 2844 } else 2845 tp->snd_cwnd = tp->t_maxseg; 2846 #endif 2847 return (1); 2848 } 2849 return (0); 2850 } 2851 #endif /* TCP_SACK */ 2852 2853 /* 2854 * Pull out of band byte out of a segment so 2855 * it doesn't appear in the user's data queue. 2856 * It is still reflected in the segment length for 2857 * sequencing purposes. 2858 */ 2859 void 2860 tcp_pulloutofband(so, urgent, m, off) 2861 struct socket *so; 2862 u_int urgent; 2863 struct mbuf *m; 2864 int off; 2865 { 2866 int cnt = off + urgent - 1; 2867 2868 while (cnt >= 0) { 2869 if (m->m_len > cnt) { 2870 char *cp = mtod(m, caddr_t) + cnt; 2871 struct tcpcb *tp = sototcpcb(so); 2872 2873 tp->t_iobc = *cp; 2874 tp->t_oobflags |= TCPOOB_HAVEDATA; 2875 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2876 m->m_len--; 2877 return; 2878 } 2879 cnt -= m->m_len; 2880 m = m->m_next; 2881 if (m == 0) 2882 break; 2883 } 2884 panic("tcp_pulloutofband"); 2885 } 2886 2887 /* 2888 * Collect new round-trip time estimate 2889 * and update averages and current timeout. 2890 */ 2891 void 2892 tcp_xmit_timer(tp, rtt) 2893 struct tcpcb *tp; 2894 short rtt; 2895 { 2896 short delta; 2897 short rttmin; 2898 2899 tcpstat.tcps_rttupdated++; 2900 --rtt; 2901 if (tp->t_srtt != 0) { 2902 /* 2903 * srtt is stored as fixed point with 3 bits after the 2904 * binary point (i.e., scaled by 8). The following magic 2905 * is equivalent to the smoothing algorithm in rfc793 with 2906 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2907 * point). Adjust rtt to origin 0. 2908 */ 2909 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT); 2910 if ((tp->t_srtt += delta) <= 0) 2911 tp->t_srtt = 1; 2912 /* 2913 * We accumulate a smoothed rtt variance (actually, a 2914 * smoothed mean difference), then set the retransmit 2915 * timer to smoothed rtt + 4 times the smoothed variance. 2916 * rttvar is stored as fixed point with 2 bits after the 2917 * binary point (scaled by 4). The following is 2918 * equivalent to rfc793 smoothing with an alpha of .75 2919 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2920 * rfc793's wired-in beta. 2921 */ 2922 if (delta < 0) 2923 delta = -delta; 2924 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 2925 if ((tp->t_rttvar += delta) <= 0) 2926 tp->t_rttvar = 1; 2927 } else { 2928 /* 2929 * No rtt measurement yet - use the unsmoothed rtt. 2930 * Set the variance to half the rtt (so our first 2931 * retransmit happens at 3*rtt). 2932 */ 2933 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2); 2934 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1); 2935 } 2936 tp->t_rtttime = 0; 2937 tp->t_rxtshift = 0; 2938 2939 /* 2940 * the retransmit should happen at rtt + 4 * rttvar. 2941 * Because of the way we do the smoothing, srtt and rttvar 2942 * will each average +1/2 tick of bias. When we compute 2943 * the retransmit timer, we want 1/2 tick of rounding and 2944 * 1 extra tick because of +-1/2 tick uncertainty in the 2945 * firing of the timer. The bias will give us exactly the 2946 * 1.5 tick we need. But, because the bias is 2947 * statistical, we have to test that we don't drop below 2948 * the minimum feasible timer (which is 2 ticks). 2949 */ 2950 if (tp->t_rttmin > rtt + 2) 2951 rttmin = tp->t_rttmin; 2952 else 2953 rttmin = rtt + 2; 2954 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), rttmin, TCPTV_REXMTMAX); 2955 2956 /* 2957 * We received an ack for a packet that wasn't retransmitted; 2958 * it is probably safe to discard any error indications we've 2959 * received recently. This isn't quite right, but close enough 2960 * for now (a route might have failed after we sent a segment, 2961 * and the return path might not be symmetrical). 2962 */ 2963 tp->t_softerror = 0; 2964 } 2965 2966 /* 2967 * Determine a reasonable value for maxseg size. 2968 * If the route is known, check route for mtu. 2969 * If none, use an mss that can be handled on the outgoing 2970 * interface without forcing IP to fragment; if bigger than 2971 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2972 * to utilize large mbufs. If no route is found, route has no mtu, 2973 * or the destination isn't local, use a default, hopefully conservative 2974 * size (usually 512 or the default IP max size, but no more than the mtu 2975 * of the interface), as we can't discover anything about intervening 2976 * gateways or networks. We also initialize the congestion/slow start 2977 * window to be a single segment if the destination isn't local. 2978 * While looking at the routing entry, we also initialize other path-dependent 2979 * parameters from pre-set or cached values in the routing entry. 2980 * 2981 * Also take into account the space needed for options that we 2982 * send regularly. Make maxseg shorter by that amount to assure 2983 * that we can send maxseg amount of data even when the options 2984 * are present. Store the upper limit of the length of options plus 2985 * data in maxopd. 2986 * 2987 * NOTE: offer == -1 indicates that the maxseg size changed due to 2988 * Path MTU discovery. 2989 */ 2990 int 2991 tcp_mss(tp, offer) 2992 struct tcpcb *tp; 2993 int offer; 2994 { 2995 struct rtentry *rt; 2996 struct ifnet *ifp; 2997 int mss, mssopt; 2998 int iphlen; 2999 struct inpcb *inp; 3000 3001 inp = tp->t_inpcb; 3002 3003 mssopt = mss = tcp_mssdflt; 3004 3005 rt = in_pcbrtentry(inp); 3006 3007 if (rt == NULL) 3008 goto out; 3009 3010 ifp = rt->rt_ifp; 3011 3012 switch (tp->pf) { 3013 #ifdef INET6 3014 case AF_INET6: 3015 iphlen = sizeof(struct ip6_hdr); 3016 break; 3017 #endif 3018 case AF_INET: 3019 iphlen = sizeof(struct ip); 3020 break; 3021 default: 3022 /* the family does not support path MTU discovery */ 3023 goto out; 3024 } 3025 3026 #ifdef RTV_MTU 3027 /* 3028 * if there's an mtu associated with the route and we support 3029 * path MTU discovery for the underlying protocol family, use it. 3030 */ 3031 if (rt->rt_rmx.rmx_mtu) { 3032 /* 3033 * One may wish to lower MSS to take into account options, 3034 * especially security-related options. 3035 */ 3036 mss = rt->rt_rmx.rmx_mtu - iphlen - sizeof(struct tcphdr); 3037 } else 3038 #endif /* RTV_MTU */ 3039 if (!ifp) 3040 /* 3041 * ifp may be null and rmx_mtu may be zero in certain 3042 * v6 cases (e.g., if ND wasn't able to resolve the 3043 * destination host. 3044 */ 3045 goto out; 3046 else if (ifp->if_flags & IFF_LOOPBACK) 3047 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 3048 else if (tp->pf == AF_INET) { 3049 if (ip_mtudisc) 3050 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 3051 else if (inp && in_localaddr(inp->inp_faddr)) 3052 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 3053 } 3054 #ifdef INET6 3055 else if (tp->pf == AF_INET6) { 3056 /* 3057 * for IPv6, path MTU discovery is always turned on, 3058 * or the node must use packet size <= 1280. 3059 */ 3060 mss = IN6_LINKMTU(ifp) - iphlen - sizeof(struct tcphdr); 3061 } 3062 #endif /* INET6 */ 3063 3064 /* Calculate the value that we offer in TCPOPT_MAXSEG */ 3065 if (offer != -1) { 3066 mssopt = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 3067 mssopt = max(tcp_mssdflt, mssopt); 3068 } 3069 3070 out: 3071 /* 3072 * The current mss, t_maxseg, is initialized to the default value. 3073 * If we compute a smaller value, reduce the current mss. 3074 * If we compute a larger value, return it for use in sending 3075 * a max seg size option, but don't store it for use 3076 * unless we received an offer at least that large from peer. 3077 * However, do not accept offers under 64 bytes. 3078 */ 3079 if (offer > 0) 3080 tp->t_peermss = offer; 3081 if (tp->t_peermss) 3082 mss = min(mss, tp->t_peermss); 3083 mss = max(mss, 64); /* sanity - at least max opt. space */ 3084 3085 /* 3086 * maxopd stores the maximum length of data AND options 3087 * in a segment; maxseg is the amount of data in a normal 3088 * segment. We need to store this value (maxopd) apart 3089 * from maxseg, because now every segment carries options 3090 * and thus we normally have somewhat less data in segments. 3091 */ 3092 tp->t_maxopd = mss; 3093 3094 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3095 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 3096 mss -= TCPOLEN_TSTAMP_APPA; 3097 3098 if (offer == -1) { 3099 /* mss changed due to Path MTU discovery */ 3100 if (mss < tp->t_maxseg) { 3101 /* 3102 * Follow suggestion in RFC 2414 to reduce the 3103 * congestion window by the ratio of the old 3104 * segment size to the new segment size. 3105 */ 3106 tp->snd_cwnd = ulmax((tp->snd_cwnd / tp->t_maxseg) * 3107 mss, mss); 3108 } 3109 } else 3110 tp->snd_cwnd = mss; 3111 3112 tp->t_maxseg = mss; 3113 3114 return (offer != -1 ? mssopt : mss); 3115 } 3116 3117 /* 3118 * Set connection variables based on the effective MSS. 3119 * We are passed the TCPCB for the actual connection. If we 3120 * are the server, we are called by the compressed state engine 3121 * when the 3-way handshake is complete. If we are the client, 3122 * we are called when we receive the SYN,ACK from the server. 3123 * 3124 * NOTE: The t_maxseg value must be initialized in the TCPCB 3125 * before this routine is called! 3126 */ 3127 void 3128 tcp_mss_update(tp) 3129 struct tcpcb *tp; 3130 { 3131 int mss, rtt; 3132 u_long bufsize; 3133 struct rtentry *rt; 3134 struct socket *so; 3135 3136 so = tp->t_inpcb->inp_socket; 3137 mss = tp->t_maxseg; 3138 3139 rt = in_pcbrtentry(tp->t_inpcb); 3140 3141 if (rt == NULL) 3142 return; 3143 3144 #ifdef RTV_MTU /* if route characteristics exist ... */ 3145 /* 3146 * While we're here, check if there's an initial rtt 3147 * or rttvar. Convert from the route-table units 3148 * to scaled multiples of the slow timeout timer. 3149 */ 3150 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 3151 /* 3152 * XXX the lock bit for MTU indicates that the value 3153 * is also a minimum value; this is subject to time. 3154 */ 3155 if (rt->rt_rmx.rmx_locks & RTV_RTT) 3156 TCPT_RANGESET(tp->t_rttmin, 3157 rtt / (RTM_RTTUNIT / PR_SLOWHZ), 3158 TCPTV_MIN, TCPTV_REXMTMAX); 3159 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 3160 if (rt->rt_rmx.rmx_rttvar) 3161 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 3162 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 3163 else 3164 /* default variation is +- 1 rtt */ 3165 tp->t_rttvar = 3166 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 3167 TCPT_RANGESET((long) tp->t_rxtcur, 3168 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 3169 tp->t_rttmin, TCPTV_REXMTMAX); 3170 } 3171 #endif 3172 3173 /* 3174 * If there's a pipesize, change the socket buffer 3175 * to that size. Make the socket buffers an integral 3176 * number of mss units; if the mss is larger than 3177 * the socket buffer, decrease the mss. 3178 */ 3179 #ifdef RTV_SPIPE 3180 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 3181 #endif 3182 bufsize = so->so_snd.sb_hiwat; 3183 if (bufsize < mss) { 3184 mss = bufsize; 3185 /* Update t_maxseg and t_maxopd */ 3186 tcp_mss(tp, mss); 3187 } else { 3188 bufsize = roundup(bufsize, mss); 3189 if (bufsize > sb_max) 3190 bufsize = sb_max; 3191 (void)sbreserve(&so->so_snd, bufsize); 3192 } 3193 3194 #ifdef RTV_RPIPE 3195 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 3196 #endif 3197 bufsize = so->so_rcv.sb_hiwat; 3198 if (bufsize > mss) { 3199 bufsize = roundup(bufsize, mss); 3200 if (bufsize > sb_max) 3201 bufsize = sb_max; 3202 (void)sbreserve(&so->so_rcv, bufsize); 3203 #ifdef RTV_RPIPE 3204 if (rt->rt_rmx.rmx_recvpipe > 0) 3205 tcp_rscale(tp, so->so_rcv.sb_hiwat); 3206 #endif 3207 } 3208 3209 #ifdef RTV_SSTHRESH 3210 if (rt->rt_rmx.rmx_ssthresh) { 3211 /* 3212 * There's some sort of gateway or interface 3213 * buffer limit on the path. Use this to set 3214 * the slow start threshhold, but set the 3215 * threshold to no less than 2*mss. 3216 */ 3217 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 3218 } 3219 #endif /* RTV_MTU */ 3220 } 3221 #endif /* TUBA_INCLUDE */ 3222 3223 #if defined (TCP_SACK) 3224 /* 3225 * Checks for partial ack. If partial ack arrives, force the retransmission 3226 * of the next unacknowledged segment, do not clear tp->t_dupacks, and return 3227 * 1. By setting snd_nxt to ti_ack, this forces retransmission timer to 3228 * be started again. If the ack advances at least to tp->snd_last, return 0. 3229 */ 3230 int 3231 tcp_newreno(tp, th) 3232 struct tcpcb *tp; 3233 struct tcphdr *th; 3234 { 3235 if (SEQ_LT(th->th_ack, tp->snd_last)) { 3236 /* 3237 * snd_una has not been updated and the socket send buffer 3238 * not yet drained of the acked data, so we have to leave 3239 * snd_una as it was to get the correct data offset in 3240 * tcp_output(). 3241 */ 3242 tcp_seq onxt = tp->snd_nxt; 3243 u_long ocwnd = tp->snd_cwnd; 3244 TCP_TIMER_DISARM(tp, TCPT_REXMT); 3245 tp->t_rtttime = 0; 3246 tp->snd_nxt = th->th_ack; 3247 /* 3248 * Set snd_cwnd to one segment beyond acknowledged offset 3249 * (tp->snd_una not yet updated when this function is called) 3250 */ 3251 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3252 (void) tcp_output(tp); 3253 tp->snd_cwnd = ocwnd; 3254 if (SEQ_GT(onxt, tp->snd_nxt)) 3255 tp->snd_nxt = onxt; 3256 /* 3257 * Partial window deflation. Relies on fact that tp->snd_una 3258 * not updated yet. 3259 */ 3260 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 3261 return 1; 3262 } 3263 return 0; 3264 } 3265 #endif /* TCP_SACK */ 3266