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