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