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