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