1 /* $NetBSD: tcp_input.c,v 1.439 2024/06/29 12:59:08 riastradh Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 34 * 35 * NRL grants permission for redistribution and use in source and binary 36 * forms, with or without modification, of the software and documentation 37 * created at NRL provided that the following conditions are met: 38 * 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgements: 46 * This product includes software developed by the University of 47 * California, Berkeley and its contributors. 48 * This product includes software developed at the Information 49 * Technology Division, US Naval Research Laboratory. 50 * 4. Neither the name of the NRL 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 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 55 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 57 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 58 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 59 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 60 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 61 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 62 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 63 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 65 * 66 * The views and conclusions contained in the software and documentation 67 * are those of the authors and should not be interpreted as representing 68 * official policies, either expressed or implied, of the US Naval 69 * Research Laboratory (NRL). 70 */ 71 72 /*- 73 * Copyright (c) 1997, 1998, 1999, 2001, 2005, 2006, 74 * 2011 The NetBSD Foundation, Inc. 75 * All rights reserved. 76 * 77 * This code is derived from software contributed to The NetBSD Foundation 78 * by Coyote Point Systems, Inc. 79 * This code is derived from software contributed to The NetBSD Foundation 80 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation 81 * Facility, NASA Ames Research Center. 82 * This code is derived from software contributed to The NetBSD Foundation 83 * by Charles M. Hannum. 84 * This code is derived from software contributed to The NetBSD Foundation 85 * by Rui Paulo. 86 * 87 * Redistribution and use in source and binary forms, with or without 88 * modification, are permitted provided that the following conditions 89 * are met: 90 * 1. Redistributions of source code must retain the above copyright 91 * notice, this list of conditions and the following disclaimer. 92 * 2. Redistributions in binary form must reproduce the above copyright 93 * notice, this list of conditions and the following disclaimer in the 94 * documentation and/or other materials provided with the distribution. 95 * 96 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 97 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 98 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 99 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 100 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 101 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 102 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 103 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 104 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 105 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 106 * POSSIBILITY OF SUCH DAMAGE. 107 */ 108 109 /* 110 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 111 * The Regents of the University of California. All rights reserved. 112 * 113 * Redistribution and use in source and binary forms, with or without 114 * modification, are permitted provided that the following conditions 115 * are met: 116 * 1. Redistributions of source code must retain the above copyright 117 * notice, this list of conditions and the following disclaimer. 118 * 2. Redistributions in binary form must reproduce the above copyright 119 * notice, this list of conditions and the following disclaimer in the 120 * documentation and/or other materials provided with the distribution. 121 * 3. Neither the name of the University nor the names of its contributors 122 * may be used to endorse or promote products derived from this software 123 * without specific prior written permission. 124 * 125 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 126 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 127 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 128 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 129 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 130 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 131 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 132 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 133 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 134 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 135 * SUCH DAMAGE. 136 * 137 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 138 */ 139 140 #include <sys/cdefs.h> 141 __KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.439 2024/06/29 12:59:08 riastradh Exp $"); 142 143 #ifdef _KERNEL_OPT 144 #include "opt_inet.h" 145 #include "opt_ipsec.h" 146 #include "opt_inet_csum.h" 147 #include "opt_tcp_debug.h" 148 #endif 149 150 #include <sys/param.h> 151 #include <sys/systm.h> 152 #include <sys/malloc.h> 153 #include <sys/mbuf.h> 154 #include <sys/protosw.h> 155 #include <sys/socket.h> 156 #include <sys/socketvar.h> 157 #include <sys/errno.h> 158 #include <sys/syslog.h> 159 #include <sys/pool.h> 160 #include <sys/domain.h> 161 #include <sys/kernel.h> 162 #ifdef TCP_SIGNATURE 163 #include <sys/md5.h> 164 #endif 165 #include <sys/lwp.h> /* for lwp0 */ 166 #include <sys/cprng.h> 167 168 #include <net/if.h> 169 #include <net/if_types.h> 170 171 #include <netinet/in.h> 172 #include <netinet/in_systm.h> 173 #include <netinet/ip.h> 174 #include <netinet/in_pcb.h> 175 #include <netinet/in_var.h> 176 #include <netinet/ip_var.h> 177 #include <netinet/in_offload.h> 178 179 #if NARP > 0 180 #include <netinet/if_inarp.h> 181 #endif 182 #ifdef INET6 183 #include <netinet/ip6.h> 184 #include <netinet6/ip6_var.h> 185 #include <netinet6/in6_pcb.h> 186 #include <netinet6/ip6_var.h> 187 #include <netinet6/in6_var.h> 188 #include <netinet/icmp6.h> 189 #include <netinet6/nd6.h> 190 #ifdef TCP_SIGNATURE 191 #include <netinet6/scope6_var.h> 192 #endif 193 #endif 194 195 #ifndef INET6 196 #include <netinet/ip6.h> 197 #endif 198 199 #include <netinet/tcp.h> 200 #include <netinet/tcp_fsm.h> 201 #include <netinet/tcp_seq.h> 202 #include <netinet/tcp_timer.h> 203 #include <netinet/tcp_var.h> 204 #include <netinet/tcp_private.h> 205 #include <netinet/tcp_congctl.h> 206 #include <netinet/tcp_debug.h> 207 #include <netinet/tcp_syncache.h> 208 209 #ifdef INET6 210 #include "faith.h" 211 #if defined(NFAITH) && NFAITH > 0 212 #include <net/if_faith.h> 213 #endif 214 #endif 215 216 #ifdef IPSEC 217 #include <netipsec/ipsec.h> 218 #include <netipsec/key.h> 219 #ifdef INET6 220 #include <netipsec/ipsec6.h> 221 #endif 222 #endif /* IPSEC*/ 223 224 #include <netinet/tcp_vtw.h> 225 226 int tcprexmtthresh = 3; 227 int tcp_log_refused; 228 229 int tcp_do_autorcvbuf = 1; 230 int tcp_autorcvbuf_inc = 16 * 1024; 231 int tcp_autorcvbuf_max = 256 * 1024; 232 int tcp_msl = (TCPTV_MSL / PR_SLOWHZ); 233 234 static int tcp_rst_ppslim_count = 0; 235 static struct timeval tcp_rst_ppslim_last; 236 static int tcp_ackdrop_ppslim_count = 0; 237 static struct timeval tcp_ackdrop_ppslim_last; 238 239 #define TCP_PAWS_IDLE (24U * 24 * 60 * 60 * PR_SLOWHZ) 240 241 /* for modulo comparisons of timestamps */ 242 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0) 243 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0) 244 245 /* 246 * Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. 247 */ 248 static void 249 nd_hint(struct tcpcb *tp) 250 { 251 struct route *ro = NULL; 252 struct rtentry *rt; 253 254 if (tp == NULL) 255 return; 256 257 ro = &tp->t_inpcb->inp_route; 258 if (ro == NULL) 259 return; 260 261 rt = rtcache_validate(ro); 262 if (rt == NULL) 263 return; 264 265 switch (tp->t_family) { 266 #if NARP > 0 267 case AF_INET: 268 arp_nud_hint(rt); 269 break; 270 #endif 271 #ifdef INET6 272 case AF_INET6: 273 nd6_nud_hint(rt); 274 break; 275 #endif 276 } 277 278 rtcache_unref(rt, ro); 279 } 280 281 /* 282 * Compute ACK transmission behavior. Delay the ACK unless 283 * we have already delayed an ACK (must send an ACK every two segments). 284 * We also ACK immediately if we received a PUSH and the ACK-on-PUSH 285 * option is enabled. 286 */ 287 static void 288 tcp_setup_ack(struct tcpcb *tp, const struct tcphdr *th) 289 { 290 291 if (tp->t_flags & TF_DELACK || 292 (tcp_ack_on_push && th->th_flags & TH_PUSH)) 293 tp->t_flags |= TF_ACKNOW; 294 else 295 TCP_SET_DELACK(tp); 296 } 297 298 static void 299 icmp_check(struct tcpcb *tp, const struct tcphdr *th, int acked) 300 { 301 302 /* 303 * If we had a pending ICMP message that refers to data that have 304 * just been acknowledged, disregard the recorded ICMP message. 305 */ 306 if ((tp->t_flags & TF_PMTUD_PEND) && 307 SEQ_GT(th->th_ack, tp->t_pmtud_th_seq)) 308 tp->t_flags &= ~TF_PMTUD_PEND; 309 310 /* 311 * Keep track of the largest chunk of data 312 * acknowledged since last PMTU update 313 */ 314 if (tp->t_pmtud_mss_acked < acked) 315 tp->t_pmtud_mss_acked = acked; 316 } 317 318 /* 319 * Convert TCP protocol fields to host order for easier processing. 320 */ 321 static void 322 tcp_fields_to_host(struct tcphdr *th) 323 { 324 325 NTOHL(th->th_seq); 326 NTOHL(th->th_ack); 327 NTOHS(th->th_win); 328 NTOHS(th->th_urp); 329 } 330 331 /* 332 * ... and reverse the above. 333 */ 334 static void 335 tcp_fields_to_net(struct tcphdr *th) 336 { 337 338 HTONL(th->th_seq); 339 HTONL(th->th_ack); 340 HTONS(th->th_win); 341 HTONS(th->th_urp); 342 } 343 344 static void 345 tcp_urp_drop(struct tcphdr *th, int todrop, int *tiflags) 346 { 347 if (th->th_urp > todrop) { 348 th->th_urp -= todrop; 349 } else { 350 *tiflags &= ~TH_URG; 351 th->th_urp = 0; 352 } 353 } 354 355 #ifdef TCP_CSUM_COUNTERS 356 #include <sys/device.h> 357 358 extern struct evcnt tcp_hwcsum_ok; 359 extern struct evcnt tcp_hwcsum_bad; 360 extern struct evcnt tcp_hwcsum_data; 361 extern struct evcnt tcp_swcsum; 362 #if defined(INET6) 363 extern struct evcnt tcp6_hwcsum_ok; 364 extern struct evcnt tcp6_hwcsum_bad; 365 extern struct evcnt tcp6_hwcsum_data; 366 extern struct evcnt tcp6_swcsum; 367 #endif /* defined(INET6) */ 368 369 #define TCP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++ 370 371 #else 372 373 #define TCP_CSUM_COUNTER_INCR(ev) /* nothing */ 374 375 #endif /* TCP_CSUM_COUNTERS */ 376 377 #ifdef TCP_REASS_COUNTERS 378 #include <sys/device.h> 379 380 extern struct evcnt tcp_reass_; 381 extern struct evcnt tcp_reass_empty; 382 extern struct evcnt tcp_reass_iteration[8]; 383 extern struct evcnt tcp_reass_prependfirst; 384 extern struct evcnt tcp_reass_prepend; 385 extern struct evcnt tcp_reass_insert; 386 extern struct evcnt tcp_reass_inserttail; 387 extern struct evcnt tcp_reass_append; 388 extern struct evcnt tcp_reass_appendtail; 389 extern struct evcnt tcp_reass_overlaptail; 390 extern struct evcnt tcp_reass_overlapfront; 391 extern struct evcnt tcp_reass_segdup; 392 extern struct evcnt tcp_reass_fragdup; 393 394 #define TCP_REASS_COUNTER_INCR(ev) (ev)->ev_count++ 395 396 #else 397 398 #define TCP_REASS_COUNTER_INCR(ev) /* nothing */ 399 400 #endif /* TCP_REASS_COUNTERS */ 401 402 static int tcp_reass(struct tcpcb *, const struct tcphdr *, struct mbuf *, 403 int); 404 405 static void tcp4_log_refused(const struct ip *, const struct tcphdr *); 406 #ifdef INET6 407 static void tcp6_log_refused(const struct ip6_hdr *, const struct tcphdr *); 408 #endif 409 410 #if defined(MBUFTRACE) 411 struct mowner tcp_reass_mowner = MOWNER_INIT("tcp", "reass"); 412 #endif /* defined(MBUFTRACE) */ 413 414 static struct pool tcpipqent_pool; 415 416 void 417 tcpipqent_init(void) 418 { 419 420 pool_init(&tcpipqent_pool, sizeof(struct ipqent), 0, 0, 0, "tcpipqepl", 421 NULL, IPL_VM); 422 } 423 424 struct ipqent * 425 tcpipqent_alloc(void) 426 { 427 struct ipqent *ipqe; 428 int s; 429 430 s = splvm(); 431 ipqe = pool_get(&tcpipqent_pool, PR_NOWAIT); 432 splx(s); 433 434 return ipqe; 435 } 436 437 void 438 tcpipqent_free(struct ipqent *ipqe) 439 { 440 int s; 441 442 s = splvm(); 443 pool_put(&tcpipqent_pool, ipqe); 444 splx(s); 445 } 446 447 /* 448 * Insert segment ti into reassembly queue of tcp with 449 * control block tp. Return TH_FIN if reassembly now includes 450 * a segment with FIN. 451 */ 452 static int 453 tcp_reass(struct tcpcb *tp, const struct tcphdr *th, struct mbuf *m, int tlen) 454 { 455 struct ipqent *p, *q, *nq, *tiqe = NULL; 456 struct socket *so = NULL; 457 int pkt_flags; 458 tcp_seq pkt_seq; 459 unsigned pkt_len; 460 u_long rcvpartdupbyte = 0; 461 u_long rcvoobyte; 462 #ifdef TCP_REASS_COUNTERS 463 u_int count = 0; 464 #endif 465 net_stat_ref_t tcps; 466 467 so = tp->t_inpcb->inp_socket; 468 469 TCP_REASS_LOCK_CHECK(tp); 470 471 /* 472 * Call with th==NULL after become established to 473 * force pre-ESTABLISHED data up to user socket. 474 */ 475 if (th == NULL) 476 goto present; 477 478 m_claimm(m, &tcp_reass_mowner); 479 480 rcvoobyte = tlen; 481 /* 482 * Copy these to local variables because the TCP header gets munged 483 * while we are collapsing mbufs. 484 */ 485 pkt_seq = th->th_seq; 486 pkt_len = tlen; 487 pkt_flags = th->th_flags; 488 489 TCP_REASS_COUNTER_INCR(&tcp_reass_); 490 491 if ((p = TAILQ_LAST(&tp->segq, ipqehead)) != NULL) { 492 /* 493 * When we miss a packet, the vast majority of time we get 494 * packets that follow it in order. So optimize for that. 495 */ 496 if (pkt_seq == p->ipqe_seq + p->ipqe_len) { 497 p->ipqe_len += pkt_len; 498 p->ipqe_flags |= pkt_flags; 499 m_cat(p->ipqe_m, m); 500 m = NULL; 501 tiqe = p; 502 TAILQ_REMOVE(&tp->timeq, p, ipqe_timeq); 503 TCP_REASS_COUNTER_INCR(&tcp_reass_appendtail); 504 goto skip_replacement; 505 } 506 /* 507 * While we're here, if the pkt is completely beyond 508 * anything we have, just insert it at the tail. 509 */ 510 if (SEQ_GT(pkt_seq, p->ipqe_seq + p->ipqe_len)) { 511 TCP_REASS_COUNTER_INCR(&tcp_reass_inserttail); 512 goto insert_it; 513 } 514 } 515 516 q = TAILQ_FIRST(&tp->segq); 517 518 if (q != NULL) { 519 /* 520 * If this segment immediately precedes the first out-of-order 521 * block, simply slap the segment in front of it and (mostly) 522 * skip the complicated logic. 523 */ 524 if (pkt_seq + pkt_len == q->ipqe_seq) { 525 q->ipqe_seq = pkt_seq; 526 q->ipqe_len += pkt_len; 527 q->ipqe_flags |= pkt_flags; 528 m_cat(m, q->ipqe_m); 529 q->ipqe_m = m; 530 tiqe = q; 531 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 532 TCP_REASS_COUNTER_INCR(&tcp_reass_prependfirst); 533 goto skip_replacement; 534 } 535 } else { 536 TCP_REASS_COUNTER_INCR(&tcp_reass_empty); 537 } 538 539 /* 540 * Find a segment which begins after this one does. 541 */ 542 for (p = NULL; q != NULL; q = nq) { 543 nq = TAILQ_NEXT(q, ipqe_q); 544 #ifdef TCP_REASS_COUNTERS 545 count++; 546 #endif 547 548 /* 549 * If the received segment is just right after this 550 * fragment, merge the two together and then check 551 * for further overlaps. 552 */ 553 if (q->ipqe_seq + q->ipqe_len == pkt_seq) { 554 pkt_len += q->ipqe_len; 555 pkt_flags |= q->ipqe_flags; 556 pkt_seq = q->ipqe_seq; 557 m_cat(q->ipqe_m, m); 558 m = q->ipqe_m; 559 TCP_REASS_COUNTER_INCR(&tcp_reass_append); 560 goto free_ipqe; 561 } 562 563 /* 564 * If the received segment is completely past this 565 * fragment, we need to go to the next fragment. 566 */ 567 if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) { 568 p = q; 569 continue; 570 } 571 572 /* 573 * If the fragment is past the received segment, 574 * it (or any following) can't be concatenated. 575 */ 576 if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len)) { 577 TCP_REASS_COUNTER_INCR(&tcp_reass_insert); 578 break; 579 } 580 581 /* 582 * We've received all the data in this segment before. 583 * Mark it as a duplicate and return. 584 */ 585 if (SEQ_LEQ(q->ipqe_seq, pkt_seq) && 586 SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) { 587 tcps = TCP_STAT_GETREF(); 588 _NET_STATINC_REF(tcps, TCP_STAT_RCVDUPPACK); 589 _NET_STATADD_REF(tcps, TCP_STAT_RCVDUPBYTE, pkt_len); 590 TCP_STAT_PUTREF(); 591 tcp_new_dsack(tp, pkt_seq, pkt_len); 592 m_freem(m); 593 if (tiqe != NULL) { 594 tcpipqent_free(tiqe); 595 } 596 TCP_REASS_COUNTER_INCR(&tcp_reass_segdup); 597 goto out; 598 } 599 600 /* 601 * Received segment completely overlaps this fragment 602 * so we drop the fragment (this keeps the temporal 603 * ordering of segments correct). 604 */ 605 if (SEQ_GEQ(q->ipqe_seq, pkt_seq) && 606 SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) { 607 rcvpartdupbyte += q->ipqe_len; 608 m_freem(q->ipqe_m); 609 TCP_REASS_COUNTER_INCR(&tcp_reass_fragdup); 610 goto free_ipqe; 611 } 612 613 /* 614 * Received segment extends past the end of the fragment. 615 * Drop the overlapping bytes, merge the fragment and 616 * segment, and treat as a longer received packet. 617 */ 618 if (SEQ_LT(q->ipqe_seq, pkt_seq) && 619 SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq)) { 620 int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq; 621 m_adj(m, overlap); 622 rcvpartdupbyte += overlap; 623 m_cat(q->ipqe_m, m); 624 m = q->ipqe_m; 625 pkt_seq = q->ipqe_seq; 626 pkt_len += q->ipqe_len - overlap; 627 rcvoobyte -= overlap; 628 TCP_REASS_COUNTER_INCR(&tcp_reass_overlaptail); 629 goto free_ipqe; 630 } 631 632 /* 633 * Received segment extends past the front of the fragment. 634 * Drop the overlapping bytes on the received packet. The 635 * packet will then be concatenated with this fragment a 636 * bit later. 637 */ 638 if (SEQ_GT(q->ipqe_seq, pkt_seq) && 639 SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len)) { 640 int overlap = pkt_seq + pkt_len - q->ipqe_seq; 641 m_adj(m, -overlap); 642 pkt_len -= overlap; 643 rcvpartdupbyte += overlap; 644 TCP_REASS_COUNTER_INCR(&tcp_reass_overlapfront); 645 rcvoobyte -= overlap; 646 } 647 648 /* 649 * If the received segment immediately precedes this 650 * fragment then tack the fragment onto this segment 651 * and reinsert the data. 652 */ 653 if (q->ipqe_seq == pkt_seq + pkt_len) { 654 pkt_len += q->ipqe_len; 655 pkt_flags |= q->ipqe_flags; 656 m_cat(m, q->ipqe_m); 657 TAILQ_REMOVE(&tp->segq, q, ipqe_q); 658 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 659 tp->t_segqlen--; 660 KASSERT(tp->t_segqlen >= 0); 661 KASSERT(tp->t_segqlen != 0 || 662 (TAILQ_EMPTY(&tp->segq) && 663 TAILQ_EMPTY(&tp->timeq))); 664 if (tiqe == NULL) { 665 tiqe = q; 666 } else { 667 tcpipqent_free(q); 668 } 669 TCP_REASS_COUNTER_INCR(&tcp_reass_prepend); 670 break; 671 } 672 673 /* 674 * If the fragment is before the segment, remember it. 675 * When this loop is terminated, p will contain the 676 * pointer to the fragment that is right before the 677 * received segment. 678 */ 679 if (SEQ_LEQ(q->ipqe_seq, pkt_seq)) 680 p = q; 681 682 continue; 683 684 /* 685 * This is a common operation. It also will allow 686 * to save doing a malloc/free in most instances. 687 */ 688 free_ipqe: 689 TAILQ_REMOVE(&tp->segq, q, ipqe_q); 690 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 691 tp->t_segqlen--; 692 KASSERT(tp->t_segqlen >= 0); 693 KASSERT(tp->t_segqlen != 0 || 694 (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq))); 695 if (tiqe == NULL) { 696 tiqe = q; 697 } else { 698 tcpipqent_free(q); 699 } 700 } 701 702 #ifdef TCP_REASS_COUNTERS 703 if (count > 7) 704 TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[0]); 705 else if (count > 0) 706 TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[count]); 707 #endif 708 709 insert_it: 710 /* 711 * Allocate a new queue entry (block) since the received segment 712 * did not collapse onto any other out-of-order block. If it had 713 * collapsed, tiqe would not be NULL and we would be reusing it. 714 * 715 * If the allocation fails, drop the packet. 716 */ 717 if (tiqe == NULL) { 718 tiqe = tcpipqent_alloc(); 719 if (tiqe == NULL) { 720 TCP_STATINC(TCP_STAT_RCVMEMDROP); 721 m_freem(m); 722 goto out; 723 } 724 } 725 726 /* 727 * Update the counters. 728 */ 729 tp->t_rcvoopack++; 730 tcps = TCP_STAT_GETREF(); 731 _NET_STATINC_REF(tcps, TCP_STAT_RCVOOPACK); 732 _NET_STATADD_REF(tcps, TCP_STAT_RCVOOBYTE, rcvoobyte); 733 if (rcvpartdupbyte) { 734 _NET_STATINC_REF(tcps, TCP_STAT_RCVPARTDUPPACK); 735 _NET_STATADD_REF(tcps, TCP_STAT_RCVPARTDUPBYTE, 736 rcvpartdupbyte); 737 } 738 TCP_STAT_PUTREF(); 739 740 /* 741 * Insert the new fragment queue entry into both queues. 742 */ 743 tiqe->ipqe_m = m; 744 tiqe->ipqe_seq = pkt_seq; 745 tiqe->ipqe_len = pkt_len; 746 tiqe->ipqe_flags = pkt_flags; 747 if (p == NULL) { 748 TAILQ_INSERT_HEAD(&tp->segq, tiqe, ipqe_q); 749 } else { 750 TAILQ_INSERT_AFTER(&tp->segq, p, tiqe, ipqe_q); 751 } 752 tp->t_segqlen++; 753 754 skip_replacement: 755 TAILQ_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq); 756 757 present: 758 /* 759 * Present data to user, advancing rcv_nxt through 760 * completed sequence space. 761 */ 762 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 763 goto out; 764 q = TAILQ_FIRST(&tp->segq); 765 if (q == NULL || q->ipqe_seq != tp->rcv_nxt) 766 goto out; 767 if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len) 768 goto out; 769 770 tp->rcv_nxt += q->ipqe_len; 771 pkt_flags = q->ipqe_flags & TH_FIN; 772 nd_hint(tp); 773 774 TAILQ_REMOVE(&tp->segq, q, ipqe_q); 775 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 776 tp->t_segqlen--; 777 KASSERT(tp->t_segqlen >= 0); 778 KASSERT(tp->t_segqlen != 0 || 779 (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq))); 780 if (so->so_state & SS_CANTRCVMORE) 781 m_freem(q->ipqe_m); 782 else 783 sbappendstream(&so->so_rcv, q->ipqe_m); 784 tcpipqent_free(q); 785 TCP_REASS_UNLOCK(tp); 786 sorwakeup(so); 787 return pkt_flags; 788 789 out: 790 TCP_REASS_UNLOCK(tp); 791 return 0; 792 } 793 794 #ifdef INET6 795 int 796 tcp6_input(struct mbuf **mp, int *offp, int proto) 797 { 798 struct mbuf *m = *mp; 799 800 /* 801 * draft-itojun-ipv6-tcp-to-anycast 802 * better place to put this in? 803 */ 804 if (m->m_flags & M_ANYCAST6) { 805 struct ip6_hdr *ip6; 806 if (m->m_len < sizeof(struct ip6_hdr)) { 807 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 808 TCP_STATINC(TCP_STAT_RCVSHORT); 809 return IPPROTO_DONE; 810 } 811 } 812 ip6 = mtod(m, struct ip6_hdr *); 813 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 814 (char *)&ip6->ip6_dst - (char *)ip6); 815 return IPPROTO_DONE; 816 } 817 818 tcp_input(m, *offp, proto); 819 return IPPROTO_DONE; 820 } 821 #endif 822 823 static void 824 tcp4_log_refused(const struct ip *ip, const struct tcphdr *th) 825 { 826 char src[INET_ADDRSTRLEN]; 827 char dst[INET_ADDRSTRLEN]; 828 829 if (ip) { 830 in_print(src, sizeof(src), &ip->ip_src); 831 in_print(dst, sizeof(dst), &ip->ip_dst); 832 } else { 833 strlcpy(src, "(unknown)", sizeof(src)); 834 strlcpy(dst, "(unknown)", sizeof(dst)); 835 } 836 log(LOG_INFO, 837 "Connection attempt to TCP %s:%d from %s:%d\n", 838 dst, ntohs(th->th_dport), 839 src, ntohs(th->th_sport)); 840 } 841 842 #ifdef INET6 843 static void 844 tcp6_log_refused(const struct ip6_hdr *ip6, const struct tcphdr *th) 845 { 846 char src[INET6_ADDRSTRLEN]; 847 char dst[INET6_ADDRSTRLEN]; 848 849 if (ip6) { 850 in6_print(src, sizeof(src), &ip6->ip6_src); 851 in6_print(dst, sizeof(dst), &ip6->ip6_dst); 852 } else { 853 strlcpy(src, "(unknown v6)", sizeof(src)); 854 strlcpy(dst, "(unknown v6)", sizeof(dst)); 855 } 856 log(LOG_INFO, 857 "Connection attempt to TCP [%s]:%d from [%s]:%d\n", 858 dst, ntohs(th->th_dport), 859 src, ntohs(th->th_sport)); 860 } 861 #endif 862 863 /* 864 * Checksum extended TCP header and data. 865 */ 866 int 867 tcp_input_checksum(int af, struct mbuf *m, const struct tcphdr *th, 868 int toff, int off, int tlen) 869 { 870 struct ifnet *rcvif; 871 int s; 872 873 /* 874 * XXX it's better to record and check if this mbuf is 875 * already checked. 876 */ 877 878 rcvif = m_get_rcvif(m, &s); 879 if (__predict_false(rcvif == NULL)) 880 goto badcsum; /* XXX */ 881 882 switch (af) { 883 case AF_INET: 884 switch (m->m_pkthdr.csum_flags & 885 ((rcvif->if_csum_flags_rx & M_CSUM_TCPv4) | 886 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) { 887 case M_CSUM_TCPv4|M_CSUM_TCP_UDP_BAD: 888 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_bad); 889 goto badcsum; 890 891 case M_CSUM_TCPv4|M_CSUM_DATA: { 892 u_int32_t hw_csum = m->m_pkthdr.csum_data; 893 894 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_data); 895 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) { 896 const struct ip *ip = 897 mtod(m, const struct ip *); 898 899 hw_csum = in_cksum_phdr(ip->ip_src.s_addr, 900 ip->ip_dst.s_addr, 901 htons(hw_csum + tlen + off + IPPROTO_TCP)); 902 } 903 if ((hw_csum ^ 0xffff) != 0) 904 goto badcsum; 905 break; 906 } 907 908 case M_CSUM_TCPv4: 909 /* Checksum was okay. */ 910 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_ok); 911 break; 912 913 default: 914 /* 915 * Must compute it ourselves. Maybe skip checksum 916 * on loopback interfaces. 917 */ 918 if (__predict_true(!(rcvif->if_flags & IFF_LOOPBACK) || 919 tcp_do_loopback_cksum)) { 920 TCP_CSUM_COUNTER_INCR(&tcp_swcsum); 921 if (in4_cksum(m, IPPROTO_TCP, toff, 922 tlen + off) != 0) 923 goto badcsum; 924 } 925 break; 926 } 927 break; 928 929 #ifdef INET6 930 case AF_INET6: 931 switch (m->m_pkthdr.csum_flags & 932 ((rcvif->if_csum_flags_rx & M_CSUM_TCPv6) | 933 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) { 934 case M_CSUM_TCPv6|M_CSUM_TCP_UDP_BAD: 935 TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_bad); 936 goto badcsum; 937 938 #if 0 /* notyet */ 939 case M_CSUM_TCPv6|M_CSUM_DATA: 940 #endif 941 942 case M_CSUM_TCPv6: 943 /* Checksum was okay. */ 944 TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_ok); 945 break; 946 947 default: 948 /* 949 * Must compute it ourselves. Maybe skip checksum 950 * on loopback interfaces. 951 */ 952 if (__predict_true((m->m_flags & M_LOOP) == 0 || 953 tcp_do_loopback_cksum)) { 954 TCP_CSUM_COUNTER_INCR(&tcp6_swcsum); 955 if (in6_cksum(m, IPPROTO_TCP, toff, 956 tlen + off) != 0) 957 goto badcsum; 958 } 959 } 960 break; 961 #endif /* INET6 */ 962 } 963 m_put_rcvif(rcvif, &s); 964 965 return 0; 966 967 badcsum: 968 m_put_rcvif(rcvif, &s); 969 TCP_STATINC(TCP_STAT_RCVBADSUM); 970 return -1; 971 } 972 973 /* 974 * When a packet arrives addressed to a vestigial tcpbp, we 975 * nevertheless have to respond to it per the spec. 976 * 977 * This code is duplicated from the one in tcp_input(). 978 */ 979 static void tcp_vtw_input(struct tcphdr *th, vestigial_inpcb_t *vp, 980 struct mbuf *m, int tlen) 981 { 982 int tiflags; 983 int todrop; 984 uint32_t t_flags = 0; 985 net_stat_ref_t tcps; 986 987 tiflags = th->th_flags; 988 todrop = vp->rcv_nxt - th->th_seq; 989 990 if (todrop > 0) { 991 if (tiflags & TH_SYN) { 992 tiflags &= ~TH_SYN; 993 th->th_seq++; 994 tcp_urp_drop(th, 1, &tiflags); 995 todrop--; 996 } 997 if (todrop > tlen || 998 (todrop == tlen && (tiflags & TH_FIN) == 0)) { 999 /* 1000 * Any valid FIN or RST must be to the left of the 1001 * window. At this point the FIN or RST must be a 1002 * duplicate or out of sequence; drop it. 1003 */ 1004 if (tiflags & TH_RST) 1005 goto drop; 1006 tiflags &= ~(TH_FIN|TH_RST); 1007 1008 /* 1009 * Send an ACK to resynchronize and drop any data. 1010 * But keep on processing for RST or ACK. 1011 */ 1012 t_flags |= TF_ACKNOW; 1013 todrop = tlen; 1014 tcps = TCP_STAT_GETREF(); 1015 _NET_STATINC_REF(tcps, TCP_STAT_RCVDUPPACK); 1016 _NET_STATADD_REF(tcps, TCP_STAT_RCVDUPBYTE, todrop); 1017 TCP_STAT_PUTREF(); 1018 } else if ((tiflags & TH_RST) && 1019 th->th_seq != vp->rcv_nxt) { 1020 /* 1021 * Test for reset before adjusting the sequence 1022 * number for overlapping data. 1023 */ 1024 goto dropafterack_ratelim; 1025 } else { 1026 tcps = TCP_STAT_GETREF(); 1027 _NET_STATINC_REF(tcps, TCP_STAT_RCVPARTDUPPACK); 1028 _NET_STATADD_REF(tcps, TCP_STAT_RCVPARTDUPBYTE, 1029 todrop); 1030 TCP_STAT_PUTREF(); 1031 } 1032 1033 // tcp_new_dsack(tp, th->th_seq, todrop); 1034 // hdroptlen += todrop; /*drop from head afterwards*/ 1035 1036 th->th_seq += todrop; 1037 tlen -= todrop; 1038 tcp_urp_drop(th, todrop, &tiflags); 1039 } 1040 1041 /* 1042 * If new data are received on a connection after the 1043 * user processes are gone, then RST the other end. 1044 */ 1045 if (tlen) { 1046 TCP_STATINC(TCP_STAT_RCVAFTERCLOSE); 1047 goto dropwithreset; 1048 } 1049 1050 /* 1051 * If segment ends after window, drop trailing data 1052 * (and PUSH and FIN); if nothing left, just ACK. 1053 */ 1054 todrop = (th->th_seq + tlen) - (vp->rcv_nxt + vp->rcv_wnd); 1055 1056 if (todrop > 0) { 1057 TCP_STATINC(TCP_STAT_RCVPACKAFTERWIN); 1058 if (todrop >= tlen) { 1059 /* 1060 * The segment actually starts after the window. 1061 * th->th_seq + tlen - vp->rcv_nxt - vp->rcv_wnd >= tlen 1062 * th->th_seq - vp->rcv_nxt - vp->rcv_wnd >= 0 1063 * th->th_seq >= vp->rcv_nxt + vp->rcv_wnd 1064 */ 1065 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, tlen); 1066 1067 /* 1068 * If a new connection request is received 1069 * while in TIME_WAIT, drop the old connection 1070 * and start over if the sequence numbers 1071 * are above the previous ones. 1072 */ 1073 if ((tiflags & TH_SYN) && 1074 SEQ_GT(th->th_seq, vp->rcv_nxt)) { 1075 /* 1076 * We only support this in the !NOFDREF case, which 1077 * is to say: not here. 1078 */ 1079 goto dropwithreset; 1080 } 1081 1082 /* 1083 * If window is closed can only take segments at 1084 * window edge, and have to drop data and PUSH from 1085 * incoming segments. Continue processing, but 1086 * remember to ack. Otherwise, drop segment 1087 * and (if not RST) ack. 1088 */ 1089 if (vp->rcv_wnd == 0 && th->th_seq == vp->rcv_nxt) { 1090 t_flags |= TF_ACKNOW; 1091 TCP_STATINC(TCP_STAT_RCVWINPROBE); 1092 } else { 1093 goto dropafterack; 1094 } 1095 } else { 1096 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, todrop); 1097 } 1098 m_adj(m, -todrop); 1099 tlen -= todrop; 1100 tiflags &= ~(TH_PUSH|TH_FIN); 1101 } 1102 1103 if (tiflags & TH_RST) { 1104 if (th->th_seq != vp->rcv_nxt) 1105 goto dropafterack_ratelim; 1106 1107 vtw_del(vp->ctl, vp->vtw); 1108 goto drop; 1109 } 1110 1111 /* 1112 * If the ACK bit is off we drop the segment and return. 1113 */ 1114 if ((tiflags & TH_ACK) == 0) { 1115 if (t_flags & TF_ACKNOW) 1116 goto dropafterack; 1117 goto drop; 1118 } 1119 1120 /* 1121 * In TIME_WAIT state the only thing that should arrive 1122 * is a retransmission of the remote FIN. Acknowledge 1123 * it and restart the finack timer. 1124 */ 1125 vtw_restart(vp); 1126 goto dropafterack; 1127 1128 dropafterack: 1129 /* 1130 * Generate an ACK dropping incoming segment if it occupies 1131 * sequence space, where the ACK reflects our state. 1132 */ 1133 if (tiflags & TH_RST) 1134 goto drop; 1135 goto dropafterack2; 1136 1137 dropafterack_ratelim: 1138 /* 1139 * We may want to rate-limit ACKs against SYN/RST attack. 1140 */ 1141 if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count, 1142 tcp_ackdrop_ppslim) == 0) { 1143 /* XXX stat */ 1144 goto drop; 1145 } 1146 /* ...fall into dropafterack2... */ 1147 1148 dropafterack2: 1149 (void)tcp_respond(0, m, m, th, th->th_seq + tlen, th->th_ack, TH_ACK); 1150 return; 1151 1152 dropwithreset: 1153 /* 1154 * Generate a RST, dropping incoming segment. 1155 * Make ACK acceptable to originator of segment. 1156 */ 1157 if (tiflags & TH_RST) 1158 goto drop; 1159 1160 if (tiflags & TH_ACK) { 1161 tcp_respond(0, m, m, th, (tcp_seq)0, th->th_ack, TH_RST); 1162 } else { 1163 if (tiflags & TH_SYN) 1164 ++tlen; 1165 (void)tcp_respond(0, m, m, th, th->th_seq + tlen, (tcp_seq)0, 1166 TH_RST|TH_ACK); 1167 } 1168 return; 1169 drop: 1170 m_freem(m); 1171 } 1172 1173 /* 1174 * TCP input routine, follows pages 65-76 of RFC 793 very closely. 1175 */ 1176 void 1177 tcp_input(struct mbuf *m, int off, int proto) 1178 { 1179 struct tcphdr *th; 1180 struct ip *ip; 1181 struct inpcb *inp; 1182 #ifdef INET6 1183 struct ip6_hdr *ip6; 1184 #endif 1185 u_int8_t *optp = NULL; 1186 int optlen = 0; 1187 int len, tlen, hdroptlen = 0; 1188 struct tcpcb *tp = NULL; 1189 int tiflags; 1190 struct socket *so = NULL; 1191 int todrop, acked, ourfinisacked, needoutput = 0; 1192 bool dupseg; 1193 #ifdef TCP_DEBUG 1194 short ostate = 0; 1195 #endif 1196 u_long tiwin; 1197 struct tcp_opt_info opti; 1198 int thlen, iphlen; 1199 int af; /* af on the wire */ 1200 struct mbuf *tcp_saveti = NULL; 1201 uint32_t ts_rtt; 1202 uint8_t iptos; 1203 net_stat_ref_t tcps; 1204 vestigial_inpcb_t vestige; 1205 1206 vestige.valid = 0; 1207 1208 MCLAIM(m, &tcp_rx_mowner); 1209 1210 TCP_STATINC(TCP_STAT_RCVTOTAL); 1211 1212 memset(&opti, 0, sizeof(opti)); 1213 opti.ts_present = 0; 1214 opti.maxseg = 0; 1215 1216 /* 1217 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN. 1218 * 1219 * TCP is, by definition, unicast, so we reject all 1220 * multicast outright. 1221 * 1222 * Note, there are additional src/dst address checks in 1223 * the AF-specific code below. 1224 */ 1225 if (m->m_flags & (M_BCAST|M_MCAST)) { 1226 /* XXX stat */ 1227 goto drop; 1228 } 1229 #ifdef INET6 1230 if (m->m_flags & M_ANYCAST6) { 1231 /* XXX stat */ 1232 goto drop; 1233 } 1234 #endif 1235 1236 M_REGION_GET(th, struct tcphdr *, m, off, sizeof(struct tcphdr)); 1237 if (th == NULL) { 1238 TCP_STATINC(TCP_STAT_RCVSHORT); 1239 return; 1240 } 1241 1242 /* 1243 * Enforce alignment requirements that are violated in 1244 * some cases, see kern/50766 for details. 1245 */ 1246 if (ACCESSIBLE_POINTER(th, struct tcphdr) == 0) { 1247 m = m_copyup(m, off + sizeof(struct tcphdr), 0); 1248 if (m == NULL) { 1249 TCP_STATINC(TCP_STAT_RCVSHORT); 1250 return; 1251 } 1252 th = (struct tcphdr *)(mtod(m, char *) + off); 1253 } 1254 KASSERT(ACCESSIBLE_POINTER(th, struct tcphdr)); 1255 1256 /* 1257 * Get IP and TCP header. 1258 * Note: IP leaves IP header in first mbuf. 1259 */ 1260 ip = mtod(m, struct ip *); 1261 #ifdef INET6 1262 ip6 = mtod(m, struct ip6_hdr *); 1263 #endif 1264 switch (ip->ip_v) { 1265 case 4: 1266 af = AF_INET; 1267 iphlen = sizeof(struct ip); 1268 1269 if (IN_MULTICAST(ip->ip_dst.s_addr) || 1270 in_broadcast(ip->ip_dst, m_get_rcvif_NOMPSAFE(m))) 1271 goto drop; 1272 1273 /* We do the checksum after PCB lookup... */ 1274 len = ntohs(ip->ip_len); 1275 tlen = len - off; 1276 iptos = ip->ip_tos; 1277 break; 1278 #ifdef INET6 1279 case 6: 1280 iphlen = sizeof(struct ip6_hdr); 1281 af = AF_INET6; 1282 1283 /* 1284 * Be proactive about unspecified IPv6 address in source. 1285 * As we use all-zero to indicate unbounded/unconnected pcb, 1286 * unspecified IPv6 address can be used to confuse us. 1287 * 1288 * Note that packets with unspecified IPv6 destination is 1289 * already dropped in ip6_input. 1290 */ 1291 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 1292 /* XXX stat */ 1293 goto drop; 1294 } 1295 1296 /* 1297 * Make sure destination address is not multicast. 1298 * Source address checked in ip6_input(). 1299 */ 1300 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1301 /* XXX stat */ 1302 goto drop; 1303 } 1304 1305 /* We do the checksum after PCB lookup... */ 1306 len = m->m_pkthdr.len; 1307 tlen = len - off; 1308 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 1309 break; 1310 #endif 1311 default: 1312 m_freem(m); 1313 return; 1314 } 1315 1316 1317 /* 1318 * Check that TCP offset makes sense, pull out TCP options and 1319 * adjust length. 1320 */ 1321 thlen = th->th_off << 2; 1322 if (thlen < sizeof(struct tcphdr) || thlen > tlen) { 1323 TCP_STATINC(TCP_STAT_RCVBADOFF); 1324 goto drop; 1325 } 1326 tlen -= thlen; 1327 1328 if (thlen > sizeof(struct tcphdr)) { 1329 M_REGION_GET(th, struct tcphdr *, m, off, thlen); 1330 if (th == NULL) { 1331 TCP_STATINC(TCP_STAT_RCVSHORT); 1332 return; 1333 } 1334 KASSERT(ACCESSIBLE_POINTER(th, struct tcphdr)); 1335 optlen = thlen - sizeof(struct tcphdr); 1336 optp = ((u_int8_t *)th) + sizeof(struct tcphdr); 1337 1338 /* 1339 * Do quick retrieval of timestamp options. 1340 * 1341 * If timestamp is the only option and it's formatted as 1342 * recommended in RFC 1323 appendix A, we quickly get the 1343 * values now and don't bother calling tcp_dooptions(), 1344 * etc. 1345 */ 1346 if ((optlen == TCPOLEN_TSTAMP_APPA || 1347 (optlen > TCPOLEN_TSTAMP_APPA && 1348 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && 1349 be32dec(optp) == TCPOPT_TSTAMP_HDR && 1350 (th->th_flags & TH_SYN) == 0) { 1351 opti.ts_present = 1; 1352 opti.ts_val = be32dec(optp + 4); 1353 opti.ts_ecr = be32dec(optp + 8); 1354 optp = NULL; /* we've parsed the options */ 1355 } 1356 } 1357 tiflags = th->th_flags; 1358 1359 /* 1360 * Checksum extended TCP header and data 1361 */ 1362 if (tcp_input_checksum(af, m, th, off, thlen, tlen)) 1363 goto badcsum; 1364 1365 /* 1366 * Locate pcb for segment. 1367 */ 1368 findpcb: 1369 inp = NULL; 1370 switch (af) { 1371 case AF_INET: 1372 inp = inpcb_lookup(&tcbtable, ip->ip_src, th->th_sport, 1373 ip->ip_dst, th->th_dport, &vestige); 1374 if (inp == NULL && !vestige.valid) { 1375 TCP_STATINC(TCP_STAT_PCBHASHMISS); 1376 inp = inpcb_lookup_bound(&tcbtable, ip->ip_dst, 1377 th->th_dport); 1378 } 1379 #ifdef INET6 1380 if (inp == NULL && !vestige.valid) { 1381 struct in6_addr s, d; 1382 1383 /* mapped addr case */ 1384 in6_in_2_v4mapin6(&ip->ip_src, &s); 1385 in6_in_2_v4mapin6(&ip->ip_dst, &d); 1386 inp = in6pcb_lookup(&tcbtable, &s, 1387 th->th_sport, &d, th->th_dport, 0, &vestige); 1388 if (inp == NULL && !vestige.valid) { 1389 TCP_STATINC(TCP_STAT_PCBHASHMISS); 1390 inp = in6pcb_lookup_bound(&tcbtable, &d, 1391 th->th_dport, 0); 1392 } 1393 } 1394 #endif 1395 if (inp == NULL && !vestige.valid) { 1396 TCP_STATINC(TCP_STAT_NOPORT); 1397 if (tcp_log_refused && 1398 (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) { 1399 tcp4_log_refused(ip, th); 1400 } 1401 tcp_fields_to_host(th); 1402 goto dropwithreset_ratelim; 1403 } 1404 #if defined(IPSEC) 1405 if (ipsec_used) { 1406 if (inp && ipsec_in_reject(m, inp)) 1407 goto drop; 1408 } 1409 #endif /*IPSEC*/ 1410 break; 1411 #ifdef INET6 1412 case AF_INET6: 1413 { 1414 int faith; 1415 1416 #if defined(NFAITH) && NFAITH > 0 1417 faith = faithprefix(&ip6->ip6_dst); 1418 #else 1419 faith = 0; 1420 #endif 1421 inp = in6pcb_lookup(&tcbtable, &ip6->ip6_src, 1422 th->th_sport, &ip6->ip6_dst, th->th_dport, faith, &vestige); 1423 if (inp == NULL && !vestige.valid) { 1424 TCP_STATINC(TCP_STAT_PCBHASHMISS); 1425 inp = in6pcb_lookup_bound(&tcbtable, &ip6->ip6_dst, 1426 th->th_dport, faith); 1427 } 1428 if (inp == NULL && !vestige.valid) { 1429 TCP_STATINC(TCP_STAT_NOPORT); 1430 if (tcp_log_refused && 1431 (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) { 1432 tcp6_log_refused(ip6, th); 1433 } 1434 tcp_fields_to_host(th); 1435 goto dropwithreset_ratelim; 1436 } 1437 #if defined(IPSEC) 1438 if (ipsec_used && inp && ipsec_in_reject(m, inp)) 1439 goto drop; 1440 #endif 1441 break; 1442 } 1443 #endif 1444 } 1445 1446 tcp_fields_to_host(th); 1447 1448 /* 1449 * If the state is CLOSED (i.e., TCB does not exist) then 1450 * all data in the incoming segment is discarded. 1451 * If the TCB exists but is in CLOSED state, it is embryonic, 1452 * but should either do a listen or a connect soon. 1453 */ 1454 tp = NULL; 1455 so = NULL; 1456 if (inp) { 1457 /* Check the minimum TTL for socket. */ 1458 if (inp->inp_af == AF_INET && ip->ip_ttl < in4p_ip_minttl(inp)) 1459 goto drop; 1460 1461 tp = intotcpcb(inp); 1462 so = inp->inp_socket; 1463 } else if (vestige.valid) { 1464 /* We do not support the resurrection of vtw tcpcps. */ 1465 tcp_vtw_input(th, &vestige, m, tlen); 1466 m = NULL; 1467 goto drop; 1468 } 1469 1470 if (tp == NULL) 1471 goto dropwithreset_ratelim; 1472 if (tp->t_state == TCPS_CLOSED) 1473 goto drop; 1474 1475 KASSERT(so->so_lock == softnet_lock); 1476 KASSERT(solocked(so)); 1477 1478 /* Unscale the window into a 32-bit value. */ 1479 if ((tiflags & TH_SYN) == 0) 1480 tiwin = th->th_win << tp->snd_scale; 1481 else 1482 tiwin = th->th_win; 1483 1484 #ifdef INET6 1485 /* save packet options if user wanted */ 1486 if (inp->inp_af == AF_INET6 && (inp->inp_flags & IN6P_CONTROLOPTS)) { 1487 if (inp->inp_options) { 1488 m_freem(inp->inp_options); 1489 inp->inp_options = NULL; 1490 } 1491 ip6_savecontrol(inp, &inp->inp_options, ip6, m); 1492 } 1493 #endif 1494 1495 if (so->so_options & SO_DEBUG) { 1496 #ifdef TCP_DEBUG 1497 ostate = tp->t_state; 1498 #endif 1499 1500 tcp_saveti = NULL; 1501 if (iphlen + sizeof(struct tcphdr) > MHLEN) 1502 goto nosave; 1503 1504 if (m->m_len > iphlen && (m->m_flags & M_EXT) == 0) { 1505 tcp_saveti = m_copym(m, 0, iphlen, M_DONTWAIT); 1506 if (tcp_saveti == NULL) 1507 goto nosave; 1508 } else { 1509 MGETHDR(tcp_saveti, M_DONTWAIT, MT_HEADER); 1510 if (tcp_saveti == NULL) 1511 goto nosave; 1512 MCLAIM(m, &tcp_mowner); 1513 tcp_saveti->m_len = iphlen; 1514 m_copydata(m, 0, iphlen, 1515 mtod(tcp_saveti, void *)); 1516 } 1517 1518 if (M_TRAILINGSPACE(tcp_saveti) < sizeof(struct tcphdr)) { 1519 m_freem(tcp_saveti); 1520 tcp_saveti = NULL; 1521 } else { 1522 tcp_saveti->m_len += sizeof(struct tcphdr); 1523 memcpy(mtod(tcp_saveti, char *) + iphlen, th, 1524 sizeof(struct tcphdr)); 1525 } 1526 nosave:; 1527 } 1528 1529 if (so->so_options & SO_ACCEPTCONN) { 1530 union syn_cache_sa src; 1531 union syn_cache_sa dst; 1532 1533 KASSERT(tp->t_state == TCPS_LISTEN); 1534 1535 memset(&src, 0, sizeof(src)); 1536 memset(&dst, 0, sizeof(dst)); 1537 switch (af) { 1538 case AF_INET: 1539 src.sin.sin_len = sizeof(struct sockaddr_in); 1540 src.sin.sin_family = AF_INET; 1541 src.sin.sin_addr = ip->ip_src; 1542 src.sin.sin_port = th->th_sport; 1543 1544 dst.sin.sin_len = sizeof(struct sockaddr_in); 1545 dst.sin.sin_family = AF_INET; 1546 dst.sin.sin_addr = ip->ip_dst; 1547 dst.sin.sin_port = th->th_dport; 1548 break; 1549 #ifdef INET6 1550 case AF_INET6: 1551 src.sin6.sin6_len = sizeof(struct sockaddr_in6); 1552 src.sin6.sin6_family = AF_INET6; 1553 src.sin6.sin6_addr = ip6->ip6_src; 1554 src.sin6.sin6_port = th->th_sport; 1555 1556 dst.sin6.sin6_len = sizeof(struct sockaddr_in6); 1557 dst.sin6.sin6_family = AF_INET6; 1558 dst.sin6.sin6_addr = ip6->ip6_dst; 1559 dst.sin6.sin6_port = th->th_dport; 1560 break; 1561 #endif 1562 } 1563 1564 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 1565 if (tiflags & TH_RST) { 1566 syn_cache_reset(&src.sa, &dst.sa, th); 1567 } else if ((tiflags & (TH_ACK|TH_SYN)) == 1568 (TH_ACK|TH_SYN)) { 1569 /* 1570 * Received a SYN,ACK. This should never 1571 * happen while we are in LISTEN. Send an RST. 1572 */ 1573 goto badsyn; 1574 } else if (tiflags & TH_ACK) { 1575 so = syn_cache_get(&src.sa, &dst.sa, th, so, m); 1576 if (so == NULL) { 1577 /* 1578 * We don't have a SYN for this ACK; 1579 * send an RST. 1580 */ 1581 goto badsyn; 1582 } else if (so == (struct socket *)(-1)) { 1583 /* 1584 * We were unable to create the 1585 * connection. If the 3-way handshake 1586 * was completed, and RST has been 1587 * sent to the peer. Since the mbuf 1588 * might be in use for the reply, do 1589 * not free it. 1590 */ 1591 m = NULL; 1592 } else { 1593 /* 1594 * We have created a full-blown 1595 * connection. 1596 */ 1597 inp = sotoinpcb(so); 1598 tp = intotcpcb(inp); 1599 if (tp == NULL) 1600 goto badsyn; /*XXX*/ 1601 tiwin <<= tp->snd_scale; 1602 goto after_listen; 1603 } 1604 } else { 1605 /* 1606 * None of RST, SYN or ACK was set. 1607 * This is an invalid packet for a 1608 * TCB in LISTEN state. Send a RST. 1609 */ 1610 goto badsyn; 1611 } 1612 } else { 1613 /* 1614 * Received a SYN. 1615 */ 1616 1617 #ifdef INET6 1618 /* 1619 * If deprecated address is forbidden, we do 1620 * not accept SYN to deprecated interface 1621 * address to prevent any new inbound 1622 * connection from getting established. 1623 * When we do not accept SYN, we send a TCP 1624 * RST, with deprecated source address (instead 1625 * of dropping it). We compromise it as it is 1626 * much better for peer to send a RST, and 1627 * RST will be the final packet for the 1628 * exchange. 1629 * 1630 * If we do not forbid deprecated addresses, we 1631 * accept the SYN packet. RFC2462 does not 1632 * suggest dropping SYN in this case. 1633 * If we decipher RFC2462 5.5.4, it says like 1634 * this: 1635 * 1. use of deprecated addr with existing 1636 * communication is okay - "SHOULD continue 1637 * to be used" 1638 * 2. use of it with new communication: 1639 * (2a) "SHOULD NOT be used if alternate 1640 * address with sufficient scope is 1641 * available" 1642 * (2b) nothing mentioned otherwise. 1643 * Here we fall into (2b) case as we have no 1644 * choice in our source address selection - we 1645 * must obey the peer. 1646 * 1647 * The wording in RFC2462 is confusing, and 1648 * there are multiple description text for 1649 * deprecated address handling - worse, they 1650 * are not exactly the same. I believe 5.5.4 1651 * is the best one, so we follow 5.5.4. 1652 */ 1653 if (af == AF_INET6 && !ip6_use_deprecated) { 1654 struct in6_ifaddr *ia6; 1655 int s; 1656 struct ifnet *rcvif = m_get_rcvif(m, &s); 1657 if (rcvif == NULL) 1658 goto dropwithreset; /* XXX */ 1659 if ((ia6 = in6ifa_ifpwithaddr(rcvif, 1660 &ip6->ip6_dst)) && 1661 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 1662 tp = NULL; 1663 m_put_rcvif(rcvif, &s); 1664 goto dropwithreset; 1665 } 1666 m_put_rcvif(rcvif, &s); 1667 } 1668 #endif 1669 1670 /* 1671 * LISTEN socket received a SYN from itself? This 1672 * can't possibly be valid; drop the packet. 1673 */ 1674 if (th->th_sport == th->th_dport) { 1675 int eq = 0; 1676 1677 switch (af) { 1678 case AF_INET: 1679 eq = in_hosteq(ip->ip_src, ip->ip_dst); 1680 break; 1681 #ifdef INET6 1682 case AF_INET6: 1683 eq = IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, 1684 &ip6->ip6_dst); 1685 break; 1686 #endif 1687 } 1688 if (eq) { 1689 TCP_STATINC(TCP_STAT_BADSYN); 1690 goto drop; 1691 } 1692 } 1693 1694 /* 1695 * SYN looks ok; create compressed TCP 1696 * state for it. 1697 */ 1698 if (so->so_qlen <= so->so_qlimit && 1699 syn_cache_add(&src.sa, &dst.sa, th, off, 1700 so, m, optp, optlen, &opti)) 1701 m = NULL; 1702 } 1703 1704 goto drop; 1705 } 1706 1707 after_listen: 1708 /* 1709 * From here on, we're dealing with !LISTEN. 1710 */ 1711 KASSERT(tp->t_state != TCPS_LISTEN); 1712 1713 /* 1714 * Segment received on connection. 1715 * Reset idle time and keep-alive timer. 1716 */ 1717 tp->t_rcvtime = tcp_now; 1718 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1719 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle); 1720 1721 /* 1722 * Process options. 1723 */ 1724 #ifdef TCP_SIGNATURE 1725 if (optp || (tp->t_flags & TF_SIGNATURE)) 1726 #else 1727 if (optp) 1728 #endif 1729 if (tcp_dooptions(tp, optp, optlen, th, m, off, &opti) < 0) 1730 goto drop; 1731 1732 if (TCP_SACK_ENABLED(tp)) { 1733 tcp_del_sackholes(tp, th); 1734 } 1735 1736 if (TCP_ECN_ALLOWED(tp)) { 1737 if (tiflags & TH_CWR) { 1738 tp->t_flags &= ~TF_ECN_SND_ECE; 1739 } 1740 switch (iptos & IPTOS_ECN_MASK) { 1741 case IPTOS_ECN_CE: 1742 tp->t_flags |= TF_ECN_SND_ECE; 1743 TCP_STATINC(TCP_STAT_ECN_CE); 1744 break; 1745 case IPTOS_ECN_ECT0: 1746 TCP_STATINC(TCP_STAT_ECN_ECT); 1747 break; 1748 case IPTOS_ECN_ECT1: 1749 /* XXX: ignore for now -- rpaulo */ 1750 break; 1751 } 1752 /* 1753 * Congestion experienced. 1754 * Ignore if we are already trying to recover. 1755 */ 1756 if ((tiflags & TH_ECE) && SEQ_GEQ(tp->snd_una, tp->snd_recover)) 1757 tp->t_congctl->cong_exp(tp); 1758 } 1759 1760 if (opti.ts_present && opti.ts_ecr) { 1761 /* 1762 * Calculate the RTT from the returned time stamp and the 1763 * connection's time base. If the time stamp is later than 1764 * the current time, or is extremely old, fall back to non-1323 1765 * RTT calculation. Since ts_rtt is unsigned, we can test both 1766 * at the same time. 1767 * 1768 * Note that ts_rtt is in units of slow ticks (500 1769 * ms). Since most earthbound RTTs are < 500 ms, 1770 * observed values will have large quantization noise. 1771 * Our smoothed RTT is then the fraction of observed 1772 * samples that are 1 tick instead of 0 (times 500 1773 * ms). 1774 * 1775 * ts_rtt is increased by 1 to denote a valid sample, 1776 * with 0 indicating an invalid measurement. This 1777 * extra 1 must be removed when ts_rtt is used, or 1778 * else an erroneous extra 500 ms will result. 1779 */ 1780 ts_rtt = TCP_TIMESTAMP(tp) - opti.ts_ecr + 1; 1781 if (ts_rtt > TCP_PAWS_IDLE) 1782 ts_rtt = 0; 1783 } else { 1784 ts_rtt = 0; 1785 } 1786 1787 /* 1788 * Fast path: check for the two common cases of a uni-directional 1789 * data transfer. If: 1790 * o We are in the ESTABLISHED state, and 1791 * o The packet has no control flags, and 1792 * o The packet is in-sequence, and 1793 * o The window didn't change, and 1794 * o We are not retransmitting 1795 * It's a candidate. 1796 * 1797 * If the length (tlen) is zero and the ack moved forward, we're 1798 * the sender side of the transfer. Just free the data acked and 1799 * wake any higher level process that was blocked waiting for 1800 * space. 1801 * 1802 * If the length is non-zero and the ack didn't move, we're the 1803 * receiver side. If we're getting packets in-order (the reassembly 1804 * queue is empty), add the data to the socket buffer and note 1805 * that we need a delayed ack. 1806 */ 1807 if (tp->t_state == TCPS_ESTABLISHED && 1808 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ECE|TH_CWR|TH_ACK)) 1809 == TH_ACK && 1810 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) && 1811 th->th_seq == tp->rcv_nxt && 1812 tiwin && tiwin == tp->snd_wnd && 1813 tp->snd_nxt == tp->snd_max) { 1814 1815 /* 1816 * If last ACK falls within this segment's sequence numbers, 1817 * record the timestamp. 1818 * NOTE that the test is modified according to the latest 1819 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1820 * 1821 * note that we already know 1822 * TSTMP_GEQ(opti.ts_val, tp->ts_recent) 1823 */ 1824 if (opti.ts_present && SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1825 tp->ts_recent_age = tcp_now; 1826 tp->ts_recent = opti.ts_val; 1827 } 1828 1829 if (tlen == 0) { 1830 /* Ack prediction. */ 1831 if (SEQ_GT(th->th_ack, tp->snd_una) && 1832 SEQ_LEQ(th->th_ack, tp->snd_max) && 1833 tp->snd_cwnd >= tp->snd_wnd && 1834 tp->t_partialacks < 0) { 1835 /* 1836 * this is a pure ack for outstanding data. 1837 */ 1838 if (ts_rtt) 1839 tcp_xmit_timer(tp, ts_rtt - 1); 1840 else if (tp->t_rtttime && 1841 SEQ_GT(th->th_ack, tp->t_rtseq)) 1842 tcp_xmit_timer(tp, 1843 tcp_now - tp->t_rtttime); 1844 acked = th->th_ack - tp->snd_una; 1845 tcps = TCP_STAT_GETREF(); 1846 _NET_STATINC_REF(tcps, TCP_STAT_PREDACK); 1847 _NET_STATINC_REF(tcps, TCP_STAT_RCVACKPACK); 1848 _NET_STATADD_REF(tcps, TCP_STAT_RCVACKBYTE, 1849 acked); 1850 TCP_STAT_PUTREF(); 1851 nd_hint(tp); 1852 1853 if (acked > (tp->t_lastoff - tp->t_inoff)) 1854 tp->t_lastm = NULL; 1855 sbdrop(&so->so_snd, acked); 1856 tp->t_lastoff -= acked; 1857 1858 icmp_check(tp, th, acked); 1859 1860 tp->snd_una = th->th_ack; 1861 tp->snd_fack = tp->snd_una; 1862 if (SEQ_LT(tp->snd_high, tp->snd_una)) 1863 tp->snd_high = tp->snd_una; 1864 /* 1865 * drag snd_wl2 along so only newer 1866 * ACKs can update the window size. 1867 * also avoids the state where snd_wl2 1868 * is eventually larger than th_ack and thus 1869 * blocking the window update mechanism and 1870 * the connection gets stuck for a loooong 1871 * time in the zero sized send window state. 1872 * 1873 * see PR/kern 55567 1874 */ 1875 tp->snd_wl2 = tp->snd_una; 1876 1877 m_freem(m); 1878 1879 /* 1880 * If all outstanding data are acked, stop 1881 * retransmit timer, otherwise restart timer 1882 * using current (possibly backed-off) value. 1883 * If process is waiting for space, 1884 * wakeup/selnotify/signal. If data 1885 * are ready to send, let tcp_output 1886 * decide between more output or persist. 1887 */ 1888 if (tp->snd_una == tp->snd_max) 1889 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1890 else if (TCP_TIMER_ISARMED(tp, 1891 TCPT_PERSIST) == 0) 1892 TCP_TIMER_ARM(tp, TCPT_REXMT, 1893 tp->t_rxtcur); 1894 1895 sowwakeup(so); 1896 if (so->so_snd.sb_cc) { 1897 KERNEL_LOCK(1, NULL); 1898 (void)tcp_output(tp); 1899 KERNEL_UNLOCK_ONE(NULL); 1900 } 1901 if (tcp_saveti) 1902 m_freem(tcp_saveti); 1903 return; 1904 } 1905 } else if (th->th_ack == tp->snd_una && 1906 TAILQ_FIRST(&tp->segq) == NULL && 1907 tlen <= sbspace(&so->so_rcv)) { 1908 int newsize = 0; 1909 1910 /* 1911 * this is a pure, in-sequence data packet 1912 * with nothing on the reassembly queue and 1913 * we have enough buffer space to take it. 1914 */ 1915 tp->rcv_nxt += tlen; 1916 1917 /* 1918 * Pull rcv_up up to prevent seq wrap relative to 1919 * rcv_nxt. 1920 */ 1921 tp->rcv_up = tp->rcv_nxt; 1922 1923 /* 1924 * Pull snd_wl1 up to prevent seq wrap relative to 1925 * th_seq. 1926 */ 1927 tp->snd_wl1 = th->th_seq; 1928 1929 tcps = TCP_STAT_GETREF(); 1930 _NET_STATINC_REF(tcps, TCP_STAT_PREDDAT); 1931 _NET_STATINC_REF(tcps, TCP_STAT_RCVPACK); 1932 _NET_STATADD_REF(tcps, TCP_STAT_RCVBYTE, tlen); 1933 TCP_STAT_PUTREF(); 1934 nd_hint(tp); 1935 /* 1936 * Automatic sizing enables the performance of large buffers 1937 * and most of the efficiency of small ones by only allocating 1938 * space when it is needed. 1939 * 1940 * On the receive side the socket buffer memory is only rarely 1941 * used to any significant extent. This allows us to be much 1942 * more aggressive in scaling the receive socket buffer. For 1943 * the case that the buffer space is actually used to a large 1944 * extent and we run out of kernel memory we can simply drop 1945 * the new segments; TCP on the sender will just retransmit it 1946 * later. Setting the buffer size too big may only consume too 1947 * much kernel memory if the application doesn't read() from 1948 * the socket or packet loss or reordering makes use of the 1949 * reassembly queue. 1950 * 1951 * The criteria to step up the receive buffer one notch are: 1952 * 1. the number of bytes received during the time it takes 1953 * one timestamp to be reflected back to us (the RTT); 1954 * 2. received bytes per RTT is within seven eighth of the 1955 * current socket buffer size; 1956 * 3. receive buffer size has not hit maximal automatic size; 1957 * 1958 * This algorithm does one step per RTT at most and only if 1959 * we receive a bulk stream w/o packet losses or reorderings. 1960 * Shrinking the buffer during idle times is not necessary as 1961 * it doesn't consume any memory when idle. 1962 * 1963 * TODO: Only step up if the application is actually serving 1964 * the buffer to better manage the socket buffer resources. 1965 */ 1966 if (tcp_do_autorcvbuf && 1967 opti.ts_ecr && 1968 (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 1969 if (opti.ts_ecr > tp->rfbuf_ts && 1970 opti.ts_ecr - tp->rfbuf_ts < PR_SLOWHZ) { 1971 if (tp->rfbuf_cnt > 1972 (so->so_rcv.sb_hiwat / 8 * 7) && 1973 so->so_rcv.sb_hiwat < 1974 tcp_autorcvbuf_max) { 1975 newsize = 1976 uimin(so->so_rcv.sb_hiwat + 1977 tcp_autorcvbuf_inc, 1978 tcp_autorcvbuf_max); 1979 } 1980 /* Start over with next RTT. */ 1981 tp->rfbuf_ts = 0; 1982 tp->rfbuf_cnt = 0; 1983 } else 1984 tp->rfbuf_cnt += tlen; /* add up */ 1985 } 1986 1987 /* 1988 * Drop TCP, IP headers and TCP options then add data 1989 * to socket buffer. 1990 */ 1991 if (so->so_state & SS_CANTRCVMORE) { 1992 m_freem(m); 1993 } else { 1994 /* 1995 * Set new socket buffer size. 1996 * Give up when limit is reached. 1997 */ 1998 if (newsize) 1999 if (!sbreserve(&so->so_rcv, 2000 newsize, so)) 2001 so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 2002 m_adj(m, off + thlen); 2003 sbappendstream(&so->so_rcv, m); 2004 } 2005 sorwakeup(so); 2006 tcp_setup_ack(tp, th); 2007 if (tp->t_flags & TF_ACKNOW) { 2008 KERNEL_LOCK(1, NULL); 2009 (void)tcp_output(tp); 2010 KERNEL_UNLOCK_ONE(NULL); 2011 } 2012 if (tcp_saveti) 2013 m_freem(tcp_saveti); 2014 return; 2015 } 2016 } 2017 2018 /* 2019 * Compute mbuf offset to TCP data segment. 2020 */ 2021 hdroptlen = off + thlen; 2022 2023 /* 2024 * Calculate amount of space in receive window. Receive window is 2025 * amount of space in rcv queue, but not less than advertised 2026 * window. 2027 */ 2028 { 2029 int win; 2030 win = sbspace(&so->so_rcv); 2031 if (win < 0) 2032 win = 0; 2033 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 2034 } 2035 2036 /* Reset receive buffer auto scaling when not in bulk receive mode. */ 2037 tp->rfbuf_ts = 0; 2038 tp->rfbuf_cnt = 0; 2039 2040 switch (tp->t_state) { 2041 /* 2042 * If the state is SYN_SENT: 2043 * if seg contains an ACK, but not for our SYN, drop the input. 2044 * if seg contains a RST, then drop the connection. 2045 * if seg does not contain SYN, then drop it. 2046 * Otherwise this is an acceptable SYN segment 2047 * initialize tp->rcv_nxt and tp->irs 2048 * if seg contains ack then advance tp->snd_una 2049 * if seg contains a ECE and ECN support is enabled, the stream 2050 * is ECN capable. 2051 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2052 * arrange for segment to be acked (eventually) 2053 * continue processing rest of data/controls, beginning with URG 2054 */ 2055 case TCPS_SYN_SENT: 2056 if ((tiflags & TH_ACK) && 2057 (SEQ_LEQ(th->th_ack, tp->iss) || 2058 SEQ_GT(th->th_ack, tp->snd_max))) 2059 goto dropwithreset; 2060 if (tiflags & TH_RST) { 2061 if (tiflags & TH_ACK) 2062 tp = tcp_drop(tp, ECONNREFUSED); 2063 goto drop; 2064 } 2065 if ((tiflags & TH_SYN) == 0) 2066 goto drop; 2067 if (tiflags & TH_ACK) { 2068 tp->snd_una = th->th_ack; 2069 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2070 tp->snd_nxt = tp->snd_una; 2071 if (SEQ_LT(tp->snd_high, tp->snd_una)) 2072 tp->snd_high = tp->snd_una; 2073 TCP_TIMER_DISARM(tp, TCPT_REXMT); 2074 2075 if ((tiflags & TH_ECE) && tcp_do_ecn) { 2076 tp->t_flags |= TF_ECN_PERMIT; 2077 TCP_STATINC(TCP_STAT_ECN_SHS); 2078 } 2079 } 2080 tp->irs = th->th_seq; 2081 tcp_rcvseqinit(tp); 2082 tp->t_flags |= TF_ACKNOW; 2083 tcp_mss_from_peer(tp, opti.maxseg); 2084 2085 /* 2086 * Initialize the initial congestion window. If we 2087 * had to retransmit the SYN, we must initialize cwnd 2088 * to 1 segment (i.e. the Loss Window). 2089 */ 2090 if (tp->t_flags & TF_SYN_REXMT) 2091 tp->snd_cwnd = tp->t_peermss; 2092 else { 2093 int ss = tcp_init_win; 2094 if (inp->inp_af == AF_INET && in_localaddr(in4p_faddr(inp))) 2095 ss = tcp_init_win_local; 2096 #ifdef INET6 2097 else if (inp->inp_af == AF_INET6 && in6_localaddr(&in6p_faddr(inp))) 2098 ss = tcp_init_win_local; 2099 #endif 2100 tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss); 2101 } 2102 2103 tcp_rmx_rtt(tp); 2104 if (tiflags & TH_ACK) { 2105 TCP_STATINC(TCP_STAT_CONNECTS); 2106 /* 2107 * move tcp_established before soisconnected 2108 * because upcall handler can drive tcp_output 2109 * functionality. 2110 * XXX we might call soisconnected at the end of 2111 * all processing 2112 */ 2113 tcp_established(tp); 2114 soisconnected(so); 2115 /* Do window scaling on this connection? */ 2116 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2117 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2118 tp->snd_scale = tp->requested_s_scale; 2119 tp->rcv_scale = tp->request_r_scale; 2120 } 2121 TCP_REASS_LOCK(tp); 2122 (void)tcp_reass(tp, NULL, NULL, tlen); 2123 /* 2124 * if we didn't have to retransmit the SYN, 2125 * use its rtt as our initial srtt & rtt var. 2126 */ 2127 if (tp->t_rtttime) 2128 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 2129 } else { 2130 tp->t_state = TCPS_SYN_RECEIVED; 2131 } 2132 2133 /* 2134 * Advance th->th_seq to correspond to first data byte. 2135 * If data, trim to stay within window, 2136 * dropping FIN if necessary. 2137 */ 2138 th->th_seq++; 2139 if (tlen > tp->rcv_wnd) { 2140 todrop = tlen - tp->rcv_wnd; 2141 m_adj(m, -todrop); 2142 tlen = tp->rcv_wnd; 2143 tiflags &= ~TH_FIN; 2144 tcps = TCP_STAT_GETREF(); 2145 _NET_STATINC_REF(tcps, TCP_STAT_RCVPACKAFTERWIN); 2146 _NET_STATADD_REF(tcps, TCP_STAT_RCVBYTEAFTERWIN, 2147 todrop); 2148 TCP_STAT_PUTREF(); 2149 } 2150 tp->snd_wl1 = th->th_seq - 1; 2151 tp->rcv_up = th->th_seq; 2152 goto step6; 2153 2154 /* 2155 * If the state is SYN_RECEIVED: 2156 * If seg contains an ACK, but not for our SYN, drop the input 2157 * and generate an RST. See page 36, rfc793 2158 */ 2159 case TCPS_SYN_RECEIVED: 2160 if ((tiflags & TH_ACK) && 2161 (SEQ_LEQ(th->th_ack, tp->iss) || 2162 SEQ_GT(th->th_ack, tp->snd_max))) 2163 goto dropwithreset; 2164 break; 2165 } 2166 2167 /* 2168 * From here on, we're dealing with !LISTEN and !SYN_SENT. 2169 */ 2170 KASSERT(tp->t_state != TCPS_LISTEN && 2171 tp->t_state != TCPS_SYN_SENT); 2172 2173 /* 2174 * RFC1323 PAWS: if we have a timestamp reply on this segment and 2175 * it's less than ts_recent, drop it. 2176 */ 2177 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && 2178 TSTMP_LT(opti.ts_val, tp->ts_recent)) { 2179 /* Check to see if ts_recent is over 24 days old. */ 2180 if (tcp_now - tp->ts_recent_age > TCP_PAWS_IDLE) { 2181 /* 2182 * Invalidate ts_recent. If this segment updates 2183 * ts_recent, the age will be reset later and ts_recent 2184 * will get a valid value. If it does not, setting 2185 * ts_recent to zero will at least satisfy the 2186 * requirement that zero be placed in the timestamp 2187 * echo reply when ts_recent isn't valid. The 2188 * age isn't reset until we get a valid ts_recent 2189 * because we don't want out-of-order segments to be 2190 * dropped when ts_recent is old. 2191 */ 2192 tp->ts_recent = 0; 2193 } else { 2194 tcps = TCP_STAT_GETREF(); 2195 _NET_STATINC_REF(tcps, TCP_STAT_RCVDUPPACK); 2196 _NET_STATADD_REF(tcps, TCP_STAT_RCVDUPBYTE, tlen); 2197 _NET_STATINC_REF(tcps, TCP_STAT_PAWSDROP); 2198 TCP_STAT_PUTREF(); 2199 tcp_new_dsack(tp, th->th_seq, tlen); 2200 goto dropafterack; 2201 } 2202 } 2203 2204 /* 2205 * Check that at least some bytes of the segment are within the 2206 * receive window. If segment begins before rcv_nxt, drop leading 2207 * data (and SYN); if nothing left, just ack. 2208 */ 2209 todrop = tp->rcv_nxt - th->th_seq; 2210 dupseg = false; 2211 if (todrop > 0) { 2212 if (tiflags & TH_SYN) { 2213 tiflags &= ~TH_SYN; 2214 th->th_seq++; 2215 tcp_urp_drop(th, 1, &tiflags); 2216 todrop--; 2217 } 2218 if (todrop > tlen || 2219 (todrop == tlen && (tiflags & TH_FIN) == 0)) { 2220 /* 2221 * Any valid FIN or RST must be to the left of the 2222 * window. At this point the FIN or RST must be a 2223 * duplicate or out of sequence; drop it. 2224 */ 2225 if (tiflags & TH_RST) 2226 goto drop; 2227 tiflags &= ~(TH_FIN|TH_RST); 2228 2229 /* 2230 * Send an ACK to resynchronize and drop any data. 2231 * But keep on processing for RST or ACK. 2232 */ 2233 tp->t_flags |= TF_ACKNOW; 2234 todrop = tlen; 2235 dupseg = true; 2236 tcps = TCP_STAT_GETREF(); 2237 _NET_STATINC_REF(tcps, TCP_STAT_RCVDUPPACK); 2238 _NET_STATADD_REF(tcps, TCP_STAT_RCVDUPBYTE, todrop); 2239 TCP_STAT_PUTREF(); 2240 } else if ((tiflags & TH_RST) && th->th_seq != tp->rcv_nxt) { 2241 /* 2242 * Test for reset before adjusting the sequence 2243 * number for overlapping data. 2244 */ 2245 goto dropafterack_ratelim; 2246 } else { 2247 tcps = TCP_STAT_GETREF(); 2248 _NET_STATINC_REF(tcps, TCP_STAT_RCVPARTDUPPACK); 2249 _NET_STATADD_REF(tcps, TCP_STAT_RCVPARTDUPBYTE, 2250 todrop); 2251 TCP_STAT_PUTREF(); 2252 } 2253 tcp_new_dsack(tp, th->th_seq, todrop); 2254 hdroptlen += todrop; /* drop from head afterwards (m_adj) */ 2255 th->th_seq += todrop; 2256 tlen -= todrop; 2257 tcp_urp_drop(th, todrop, &tiflags); 2258 } 2259 2260 /* 2261 * If new data is received on a connection after the user processes 2262 * are gone, then RST the other end. 2263 */ 2264 if ((so->so_state & SS_NOFDREF) && 2265 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2266 tp = tcp_close(tp); 2267 TCP_STATINC(TCP_STAT_RCVAFTERCLOSE); 2268 goto dropwithreset; 2269 } 2270 2271 /* 2272 * If the segment ends after the window, drop trailing data (and 2273 * PUSH and FIN); if nothing left, just ACK. 2274 */ 2275 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2276 if (todrop > 0) { 2277 TCP_STATINC(TCP_STAT_RCVPACKAFTERWIN); 2278 if (todrop >= tlen) { 2279 /* 2280 * The segment actually starts after the window. 2281 * th->th_seq + tlen - tp->rcv_nxt - tp->rcv_wnd >= tlen 2282 * th->th_seq - tp->rcv_nxt - tp->rcv_wnd >= 0 2283 * th->th_seq >= tp->rcv_nxt + tp->rcv_wnd 2284 */ 2285 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, tlen); 2286 2287 /* 2288 * If a new connection request is received while in 2289 * TIME_WAIT, drop the old connection and start over 2290 * if the sequence numbers are above the previous 2291 * ones. 2292 * 2293 * NOTE: We need to put the header fields back into 2294 * network order. 2295 */ 2296 if ((tiflags & TH_SYN) && 2297 tp->t_state == TCPS_TIME_WAIT && 2298 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 2299 tp = tcp_close(tp); 2300 tcp_fields_to_net(th); 2301 m_freem(tcp_saveti); 2302 tcp_saveti = NULL; 2303 goto findpcb; 2304 } 2305 2306 /* 2307 * If window is closed can only take segments at 2308 * window edge, and have to drop data and PUSH from 2309 * incoming segments. Continue processing, but 2310 * remember to ack. Otherwise, drop segment 2311 * and (if not RST) ack. 2312 */ 2313 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2314 KASSERT(todrop == tlen); 2315 tp->t_flags |= TF_ACKNOW; 2316 TCP_STATINC(TCP_STAT_RCVWINPROBE); 2317 } else { 2318 goto dropafterack; 2319 } 2320 } else { 2321 TCP_STATADD(TCP_STAT_RCVBYTEAFTERWIN, todrop); 2322 } 2323 m_adj(m, -todrop); 2324 tlen -= todrop; 2325 tiflags &= ~(TH_PUSH|TH_FIN); 2326 } 2327 2328 /* 2329 * If last ACK falls within this segment's sequence numbers, 2330 * record the timestamp. 2331 * NOTE: 2332 * 1) That the test incorporates suggestions from the latest 2333 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2334 * 2) That updating only on newer timestamps interferes with 2335 * our earlier PAWS tests, so this check should be solely 2336 * predicated on the sequence space of this segment. 2337 * 3) That we modify the segment boundary check to be 2338 * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2339 * instead of RFC1323's 2340 * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2341 * This modified check allows us to overcome RFC1323's 2342 * limitations as described in Stevens TCP/IP Illustrated 2343 * Vol. 2 p.869. In such cases, we can still calculate the 2344 * RTT correctly when RCV.NXT == Last.ACK.Sent. 2345 */ 2346 if (opti.ts_present && 2347 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2348 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2349 ((tiflags & (TH_SYN|TH_FIN)) != 0))) { 2350 tp->ts_recent_age = tcp_now; 2351 tp->ts_recent = opti.ts_val; 2352 } 2353 2354 /* 2355 * If the RST bit is set examine the state: 2356 * RECEIVED state: 2357 * If passive open, return to LISTEN state. 2358 * If active open, inform user that connection was refused. 2359 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT states: 2360 * Inform user that connection was reset, and close tcb. 2361 * CLOSING, LAST_ACK, TIME_WAIT states: 2362 * Close the tcb. 2363 */ 2364 if (tiflags & TH_RST) { 2365 if (th->th_seq != tp->rcv_nxt) 2366 goto dropafterack_ratelim; 2367 2368 switch (tp->t_state) { 2369 case TCPS_SYN_RECEIVED: 2370 so->so_error = ECONNREFUSED; 2371 goto close; 2372 2373 case TCPS_ESTABLISHED: 2374 case TCPS_FIN_WAIT_1: 2375 case TCPS_FIN_WAIT_2: 2376 case TCPS_CLOSE_WAIT: 2377 so->so_error = ECONNRESET; 2378 close: 2379 tp->t_state = TCPS_CLOSED; 2380 TCP_STATINC(TCP_STAT_DROPS); 2381 tp = tcp_close(tp); 2382 goto drop; 2383 2384 case TCPS_CLOSING: 2385 case TCPS_LAST_ACK: 2386 case TCPS_TIME_WAIT: 2387 tp = tcp_close(tp); 2388 goto drop; 2389 } 2390 } 2391 2392 /* 2393 * Since we've covered the SYN-SENT and SYN-RECEIVED states above 2394 * we must be in a synchronized state. RFC793 states (under Reset 2395 * Generation) that any unacceptable segment (an out-of-order SYN 2396 * qualifies) received in a synchronized state must elicit only an 2397 * empty acknowledgment segment ... and the connection remains in 2398 * the same state. 2399 */ 2400 if (tiflags & TH_SYN) { 2401 if (tp->rcv_nxt == th->th_seq) { 2402 tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack - 1, 2403 TH_ACK); 2404 if (tcp_saveti) 2405 m_freem(tcp_saveti); 2406 return; 2407 } 2408 2409 goto dropafterack_ratelim; 2410 } 2411 2412 /* 2413 * If the ACK bit is off we drop the segment and return. 2414 */ 2415 if ((tiflags & TH_ACK) == 0) { 2416 if (tp->t_flags & TF_ACKNOW) 2417 goto dropafterack; 2418 goto drop; 2419 } 2420 2421 /* 2422 * From here on, we're doing ACK processing. 2423 */ 2424 2425 switch (tp->t_state) { 2426 /* 2427 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 2428 * ESTABLISHED state and continue processing, otherwise 2429 * send an RST. 2430 */ 2431 case TCPS_SYN_RECEIVED: 2432 if (SEQ_GT(tp->snd_una, th->th_ack) || 2433 SEQ_GT(th->th_ack, tp->snd_max)) 2434 goto dropwithreset; 2435 TCP_STATINC(TCP_STAT_CONNECTS); 2436 soisconnected(so); 2437 tcp_established(tp); 2438 /* Do window scaling? */ 2439 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2440 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2441 tp->snd_scale = tp->requested_s_scale; 2442 tp->rcv_scale = tp->request_r_scale; 2443 } 2444 TCP_REASS_LOCK(tp); 2445 (void)tcp_reass(tp, NULL, NULL, tlen); 2446 tp->snd_wl1 = th->th_seq - 1; 2447 /* FALLTHROUGH */ 2448 2449 /* 2450 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2451 * ACKs. If the ack is in the range 2452 * tp->snd_una < th->th_ack <= tp->snd_max 2453 * then advance tp->snd_una to th->th_ack and drop 2454 * data from the retransmission queue. If this ACK reflects 2455 * more up to date window information we update our window information. 2456 */ 2457 case TCPS_ESTABLISHED: 2458 case TCPS_FIN_WAIT_1: 2459 case TCPS_FIN_WAIT_2: 2460 case TCPS_CLOSE_WAIT: 2461 case TCPS_CLOSING: 2462 case TCPS_LAST_ACK: 2463 case TCPS_TIME_WAIT: 2464 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2465 if (tlen == 0 && !dupseg && tiwin == tp->snd_wnd) { 2466 TCP_STATINC(TCP_STAT_RCVDUPACK); 2467 /* 2468 * If we have outstanding data (other than 2469 * a window probe), this is a completely 2470 * duplicate ack (ie, window info didn't 2471 * change), the ack is the biggest we've 2472 * seen and we've seen exactly our rexmt 2473 * threshold of them, assume a packet 2474 * has been dropped and retransmit it. 2475 * Kludge snd_nxt & the congestion 2476 * window so we send only this one 2477 * packet. 2478 */ 2479 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 || 2480 th->th_ack != tp->snd_una) 2481 tp->t_dupacks = 0; 2482 else if (tp->t_partialacks < 0 && 2483 (++tp->t_dupacks == tcprexmtthresh || 2484 TCP_FACK_FASTRECOV(tp))) { 2485 /* 2486 * Do the fast retransmit, and adjust 2487 * congestion control parameters. 2488 */ 2489 if (tp->t_congctl->fast_retransmit(tp, th)) { 2490 /* False fast retransmit */ 2491 break; 2492 } 2493 goto drop; 2494 } else if (tp->t_dupacks > tcprexmtthresh) { 2495 tp->snd_cwnd += tp->t_segsz; 2496 KERNEL_LOCK(1, NULL); 2497 (void)tcp_output(tp); 2498 KERNEL_UNLOCK_ONE(NULL); 2499 goto drop; 2500 } 2501 } else { 2502 /* 2503 * If the ack appears to be very old, only 2504 * allow data that is in-sequence. This 2505 * makes it somewhat more difficult to insert 2506 * forged data by guessing sequence numbers. 2507 * Sent an ack to try to update the send 2508 * sequence number on the other side. 2509 */ 2510 if (tlen && th->th_seq != tp->rcv_nxt && 2511 SEQ_LT(th->th_ack, 2512 tp->snd_una - tp->max_sndwnd)) 2513 goto dropafterack; 2514 } 2515 break; 2516 } 2517 /* 2518 * If the congestion window was inflated to account 2519 * for the other side's cached packets, retract it. 2520 */ 2521 tp->t_congctl->fast_retransmit_newack(tp, th); 2522 2523 if (SEQ_GT(th->th_ack, tp->snd_max)) { 2524 TCP_STATINC(TCP_STAT_RCVACKTOOMUCH); 2525 goto dropafterack; 2526 } 2527 acked = th->th_ack - tp->snd_una; 2528 tcps = TCP_STAT_GETREF(); 2529 _NET_STATINC_REF(tcps, TCP_STAT_RCVACKPACK); 2530 _NET_STATADD_REF(tcps, TCP_STAT_RCVACKBYTE, acked); 2531 TCP_STAT_PUTREF(); 2532 2533 /* 2534 * If we have a timestamp reply, update smoothed 2535 * round trip time. If no timestamp is present but 2536 * transmit timer is running and timed sequence 2537 * number was acked, update smoothed round trip time. 2538 * Since we now have an rtt measurement, cancel the 2539 * timer backoff (cf., Phil Karn's retransmit alg.). 2540 * Recompute the initial retransmit timer. 2541 */ 2542 if (ts_rtt) 2543 tcp_xmit_timer(tp, ts_rtt - 1); 2544 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 2545 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 2546 2547 /* 2548 * If all outstanding data is acked, stop retransmit 2549 * timer and remember to restart (more output or persist). 2550 * If there is more data to be acked, restart retransmit 2551 * timer, using current (possibly backed-off) value. 2552 */ 2553 if (th->th_ack == tp->snd_max) { 2554 TCP_TIMER_DISARM(tp, TCPT_REXMT); 2555 needoutput = 1; 2556 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 2557 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 2558 2559 /* 2560 * New data has been acked, adjust the congestion window. 2561 */ 2562 tp->t_congctl->newack(tp, th); 2563 2564 nd_hint(tp); 2565 if (acked > so->so_snd.sb_cc) { 2566 tp->snd_wnd -= so->so_snd.sb_cc; 2567 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 2568 ourfinisacked = 1; 2569 } else { 2570 if (acked > (tp->t_lastoff - tp->t_inoff)) 2571 tp->t_lastm = NULL; 2572 sbdrop(&so->so_snd, acked); 2573 tp->t_lastoff -= acked; 2574 if (tp->snd_wnd > acked) 2575 tp->snd_wnd -= acked; 2576 else 2577 tp->snd_wnd = 0; 2578 ourfinisacked = 0; 2579 } 2580 sowwakeup(so); 2581 2582 icmp_check(tp, th, acked); 2583 2584 tp->snd_una = th->th_ack; 2585 if (SEQ_GT(tp->snd_una, tp->snd_fack)) 2586 tp->snd_fack = tp->snd_una; 2587 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2588 tp->snd_nxt = tp->snd_una; 2589 if (SEQ_LT(tp->snd_high, tp->snd_una)) 2590 tp->snd_high = tp->snd_una; 2591 2592 switch (tp->t_state) { 2593 2594 /* 2595 * In FIN_WAIT_1 STATE in addition to the processing 2596 * for the ESTABLISHED state if our FIN is now acknowledged 2597 * then enter FIN_WAIT_2. 2598 */ 2599 case TCPS_FIN_WAIT_1: 2600 if (ourfinisacked) { 2601 /* 2602 * If we can't receive any more 2603 * data, then closing user can proceed. 2604 * Starting the timer is contrary to the 2605 * specification, but if we don't get a FIN 2606 * we'll hang forever. 2607 */ 2608 if (so->so_state & SS_CANTRCVMORE) { 2609 soisdisconnected(so); 2610 if (tp->t_maxidle > 0) 2611 TCP_TIMER_ARM(tp, TCPT_2MSL, 2612 tp->t_maxidle); 2613 } 2614 tp->t_state = TCPS_FIN_WAIT_2; 2615 } 2616 break; 2617 2618 /* 2619 * In CLOSING STATE in addition to the processing for 2620 * the ESTABLISHED state if the ACK acknowledges our FIN 2621 * then enter the TIME-WAIT state, otherwise ignore 2622 * the segment. 2623 */ 2624 case TCPS_CLOSING: 2625 if (ourfinisacked) { 2626 tp->t_state = TCPS_TIME_WAIT; 2627 tcp_canceltimers(tp); 2628 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl); 2629 soisdisconnected(so); 2630 } 2631 break; 2632 2633 /* 2634 * In LAST_ACK, we may still be waiting for data to drain 2635 * and/or to be acked, as well as for the ack of our FIN. 2636 * If our FIN is now acknowledged, delete the TCB, 2637 * enter the closed state and return. 2638 */ 2639 case TCPS_LAST_ACK: 2640 if (ourfinisacked) { 2641 tp = tcp_close(tp); 2642 goto drop; 2643 } 2644 break; 2645 2646 /* 2647 * In TIME_WAIT state the only thing that should arrive 2648 * is a retransmission of the remote FIN. Acknowledge 2649 * it and restart the finack timer. 2650 */ 2651 case TCPS_TIME_WAIT: 2652 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl); 2653 goto dropafterack; 2654 } 2655 } 2656 2657 step6: 2658 /* 2659 * Update window information. 2660 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2661 */ 2662 if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) || 2663 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2664 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2665 /* keep track of pure window updates */ 2666 if (tlen == 0 && 2667 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2668 TCP_STATINC(TCP_STAT_RCVWINUPD); 2669 tp->snd_wnd = tiwin; 2670 tp->snd_wl1 = th->th_seq; 2671 tp->snd_wl2 = th->th_ack; 2672 if (tp->snd_wnd > tp->max_sndwnd) 2673 tp->max_sndwnd = tp->snd_wnd; 2674 needoutput = 1; 2675 } 2676 2677 /* 2678 * Process segments with URG. 2679 */ 2680 if ((tiflags & TH_URG) && th->th_urp && 2681 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2682 /* 2683 * This is a kludge, but if we receive and accept 2684 * random urgent pointers, we'll crash in 2685 * soreceive. It's hard to imagine someone 2686 * actually wanting to send this much urgent data. 2687 */ 2688 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2689 th->th_urp = 0; /* XXX */ 2690 tiflags &= ~TH_URG; /* XXX */ 2691 goto dodata; /* XXX */ 2692 } 2693 2694 /* 2695 * If this segment advances the known urgent pointer, 2696 * then mark the data stream. This should not happen 2697 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2698 * a FIN has been received from the remote side. 2699 * In these states we ignore the URG. 2700 * 2701 * According to RFC961 (Assigned Protocols), 2702 * the urgent pointer points to the last octet 2703 * of urgent data. We continue, however, 2704 * to consider it to indicate the first octet 2705 * of data past the urgent section as the original 2706 * spec states (in one of two places). 2707 */ 2708 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2709 tp->rcv_up = th->th_seq + th->th_urp; 2710 so->so_oobmark = so->so_rcv.sb_cc + 2711 (tp->rcv_up - tp->rcv_nxt) - 1; 2712 if (so->so_oobmark == 0) 2713 so->so_state |= SS_RCVATMARK; 2714 sohasoutofband(so); 2715 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2716 } 2717 2718 /* 2719 * Remove out of band data so doesn't get presented to user. 2720 * This can happen independent of advancing the URG pointer, 2721 * but if two URG's are pending at once, some out-of-band 2722 * data may creep in... ick. 2723 */ 2724 if (th->th_urp <= (u_int16_t)tlen && 2725 (so->so_options & SO_OOBINLINE) == 0) 2726 tcp_pulloutofband(so, th, m, hdroptlen); 2727 } else { 2728 /* 2729 * If no out of band data is expected, 2730 * pull receive urgent pointer along 2731 * with the receive window. 2732 */ 2733 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2734 tp->rcv_up = tp->rcv_nxt; 2735 } 2736 dodata: 2737 2738 /* 2739 * Process the segment text, merging it into the TCP sequencing queue, 2740 * and arranging for acknowledgement of receipt if necessary. 2741 * This process logically involves adjusting tp->rcv_wnd as data 2742 * is presented to the user (this happens in tcp_usrreq.c, 2743 * tcp_rcvd()). If a FIN has already been received on this 2744 * connection then we just ignore the text. 2745 */ 2746 if ((tlen || (tiflags & TH_FIN)) && 2747 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2748 /* 2749 * Handle the common case: 2750 * o Segment is the next to be received, and 2751 * o The queue is empty, and 2752 * o The connection is established 2753 * In this case, we avoid calling tcp_reass. 2754 * 2755 * tcp_setup_ack: set DELACK for segments received in order, 2756 * but ack immediately when segments are out of order (so that 2757 * fast retransmit can work). 2758 */ 2759 TCP_REASS_LOCK(tp); 2760 if (th->th_seq == tp->rcv_nxt && 2761 TAILQ_FIRST(&tp->segq) == NULL && 2762 tp->t_state == TCPS_ESTABLISHED) { 2763 tcp_setup_ack(tp, th); 2764 tp->rcv_nxt += tlen; 2765 tiflags = th->th_flags & TH_FIN; 2766 tcps = TCP_STAT_GETREF(); 2767 _NET_STATINC_REF(tcps, TCP_STAT_RCVPACK); 2768 _NET_STATADD_REF(tcps, TCP_STAT_RCVBYTE, tlen); 2769 TCP_STAT_PUTREF(); 2770 nd_hint(tp); 2771 if (so->so_state & SS_CANTRCVMORE) { 2772 m_freem(m); 2773 } else { 2774 m_adj(m, hdroptlen); 2775 sbappendstream(&(so)->so_rcv, m); 2776 } 2777 TCP_REASS_UNLOCK(tp); 2778 sorwakeup(so); 2779 } else { 2780 m_adj(m, hdroptlen); 2781 tiflags = tcp_reass(tp, th, m, tlen); 2782 tp->t_flags |= TF_ACKNOW; 2783 } 2784 2785 /* 2786 * Note the amount of data that peer has sent into 2787 * our window, in order to estimate the sender's 2788 * buffer size. 2789 */ 2790 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2791 } else { 2792 m_freem(m); 2793 m = NULL; 2794 tiflags &= ~TH_FIN; 2795 } 2796 2797 /* 2798 * If FIN is received ACK the FIN and let the user know 2799 * that the connection is closing. Ignore a FIN received before 2800 * the connection is fully established. 2801 */ 2802 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) { 2803 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2804 socantrcvmore(so); 2805 tp->t_flags |= TF_ACKNOW; 2806 tp->rcv_nxt++; 2807 } 2808 switch (tp->t_state) { 2809 2810 /* 2811 * In ESTABLISHED STATE enter the CLOSE_WAIT state. 2812 */ 2813 case TCPS_ESTABLISHED: 2814 tp->t_state = TCPS_CLOSE_WAIT; 2815 break; 2816 2817 /* 2818 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2819 * enter the CLOSING state. 2820 */ 2821 case TCPS_FIN_WAIT_1: 2822 tp->t_state = TCPS_CLOSING; 2823 break; 2824 2825 /* 2826 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2827 * starting the time-wait timer, turning off the other 2828 * standard timers. 2829 */ 2830 case TCPS_FIN_WAIT_2: 2831 tp->t_state = TCPS_TIME_WAIT; 2832 tcp_canceltimers(tp); 2833 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl); 2834 soisdisconnected(so); 2835 break; 2836 2837 /* 2838 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2839 */ 2840 case TCPS_TIME_WAIT: 2841 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * tp->t_msl); 2842 break; 2843 } 2844 } 2845 #ifdef TCP_DEBUG 2846 if (so->so_options & SO_DEBUG) 2847 tcp_trace(TA_INPUT, ostate, tp, tcp_saveti, 0); 2848 #endif 2849 2850 /* 2851 * Return any desired output. 2852 */ 2853 if (needoutput || (tp->t_flags & TF_ACKNOW)) { 2854 KERNEL_LOCK(1, NULL); 2855 (void)tcp_output(tp); 2856 KERNEL_UNLOCK_ONE(NULL); 2857 } 2858 if (tcp_saveti) 2859 m_freem(tcp_saveti); 2860 2861 if (tp->t_state == TCPS_TIME_WAIT 2862 && (so->so_state & SS_NOFDREF) 2863 && (tp->t_inpcb || af != AF_INET || af != AF_INET6) 2864 && ((af == AF_INET ? tcp4_vtw_enable : tcp6_vtw_enable) & 1) != 0 2865 && TAILQ_EMPTY(&tp->segq) 2866 && vtw_add(af, tp)) { 2867 ; 2868 } 2869 return; 2870 2871 badsyn: 2872 /* 2873 * Received a bad SYN. Increment counters and dropwithreset. 2874 */ 2875 TCP_STATINC(TCP_STAT_BADSYN); 2876 tp = NULL; 2877 goto dropwithreset; 2878 2879 dropafterack: 2880 /* 2881 * Generate an ACK dropping incoming segment if it occupies 2882 * sequence space, where the ACK reflects our state. 2883 */ 2884 if (tiflags & TH_RST) 2885 goto drop; 2886 goto dropafterack2; 2887 2888 dropafterack_ratelim: 2889 /* 2890 * We may want to rate-limit ACKs against SYN/RST attack. 2891 */ 2892 if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count, 2893 tcp_ackdrop_ppslim) == 0) { 2894 /* XXX stat */ 2895 goto drop; 2896 } 2897 2898 dropafterack2: 2899 m_freem(m); 2900 tp->t_flags |= TF_ACKNOW; 2901 KERNEL_LOCK(1, NULL); 2902 (void)tcp_output(tp); 2903 KERNEL_UNLOCK_ONE(NULL); 2904 if (tcp_saveti) 2905 m_freem(tcp_saveti); 2906 return; 2907 2908 dropwithreset_ratelim: 2909 /* 2910 * We may want to rate-limit RSTs in certain situations, 2911 * particularly if we are sending an RST in response to 2912 * an attempt to connect to or otherwise communicate with 2913 * a port for which we have no socket. 2914 */ 2915 if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count, 2916 tcp_rst_ppslim) == 0) { 2917 /* XXX stat */ 2918 goto drop; 2919 } 2920 2921 dropwithreset: 2922 /* 2923 * Generate a RST, dropping incoming segment. 2924 * Make ACK acceptable to originator of segment. 2925 */ 2926 if (tiflags & TH_RST) 2927 goto drop; 2928 if (tiflags & TH_ACK) { 2929 (void)tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack, TH_RST); 2930 } else { 2931 if (tiflags & TH_SYN) 2932 tlen++; 2933 (void)tcp_respond(tp, m, m, th, th->th_seq + tlen, (tcp_seq)0, 2934 TH_RST|TH_ACK); 2935 } 2936 if (tcp_saveti) 2937 m_freem(tcp_saveti); 2938 return; 2939 2940 badcsum: 2941 drop: 2942 /* 2943 * Drop space held by incoming segment and return. 2944 */ 2945 if (tp) { 2946 so = tp->t_inpcb->inp_socket; 2947 #ifdef TCP_DEBUG 2948 if (so && (so->so_options & SO_DEBUG) != 0) 2949 tcp_trace(TA_DROP, ostate, tp, tcp_saveti, 0); 2950 #endif 2951 } 2952 if (tcp_saveti) 2953 m_freem(tcp_saveti); 2954 m_freem(m); 2955 return; 2956 } 2957 2958 #ifdef TCP_SIGNATURE 2959 int 2960 tcp_signature_apply(void *fstate, void *data, u_int len) 2961 { 2962 2963 MD5Update(fstate, (u_char *)data, len); 2964 return (0); 2965 } 2966 2967 struct secasvar * 2968 tcp_signature_getsav(struct mbuf *m) 2969 { 2970 struct ip *ip; 2971 struct ip6_hdr *ip6; 2972 2973 ip = mtod(m, struct ip *); 2974 switch (ip->ip_v) { 2975 case 4: 2976 ip = mtod(m, struct ip *); 2977 ip6 = NULL; 2978 break; 2979 case 6: 2980 ip = NULL; 2981 ip6 = mtod(m, struct ip6_hdr *); 2982 break; 2983 default: 2984 return (NULL); 2985 } 2986 2987 #ifdef IPSEC 2988 union sockaddr_union dst; 2989 2990 /* Extract the destination from the IP header in the mbuf. */ 2991 memset(&dst, 0, sizeof(union sockaddr_union)); 2992 if (ip != NULL) { 2993 dst.sa.sa_len = sizeof(struct sockaddr_in); 2994 dst.sa.sa_family = AF_INET; 2995 dst.sin.sin_addr = ip->ip_dst; 2996 } else { 2997 dst.sa.sa_len = sizeof(struct sockaddr_in6); 2998 dst.sa.sa_family = AF_INET6; 2999 dst.sin6.sin6_addr = ip6->ip6_dst; 3000 } 3001 3002 /* 3003 * Look up an SADB entry which matches the address of the peer. 3004 */ 3005 return KEY_LOOKUP_SA(&dst, IPPROTO_TCP, htonl(TCP_SIG_SPI), 0, 0); 3006 #else 3007 return NULL; 3008 #endif 3009 } 3010 3011 int 3012 tcp_signature(struct mbuf *m, struct tcphdr *th, int thoff, 3013 struct secasvar *sav, char *sig) 3014 { 3015 MD5_CTX ctx; 3016 struct ip *ip; 3017 struct ipovly *ipovly; 3018 #ifdef INET6 3019 struct ip6_hdr *ip6; 3020 struct ip6_hdr_pseudo ip6pseudo; 3021 #endif 3022 struct ippseudo ippseudo; 3023 struct tcphdr th0; 3024 int l, tcphdrlen; 3025 3026 if (sav == NULL) 3027 return (-1); 3028 3029 tcphdrlen = th->th_off * 4; 3030 3031 switch (mtod(m, struct ip *)->ip_v) { 3032 case 4: 3033 MD5Init(&ctx); 3034 ip = mtod(m, struct ip *); 3035 memset(&ippseudo, 0, sizeof(ippseudo)); 3036 ipovly = (struct ipovly *)ip; 3037 ippseudo.ippseudo_src = ipovly->ih_src; 3038 ippseudo.ippseudo_dst = ipovly->ih_dst; 3039 ippseudo.ippseudo_pad = 0; 3040 ippseudo.ippseudo_p = IPPROTO_TCP; 3041 ippseudo.ippseudo_len = htons(m->m_pkthdr.len - thoff); 3042 MD5Update(&ctx, (char *)&ippseudo, sizeof(ippseudo)); 3043 break; 3044 #if INET6 3045 case 6: 3046 MD5Init(&ctx); 3047 ip6 = mtod(m, struct ip6_hdr *); 3048 memset(&ip6pseudo, 0, sizeof(ip6pseudo)); 3049 ip6pseudo.ip6ph_src = ip6->ip6_src; 3050 in6_clearscope(&ip6pseudo.ip6ph_src); 3051 ip6pseudo.ip6ph_dst = ip6->ip6_dst; 3052 in6_clearscope(&ip6pseudo.ip6ph_dst); 3053 ip6pseudo.ip6ph_len = htons(m->m_pkthdr.len - thoff); 3054 ip6pseudo.ip6ph_nxt = IPPROTO_TCP; 3055 MD5Update(&ctx, (char *)&ip6pseudo, sizeof(ip6pseudo)); 3056 break; 3057 #endif 3058 default: 3059 return (-1); 3060 } 3061 3062 th0 = *th; 3063 th0.th_sum = 0; 3064 MD5Update(&ctx, (char *)&th0, sizeof(th0)); 3065 3066 l = m->m_pkthdr.len - thoff - tcphdrlen; 3067 if (l > 0) 3068 m_apply(m, thoff + tcphdrlen, 3069 m->m_pkthdr.len - thoff - tcphdrlen, 3070 tcp_signature_apply, &ctx); 3071 3072 MD5Update(&ctx, _KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth)); 3073 MD5Final(sig, &ctx); 3074 3075 return (0); 3076 } 3077 #endif 3078 3079 /* 3080 * Parse and process tcp options. 3081 * 3082 * Returns -1 if this segment should be dropped. (eg. wrong signature) 3083 * Otherwise returns 0. 3084 */ 3085 int 3086 tcp_dooptions(struct tcpcb *tp, const u_char *cp, int cnt, struct tcphdr *th, 3087 struct mbuf *m, int toff, struct tcp_opt_info *oi) 3088 { 3089 u_int16_t mss; 3090 int opt, optlen = 0; 3091 #ifdef TCP_SIGNATURE 3092 void *sigp = NULL; 3093 char sigbuf[TCP_SIGLEN]; 3094 struct secasvar *sav = NULL; 3095 #endif 3096 3097 for (; cp && cnt > 0; cnt -= optlen, cp += optlen) { 3098 opt = cp[0]; 3099 if (opt == TCPOPT_EOL) 3100 break; 3101 if (opt == TCPOPT_NOP) 3102 optlen = 1; 3103 else { 3104 if (cnt < 2) 3105 break; 3106 optlen = cp[1]; 3107 if (optlen < 2 || optlen > cnt) 3108 break; 3109 } 3110 switch (opt) { 3111 3112 default: 3113 continue; 3114 3115 case TCPOPT_MAXSEG: 3116 if (optlen != TCPOLEN_MAXSEG) 3117 continue; 3118 if (!(th->th_flags & TH_SYN)) 3119 continue; 3120 if (TCPS_HAVERCVDSYN(tp->t_state)) 3121 continue; 3122 memcpy(&mss, cp + 2, sizeof(mss)); 3123 oi->maxseg = ntohs(mss); 3124 break; 3125 3126 case TCPOPT_WINDOW: 3127 if (optlen != TCPOLEN_WINDOW) 3128 continue; 3129 if (!(th->th_flags & TH_SYN)) 3130 continue; 3131 if (TCPS_HAVERCVDSYN(tp->t_state)) 3132 continue; 3133 tp->t_flags |= TF_RCVD_SCALE; 3134 tp->requested_s_scale = cp[2]; 3135 if (tp->requested_s_scale > TCP_MAX_WINSHIFT) { 3136 char buf[INET6_ADDRSTRLEN]; 3137 struct ip *ip = mtod(m, struct ip *); 3138 #ifdef INET6 3139 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 3140 #endif 3141 3142 switch (ip->ip_v) { 3143 case 4: 3144 in_print(buf, sizeof(buf), 3145 &ip->ip_src); 3146 break; 3147 #ifdef INET6 3148 case 6: 3149 in6_print(buf, sizeof(buf), 3150 &ip6->ip6_src); 3151 break; 3152 #endif 3153 default: 3154 strlcpy(buf, "(unknown)", sizeof(buf)); 3155 break; 3156 } 3157 3158 log(LOG_ERR, "TCP: invalid wscale %d from %s, " 3159 "assuming %d\n", 3160 tp->requested_s_scale, buf, 3161 TCP_MAX_WINSHIFT); 3162 tp->requested_s_scale = TCP_MAX_WINSHIFT; 3163 } 3164 break; 3165 3166 case TCPOPT_TIMESTAMP: 3167 if (optlen != TCPOLEN_TIMESTAMP) 3168 continue; 3169 oi->ts_present = 1; 3170 memcpy(&oi->ts_val, cp + 2, sizeof(oi->ts_val)); 3171 NTOHL(oi->ts_val); 3172 memcpy(&oi->ts_ecr, cp + 6, sizeof(oi->ts_ecr)); 3173 NTOHL(oi->ts_ecr); 3174 3175 if (!(th->th_flags & TH_SYN)) 3176 continue; 3177 if (TCPS_HAVERCVDSYN(tp->t_state)) 3178 continue; 3179 /* 3180 * A timestamp received in a SYN makes 3181 * it ok to send timestamp requests and replies. 3182 */ 3183 tp->t_flags |= TF_RCVD_TSTMP; 3184 tp->ts_recent = oi->ts_val; 3185 tp->ts_recent_age = tcp_now; 3186 break; 3187 3188 case TCPOPT_SACK_PERMITTED: 3189 if (optlen != TCPOLEN_SACK_PERMITTED) 3190 continue; 3191 if (!(th->th_flags & TH_SYN)) 3192 continue; 3193 if (TCPS_HAVERCVDSYN(tp->t_state)) 3194 continue; 3195 if (tcp_do_sack) { 3196 tp->t_flags |= TF_SACK_PERMIT; 3197 tp->t_flags |= TF_WILL_SACK; 3198 } 3199 break; 3200 3201 case TCPOPT_SACK: 3202 tcp_sack_option(tp, th, cp, optlen); 3203 break; 3204 #ifdef TCP_SIGNATURE 3205 case TCPOPT_SIGNATURE: 3206 if (optlen != TCPOLEN_SIGNATURE) 3207 continue; 3208 if (sigp && 3209 !consttime_memequal(sigp, cp + 2, TCP_SIGLEN)) 3210 return (-1); 3211 3212 sigp = sigbuf; 3213 memcpy(sigbuf, cp + 2, TCP_SIGLEN); 3214 tp->t_flags |= TF_SIGNATURE; 3215 break; 3216 #endif 3217 } 3218 } 3219 3220 #ifndef TCP_SIGNATURE 3221 return 0; 3222 #else 3223 if (tp->t_flags & TF_SIGNATURE) { 3224 sav = tcp_signature_getsav(m); 3225 if (sav == NULL && tp->t_state == TCPS_LISTEN) 3226 return (-1); 3227 } 3228 3229 if ((sigp ? TF_SIGNATURE : 0) ^ (tp->t_flags & TF_SIGNATURE)) 3230 goto out; 3231 3232 if (sigp) { 3233 char sig[TCP_SIGLEN]; 3234 3235 tcp_fields_to_net(th); 3236 if (tcp_signature(m, th, toff, sav, sig) < 0) { 3237 tcp_fields_to_host(th); 3238 goto out; 3239 } 3240 tcp_fields_to_host(th); 3241 3242 if (!consttime_memequal(sig, sigp, TCP_SIGLEN)) { 3243 TCP_STATINC(TCP_STAT_BADSIG); 3244 goto out; 3245 } else 3246 TCP_STATINC(TCP_STAT_GOODSIG); 3247 3248 key_sa_recordxfer(sav, m); 3249 KEY_SA_UNREF(&sav); 3250 } 3251 return 0; 3252 out: 3253 if (sav != NULL) 3254 KEY_SA_UNREF(&sav); 3255 return -1; 3256 #endif 3257 } 3258 3259 /* 3260 * Pull out of band byte out of a segment so 3261 * it doesn't appear in the user's data queue. 3262 * It is still reflected in the segment length for 3263 * sequencing purposes. 3264 */ 3265 void 3266 tcp_pulloutofband(struct socket *so, struct tcphdr *th, 3267 struct mbuf *m, int off) 3268 { 3269 int cnt = off + th->th_urp - 1; 3270 3271 while (cnt >= 0) { 3272 if (m->m_len > cnt) { 3273 char *cp = mtod(m, char *) + cnt; 3274 struct tcpcb *tp = sototcpcb(so); 3275 3276 tp->t_iobc = *cp; 3277 tp->t_oobflags |= TCPOOB_HAVEDATA; 3278 memmove(cp, cp + 1, (unsigned)(m->m_len - cnt - 1)); 3279 m->m_len--; 3280 return; 3281 } 3282 cnt -= m->m_len; 3283 m = m->m_next; 3284 if (m == NULL) 3285 break; 3286 } 3287 panic("tcp_pulloutofband"); 3288 } 3289 3290 /* 3291 * Collect new round-trip time estimate 3292 * and update averages and current timeout. 3293 * 3294 * rtt is in units of slow ticks (typically 500 ms) -- essentially the 3295 * difference of two timestamps. 3296 */ 3297 void 3298 tcp_xmit_timer(struct tcpcb *tp, uint32_t rtt) 3299 { 3300 int32_t delta; 3301 3302 TCP_STATINC(TCP_STAT_RTTUPDATED); 3303 if (tp->t_srtt != 0) { 3304 /* 3305 * Compute the amount to add to srtt for smoothing, 3306 * *alpha, or 2^(-TCP_RTT_SHIFT). Because 3307 * srtt is stored in 1/32 slow ticks, we conceptually 3308 * shift left 5 bits, subtract srtt to get the 3309 * difference, and then shift right by TCP_RTT_SHIFT 3310 * (3) to obtain 1/8 of the difference. 3311 */ 3312 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT); 3313 /* 3314 * This can never happen, because delta's lowest 3315 * possible value is 1/8 of t_srtt. But if it does, 3316 * set srtt to some reasonable value, here chosen 3317 * as 1/8 tick. 3318 */ 3319 if ((tp->t_srtt += delta) <= 0) 3320 tp->t_srtt = 1 << 2; 3321 /* 3322 * RFC2988 requires that rttvar be updated first. 3323 * This code is compliant because "delta" is the old 3324 * srtt minus the new observation (scaled). 3325 * 3326 * RFC2988 says: 3327 * rttvar = (1-beta) * rttvar + beta * |srtt-observed| 3328 * 3329 * delta is in units of 1/32 ticks, and has then been 3330 * divided by 8. This is equivalent to being in 1/16s 3331 * units and divided by 4. Subtract from it 1/4 of 3332 * the existing rttvar to form the (signed) amount to 3333 * adjust. 3334 */ 3335 if (delta < 0) 3336 delta = -delta; 3337 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 3338 /* 3339 * As with srtt, this should never happen. There is 3340 * no support in RFC2988 for this operation. But 1/4s 3341 * as rttvar when faced with something arguably wrong 3342 * is ok. 3343 */ 3344 if ((tp->t_rttvar += delta) <= 0) 3345 tp->t_rttvar = 1 << 2; 3346 3347 /* 3348 * If srtt exceeds .01 second, ensure we use the 'remote' MSL 3349 * Problem is: it doesn't work. Disabled by defaulting 3350 * tcp_rttlocal to 0; see corresponding code in 3351 * tcp_subr that selects local vs remote in a different way. 3352 * 3353 * The static branch prediction hint here should be removed 3354 * when the rtt estimator is fixed and the rtt_enable code 3355 * is turned back on. 3356 */ 3357 if (__predict_false(tcp_rttlocal) && tcp_msl_enable 3358 && tp->t_srtt > tcp_msl_remote_threshold 3359 && tp->t_msl < tcp_msl_remote) { 3360 tp->t_msl = MIN(tcp_msl_remote, TCP_MAXMSL); 3361 } 3362 } else { 3363 /* 3364 * This is the first measurement. Per RFC2988, 2.2, 3365 * set rtt=R and srtt=R/2. 3366 * For srtt, storage representation is 1/32 ticks, 3367 * so shift left by 5. 3368 * For rttvar, storage representation is 1/16 ticks, 3369 * So shift left by 4, but then right by 1 to halve. 3370 */ 3371 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2); 3372 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1); 3373 } 3374 tp->t_rtttime = 0; 3375 tp->t_rxtshift = 0; 3376 3377 /* 3378 * the retransmit should happen at rtt + 4 * rttvar. 3379 * Because of the way we do the smoothing, srtt and rttvar 3380 * will each average +1/2 tick of bias. When we compute 3381 * the retransmit timer, we want 1/2 tick of rounding and 3382 * 1 extra tick because of +-1/2 tick uncertainty in the 3383 * firing of the timer. The bias will give us exactly the 3384 * 1.5 tick we need. But, because the bias is 3385 * statistical, we have to test that we don't drop below 3386 * the minimum feasible timer (which is 2 ticks). 3387 */ 3388 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 3389 uimax(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3390 3391 /* 3392 * We received an ack for a packet that wasn't retransmitted; 3393 * it is probably safe to discard any error indications we've 3394 * received recently. This isn't quite right, but close enough 3395 * for now (a route might have failed after we sent a segment, 3396 * and the return path might not be symmetrical). 3397 */ 3398 tp->t_softerror = 0; 3399 } 3400