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