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