1 /* $NetBSD: tcp_input.c,v 1.231 2005/07/19 17:00:02 christos 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 The NetBSD Foundation, Inc. 74 * All rights reserved. 75 * 76 * This code is derived from software contributed to The NetBSD Foundation 77 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation 78 * Facility, NASA Ames Research Center. 79 * This code is derived from software contributed to The NetBSD Foundation 80 * by Charles M. Hannum. 81 * 82 * Redistribution and use in source and binary forms, with or without 83 * modification, are permitted provided that the following conditions 84 * are met: 85 * 1. Redistributions of source code must retain the above copyright 86 * notice, this list of conditions and the following disclaimer. 87 * 2. Redistributions in binary form must reproduce the above copyright 88 * notice, this list of conditions and the following disclaimer in the 89 * documentation and/or other materials provided with the distribution. 90 * 3. All advertising materials mentioning features or use of this software 91 * must display the following acknowledgement: 92 * This product includes software developed by the NetBSD 93 * Foundation, Inc. and its contributors. 94 * 4. Neither the name of The NetBSD Foundation nor the names of its 95 * contributors may be used to endorse or promote products derived 96 * from this software without specific prior written permission. 97 * 98 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 99 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 100 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 101 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 102 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 103 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 104 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 105 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 106 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 107 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 108 * POSSIBILITY OF SUCH DAMAGE. 109 */ 110 111 /* 112 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 113 * The Regents of the University of California. All rights reserved. 114 * 115 * Redistribution and use in source and binary forms, with or without 116 * modification, are permitted provided that the following conditions 117 * are met: 118 * 1. Redistributions of source code must retain the above copyright 119 * notice, this list of conditions and the following disclaimer. 120 * 2. Redistributions in binary form must reproduce the above copyright 121 * notice, this list of conditions and the following disclaimer in the 122 * documentation and/or other materials provided with the distribution. 123 * 3. Neither the name of the University nor the names of its contributors 124 * may be used to endorse or promote products derived from this software 125 * without specific prior written permission. 126 * 127 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 128 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 129 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 130 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 131 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 132 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 133 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 134 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 135 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 136 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 137 * SUCH DAMAGE. 138 * 139 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 140 */ 141 142 /* 143 * TODO list for SYN cache stuff: 144 * 145 * Find room for a "state" field, which is needed to keep a 146 * compressed state for TIME_WAIT TCBs. It's been noted already 147 * that this is fairly important for very high-volume web and 148 * mail servers, which use a large number of short-lived 149 * connections. 150 */ 151 152 #include <sys/cdefs.h> 153 __KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.231 2005/07/19 17:00:02 christos Exp $"); 154 155 #include "opt_inet.h" 156 #include "opt_ipsec.h" 157 #include "opt_inet_csum.h" 158 #include "opt_tcp_debug.h" 159 160 #include <sys/param.h> 161 #include <sys/systm.h> 162 #include <sys/malloc.h> 163 #include <sys/mbuf.h> 164 #include <sys/protosw.h> 165 #include <sys/socket.h> 166 #include <sys/socketvar.h> 167 #include <sys/errno.h> 168 #include <sys/syslog.h> 169 #include <sys/pool.h> 170 #include <sys/domain.h> 171 #include <sys/kernel.h> 172 #ifdef TCP_SIGNATURE 173 #include <sys/md5.h> 174 #endif 175 176 #include <net/if.h> 177 #include <net/route.h> 178 #include <net/if_types.h> 179 180 #include <netinet/in.h> 181 #include <netinet/in_systm.h> 182 #include <netinet/ip.h> 183 #include <netinet/in_pcb.h> 184 #include <netinet/in_var.h> 185 #include <netinet/ip_var.h> 186 187 #ifdef INET6 188 #ifndef INET 189 #include <netinet/in.h> 190 #endif 191 #include <netinet/ip6.h> 192 #include <netinet6/ip6_var.h> 193 #include <netinet6/in6_pcb.h> 194 #include <netinet6/ip6_var.h> 195 #include <netinet6/in6_var.h> 196 #include <netinet/icmp6.h> 197 #include <netinet6/nd6.h> 198 #endif 199 200 #ifndef INET6 201 /* always need ip6.h for IP6_EXTHDR_GET */ 202 #include <netinet/ip6.h> 203 #endif 204 205 #include <netinet/tcp.h> 206 #include <netinet/tcp_fsm.h> 207 #include <netinet/tcp_seq.h> 208 #include <netinet/tcp_timer.h> 209 #include <netinet/tcp_var.h> 210 #include <netinet/tcpip.h> 211 #include <netinet/tcp_debug.h> 212 213 #include <machine/stdarg.h> 214 215 #ifdef IPSEC 216 #include <netinet6/ipsec.h> 217 #include <netkey/key.h> 218 #endif /*IPSEC*/ 219 #ifdef INET6 220 #include "faith.h" 221 #if defined(NFAITH) && NFAITH > 0 222 #include <net/if_faith.h> 223 #endif 224 #endif /* IPSEC */ 225 226 #ifdef FAST_IPSEC 227 #include <netipsec/ipsec.h> 228 #include <netipsec/ipsec_var.h> /* XXX ipsecstat namespace */ 229 #include <netipsec/key.h> 230 #ifdef INET6 231 #include <netipsec/ipsec6.h> 232 #endif 233 #endif /* FAST_IPSEC*/ 234 235 int tcprexmtthresh = 3; 236 int tcp_log_refused; 237 238 static int tcp_rst_ppslim_count = 0; 239 static struct timeval tcp_rst_ppslim_last; 240 static int tcp_ackdrop_ppslim_count = 0; 241 static struct timeval tcp_ackdrop_ppslim_last; 242 243 #define TCP_PAWS_IDLE (24U * 24 * 60 * 60 * PR_SLOWHZ) 244 245 /* for modulo comparisons of timestamps */ 246 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0) 247 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0) 248 249 /* 250 * Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. 251 */ 252 #ifdef INET6 253 #define ND6_HINT(tp) \ 254 do { \ 255 if (tp && tp->t_in6pcb && tp->t_family == AF_INET6 && \ 256 tp->t_in6pcb->in6p_route.ro_rt) { \ 257 nd6_nud_hint(tp->t_in6pcb->in6p_route.ro_rt, NULL, 0); \ 258 } \ 259 } while (/*CONSTCOND*/ 0) 260 #else 261 #define ND6_HINT(tp) 262 #endif 263 264 /* 265 * Macro to compute ACK transmission behavior. Delay the ACK unless 266 * we have already delayed an ACK (must send an ACK every two segments). 267 * We also ACK immediately if we received a PUSH and the ACK-on-PUSH 268 * option is enabled. 269 */ 270 #define TCP_SETUP_ACK(tp, th) \ 271 do { \ 272 if ((tp)->t_flags & TF_DELACK || \ 273 (tcp_ack_on_push && (th)->th_flags & TH_PUSH)) \ 274 tp->t_flags |= TF_ACKNOW; \ 275 else \ 276 TCP_SET_DELACK(tp); \ 277 } while (/*CONSTCOND*/ 0) 278 279 #define ICMP_CHECK(tp, th, acked) \ 280 do { \ 281 /* \ 282 * If we had a pending ICMP message that \ 283 * refers to data that have just been \ 284 * acknowledged, disregard the recorded ICMP \ 285 * message. \ 286 */ \ 287 if (((tp)->t_flags & TF_PMTUD_PEND) && \ 288 SEQ_GT((th)->th_ack, (tp)->t_pmtud_th_seq)) \ 289 (tp)->t_flags &= ~TF_PMTUD_PEND; \ 290 \ 291 /* \ 292 * Keep track of the largest chunk of data \ 293 * acknowledged since last PMTU update \ 294 */ \ 295 if ((tp)->t_pmtud_mss_acked < (acked)) \ 296 (tp)->t_pmtud_mss_acked = (acked); \ 297 } while (/*CONSTCOND*/ 0) 298 299 /* 300 * Convert TCP protocol fields to host order for easier processing. 301 */ 302 #define TCP_FIELDS_TO_HOST(th) \ 303 do { \ 304 NTOHL((th)->th_seq); \ 305 NTOHL((th)->th_ack); \ 306 NTOHS((th)->th_win); \ 307 NTOHS((th)->th_urp); \ 308 } while (/*CONSTCOND*/ 0) 309 310 /* 311 * ... and reverse the above. 312 */ 313 #define TCP_FIELDS_TO_NET(th) \ 314 do { \ 315 HTONL((th)->th_seq); \ 316 HTONL((th)->th_ack); \ 317 HTONS((th)->th_win); \ 318 HTONS((th)->th_urp); \ 319 } while (/*CONSTCOND*/ 0) 320 321 #ifdef TCP_CSUM_COUNTERS 322 #include <sys/device.h> 323 324 extern struct evcnt tcp_hwcsum_ok; 325 extern struct evcnt tcp_hwcsum_bad; 326 extern struct evcnt tcp_hwcsum_data; 327 extern struct evcnt tcp_swcsum; 328 329 #define TCP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++ 330 331 #else 332 333 #define TCP_CSUM_COUNTER_INCR(ev) /* nothing */ 334 335 #endif /* TCP_CSUM_COUNTERS */ 336 337 #ifdef TCP_REASS_COUNTERS 338 #include <sys/device.h> 339 340 extern struct evcnt tcp_reass_; 341 extern struct evcnt tcp_reass_empty; 342 extern struct evcnt tcp_reass_iteration[8]; 343 extern struct evcnt tcp_reass_prependfirst; 344 extern struct evcnt tcp_reass_prepend; 345 extern struct evcnt tcp_reass_insert; 346 extern struct evcnt tcp_reass_inserttail; 347 extern struct evcnt tcp_reass_append; 348 extern struct evcnt tcp_reass_appendtail; 349 extern struct evcnt tcp_reass_overlaptail; 350 extern struct evcnt tcp_reass_overlapfront; 351 extern struct evcnt tcp_reass_segdup; 352 extern struct evcnt tcp_reass_fragdup; 353 354 #define TCP_REASS_COUNTER_INCR(ev) (ev)->ev_count++ 355 356 #else 357 358 #define TCP_REASS_COUNTER_INCR(ev) /* nothing */ 359 360 #endif /* TCP_REASS_COUNTERS */ 361 362 #ifdef INET 363 static void tcp4_log_refused(const struct ip *, const struct tcphdr *); 364 #endif 365 #ifdef INET6 366 static void tcp6_log_refused(const struct ip6_hdr *, const struct tcphdr *); 367 #endif 368 369 #define TRAVERSE(x) while ((x)->m_next) (x) = (x)->m_next 370 371 POOL_INIT(tcpipqent_pool, sizeof(struct ipqent), 0, 0, 0, "tcpipqepl", NULL); 372 373 struct ipqent * 374 tcpipqent_alloc() 375 { 376 struct ipqent *ipqe; 377 int s; 378 379 s = splvm(); 380 ipqe = pool_get(&tcpipqent_pool, PR_NOWAIT); 381 splx(s); 382 383 return ipqe; 384 } 385 386 void 387 tcpipqent_free(struct ipqent *ipqe) 388 { 389 int s; 390 391 s = splvm(); 392 pool_put(&tcpipqent_pool, ipqe); 393 splx(s); 394 } 395 396 int 397 tcp_reass(struct tcpcb *tp, struct tcphdr *th, struct mbuf *m, int *tlen) 398 { 399 struct ipqent *p, *q, *nq, *tiqe = NULL; 400 struct socket *so = NULL; 401 int pkt_flags; 402 tcp_seq pkt_seq; 403 unsigned pkt_len; 404 u_long rcvpartdupbyte = 0; 405 u_long rcvoobyte; 406 #ifdef TCP_REASS_COUNTERS 407 u_int count = 0; 408 #endif 409 410 if (tp->t_inpcb) 411 so = tp->t_inpcb->inp_socket; 412 #ifdef INET6 413 else if (tp->t_in6pcb) 414 so = tp->t_in6pcb->in6p_socket; 415 #endif 416 417 TCP_REASS_LOCK_CHECK(tp); 418 419 /* 420 * Call with th==0 after become established to 421 * force pre-ESTABLISHED data up to user socket. 422 */ 423 if (th == 0) 424 goto present; 425 426 rcvoobyte = *tlen; 427 /* 428 * Copy these to local variables because the tcpiphdr 429 * gets munged while we are collapsing mbufs. 430 */ 431 pkt_seq = th->th_seq; 432 pkt_len = *tlen; 433 pkt_flags = th->th_flags; 434 435 TCP_REASS_COUNTER_INCR(&tcp_reass_); 436 437 if ((p = TAILQ_LAST(&tp->segq, ipqehead)) != NULL) { 438 /* 439 * When we miss a packet, the vast majority of time we get 440 * packets that follow it in order. So optimize for that. 441 */ 442 if (pkt_seq == p->ipqe_seq + p->ipqe_len) { 443 p->ipqe_len += pkt_len; 444 p->ipqe_flags |= pkt_flags; 445 m_cat(p->ipre_mlast, m); 446 TRAVERSE(p->ipre_mlast); 447 m = NULL; 448 tiqe = p; 449 TAILQ_REMOVE(&tp->timeq, p, ipqe_timeq); 450 TCP_REASS_COUNTER_INCR(&tcp_reass_appendtail); 451 goto skip_replacement; 452 } 453 /* 454 * While we're here, if the pkt is completely beyond 455 * anything we have, just insert it at the tail. 456 */ 457 if (SEQ_GT(pkt_seq, p->ipqe_seq + p->ipqe_len)) { 458 TCP_REASS_COUNTER_INCR(&tcp_reass_inserttail); 459 goto insert_it; 460 } 461 } 462 463 q = TAILQ_FIRST(&tp->segq); 464 465 if (q != NULL) { 466 /* 467 * If this segment immediately precedes the first out-of-order 468 * block, simply slap the segment in front of it and (mostly) 469 * skip the complicated logic. 470 */ 471 if (pkt_seq + pkt_len == q->ipqe_seq) { 472 q->ipqe_seq = pkt_seq; 473 q->ipqe_len += pkt_len; 474 q->ipqe_flags |= pkt_flags; 475 m_cat(m, q->ipqe_m); 476 q->ipqe_m = m; 477 q->ipre_mlast = m; /* last mbuf may have changed */ 478 TRAVERSE(q->ipre_mlast); 479 tiqe = q; 480 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 481 TCP_REASS_COUNTER_INCR(&tcp_reass_prependfirst); 482 goto skip_replacement; 483 } 484 } else { 485 TCP_REASS_COUNTER_INCR(&tcp_reass_empty); 486 } 487 488 /* 489 * Find a segment which begins after this one does. 490 */ 491 for (p = NULL; q != NULL; q = nq) { 492 nq = TAILQ_NEXT(q, ipqe_q); 493 #ifdef TCP_REASS_COUNTERS 494 count++; 495 #endif 496 /* 497 * If the received segment is just right after this 498 * fragment, merge the two together and then check 499 * for further overlaps. 500 */ 501 if (q->ipqe_seq + q->ipqe_len == pkt_seq) { 502 #ifdef TCPREASS_DEBUG 503 printf("tcp_reass[%p]: concat %u:%u(%u) to %u:%u(%u)\n", 504 tp, pkt_seq, pkt_seq + pkt_len, pkt_len, 505 q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len); 506 #endif 507 pkt_len += q->ipqe_len; 508 pkt_flags |= q->ipqe_flags; 509 pkt_seq = q->ipqe_seq; 510 m_cat(q->ipre_mlast, m); 511 TRAVERSE(q->ipre_mlast); 512 m = q->ipqe_m; 513 TCP_REASS_COUNTER_INCR(&tcp_reass_append); 514 goto free_ipqe; 515 } 516 /* 517 * If the received segment is completely past this 518 * fragment, we need to go the next fragment. 519 */ 520 if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) { 521 p = q; 522 continue; 523 } 524 /* 525 * If the fragment is past the received segment, 526 * it (or any following) can't be concatenated. 527 */ 528 if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len)) { 529 TCP_REASS_COUNTER_INCR(&tcp_reass_insert); 530 break; 531 } 532 533 /* 534 * We've received all the data in this segment before. 535 * mark it as a duplicate and return. 536 */ 537 if (SEQ_LEQ(q->ipqe_seq, pkt_seq) && 538 SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) { 539 tcpstat.tcps_rcvduppack++; 540 tcpstat.tcps_rcvdupbyte += pkt_len; 541 tcp_new_dsack(tp, pkt_seq, pkt_len); 542 m_freem(m); 543 if (tiqe != NULL) { 544 tcpipqent_free(tiqe); 545 } 546 TCP_REASS_COUNTER_INCR(&tcp_reass_segdup); 547 return (0); 548 } 549 /* 550 * Received segment completely overlaps this fragment 551 * so we drop the fragment (this keeps the temporal 552 * ordering of segments correct). 553 */ 554 if (SEQ_GEQ(q->ipqe_seq, pkt_seq) && 555 SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) { 556 rcvpartdupbyte += q->ipqe_len; 557 m_freem(q->ipqe_m); 558 TCP_REASS_COUNTER_INCR(&tcp_reass_fragdup); 559 goto free_ipqe; 560 } 561 /* 562 * RX'ed segment extends past the end of the 563 * fragment. Drop the overlapping bytes. Then 564 * merge the fragment and segment then treat as 565 * a longer received packet. 566 */ 567 if (SEQ_LT(q->ipqe_seq, pkt_seq) && 568 SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq)) { 569 int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq; 570 #ifdef TCPREASS_DEBUG 571 printf("tcp_reass[%p]: trim starting %d bytes of %u:%u(%u)\n", 572 tp, overlap, 573 pkt_seq, pkt_seq + pkt_len, pkt_len); 574 #endif 575 m_adj(m, overlap); 576 rcvpartdupbyte += overlap; 577 m_cat(q->ipre_mlast, m); 578 TRAVERSE(q->ipre_mlast); 579 m = q->ipqe_m; 580 pkt_seq = q->ipqe_seq; 581 pkt_len += q->ipqe_len - overlap; 582 rcvoobyte -= overlap; 583 TCP_REASS_COUNTER_INCR(&tcp_reass_overlaptail); 584 goto free_ipqe; 585 } 586 /* 587 * RX'ed segment extends past the front of the 588 * fragment. Drop the overlapping bytes on the 589 * received packet. The packet will then be 590 * contatentated with this fragment a bit later. 591 */ 592 if (SEQ_GT(q->ipqe_seq, pkt_seq) && 593 SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len)) { 594 int overlap = pkt_seq + pkt_len - q->ipqe_seq; 595 #ifdef TCPREASS_DEBUG 596 printf("tcp_reass[%p]: trim trailing %d bytes of %u:%u(%u)\n", 597 tp, overlap, 598 pkt_seq, pkt_seq + pkt_len, pkt_len); 599 #endif 600 m_adj(m, -overlap); 601 pkt_len -= overlap; 602 rcvpartdupbyte += overlap; 603 TCP_REASS_COUNTER_INCR(&tcp_reass_overlapfront); 604 rcvoobyte -= overlap; 605 } 606 /* 607 * If the received segment immediates precedes this 608 * fragment then tack the fragment onto this segment 609 * and reinsert the data. 610 */ 611 if (q->ipqe_seq == pkt_seq + pkt_len) { 612 #ifdef TCPREASS_DEBUG 613 printf("tcp_reass[%p]: append %u:%u(%u) to %u:%u(%u)\n", 614 tp, q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len, 615 pkt_seq, pkt_seq + pkt_len, pkt_len); 616 #endif 617 pkt_len += q->ipqe_len; 618 pkt_flags |= q->ipqe_flags; 619 m_cat(m, q->ipqe_m); 620 TAILQ_REMOVE(&tp->segq, q, ipqe_q); 621 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 622 tp->t_segqlen--; 623 KASSERT(tp->t_segqlen >= 0); 624 KASSERT(tp->t_segqlen != 0 || 625 (TAILQ_EMPTY(&tp->segq) && 626 TAILQ_EMPTY(&tp->timeq))); 627 if (tiqe == NULL) { 628 tiqe = q; 629 } else { 630 tcpipqent_free(q); 631 } 632 TCP_REASS_COUNTER_INCR(&tcp_reass_prepend); 633 break; 634 } 635 /* 636 * If the fragment is before the segment, remember it. 637 * When this loop is terminated, p will contain the 638 * pointer to fragment that is right before the received 639 * segment. 640 */ 641 if (SEQ_LEQ(q->ipqe_seq, pkt_seq)) 642 p = q; 643 644 continue; 645 646 /* 647 * This is a common operation. It also will allow 648 * to save doing a malloc/free in most instances. 649 */ 650 free_ipqe: 651 TAILQ_REMOVE(&tp->segq, q, ipqe_q); 652 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 653 tp->t_segqlen--; 654 KASSERT(tp->t_segqlen >= 0); 655 KASSERT(tp->t_segqlen != 0 || 656 (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq))); 657 if (tiqe == NULL) { 658 tiqe = q; 659 } else { 660 tcpipqent_free(q); 661 } 662 } 663 664 #ifdef TCP_REASS_COUNTERS 665 if (count > 7) 666 TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[0]); 667 else if (count > 0) 668 TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[count]); 669 #endif 670 671 insert_it: 672 673 /* 674 * Allocate a new queue entry since the received segment did not 675 * collapse onto any other out-of-order block; thus we are allocating 676 * a new block. If it had collapsed, tiqe would not be NULL and 677 * we would be reusing it. 678 * XXX If we can't, just drop the packet. XXX 679 */ 680 if (tiqe == NULL) { 681 tiqe = tcpipqent_alloc(); 682 if (tiqe == NULL) { 683 tcpstat.tcps_rcvmemdrop++; 684 m_freem(m); 685 return (0); 686 } 687 } 688 689 /* 690 * Update the counters. 691 */ 692 tcpstat.tcps_rcvoopack++; 693 tcpstat.tcps_rcvoobyte += rcvoobyte; 694 if (rcvpartdupbyte) { 695 tcpstat.tcps_rcvpartduppack++; 696 tcpstat.tcps_rcvpartdupbyte += rcvpartdupbyte; 697 } 698 699 /* 700 * Insert the new fragment queue entry into both queues. 701 */ 702 tiqe->ipqe_m = m; 703 tiqe->ipre_mlast = m; 704 tiqe->ipqe_seq = pkt_seq; 705 tiqe->ipqe_len = pkt_len; 706 tiqe->ipqe_flags = pkt_flags; 707 if (p == NULL) { 708 TAILQ_INSERT_HEAD(&tp->segq, tiqe, ipqe_q); 709 #ifdef TCPREASS_DEBUG 710 if (tiqe->ipqe_seq != tp->rcv_nxt) 711 printf("tcp_reass[%p]: insert %u:%u(%u) at front\n", 712 tp, pkt_seq, pkt_seq + pkt_len, pkt_len); 713 #endif 714 } else { 715 TAILQ_INSERT_AFTER(&tp->segq, p, tiqe, ipqe_q); 716 #ifdef TCPREASS_DEBUG 717 printf("tcp_reass[%p]: insert %u:%u(%u) after %u:%u(%u)\n", 718 tp, pkt_seq, pkt_seq + pkt_len, pkt_len, 719 p->ipqe_seq, p->ipqe_seq + p->ipqe_len, p->ipqe_len); 720 #endif 721 } 722 tp->t_segqlen++; 723 724 skip_replacement: 725 726 TAILQ_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq); 727 728 present: 729 /* 730 * Present data to user, advancing rcv_nxt through 731 * completed sequence space. 732 */ 733 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 734 return (0); 735 q = TAILQ_FIRST(&tp->segq); 736 if (q == NULL || q->ipqe_seq != tp->rcv_nxt) 737 return (0); 738 if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len) 739 return (0); 740 741 tp->rcv_nxt += q->ipqe_len; 742 pkt_flags = q->ipqe_flags & TH_FIN; 743 ND6_HINT(tp); 744 745 TAILQ_REMOVE(&tp->segq, q, ipqe_q); 746 TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq); 747 tp->t_segqlen--; 748 KASSERT(tp->t_segqlen >= 0); 749 KASSERT(tp->t_segqlen != 0 || 750 (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq))); 751 if (so->so_state & SS_CANTRCVMORE) 752 m_freem(q->ipqe_m); 753 else 754 sbappendstream(&so->so_rcv, q->ipqe_m); 755 tcpipqent_free(q); 756 sorwakeup(so); 757 return (pkt_flags); 758 } 759 760 #ifdef INET6 761 int 762 tcp6_input(struct mbuf **mp, int *offp, int proto) 763 { 764 struct mbuf *m = *mp; 765 766 /* 767 * draft-itojun-ipv6-tcp-to-anycast 768 * better place to put this in? 769 */ 770 if (m->m_flags & M_ANYCAST6) { 771 struct ip6_hdr *ip6; 772 if (m->m_len < sizeof(struct ip6_hdr)) { 773 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 774 tcpstat.tcps_rcvshort++; 775 return IPPROTO_DONE; 776 } 777 } 778 ip6 = mtod(m, struct ip6_hdr *); 779 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 780 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 781 return IPPROTO_DONE; 782 } 783 784 tcp_input(m, *offp, proto); 785 return IPPROTO_DONE; 786 } 787 #endif 788 789 #ifdef INET 790 static void 791 tcp4_log_refused(const struct ip *ip, const struct tcphdr *th) 792 { 793 char src[4*sizeof "123"]; 794 char dst[4*sizeof "123"]; 795 796 if (ip) { 797 strlcpy(src, inet_ntoa(ip->ip_src), sizeof(src)); 798 strlcpy(dst, inet_ntoa(ip->ip_dst), sizeof(dst)); 799 } 800 else { 801 strlcpy(src, "(unknown)", sizeof(src)); 802 strlcpy(dst, "(unknown)", sizeof(dst)); 803 } 804 log(LOG_INFO, 805 "Connection attempt to TCP %s:%d from %s:%d\n", 806 dst, ntohs(th->th_dport), 807 src, ntohs(th->th_sport)); 808 } 809 #endif 810 811 #ifdef INET6 812 static void 813 tcp6_log_refused(const struct ip6_hdr *ip6, const struct tcphdr *th) 814 { 815 char src[INET6_ADDRSTRLEN]; 816 char dst[INET6_ADDRSTRLEN]; 817 818 if (ip6) { 819 strlcpy(src, ip6_sprintf(&ip6->ip6_src), sizeof(src)); 820 strlcpy(dst, ip6_sprintf(&ip6->ip6_dst), sizeof(dst)); 821 } 822 else { 823 strlcpy(src, "(unknown v6)", sizeof(src)); 824 strlcpy(dst, "(unknown v6)", sizeof(dst)); 825 } 826 log(LOG_INFO, 827 "Connection attempt to TCP [%s]:%d from [%s]:%d\n", 828 dst, ntohs(th->th_dport), 829 src, ntohs(th->th_sport)); 830 } 831 #endif 832 833 /* 834 * Checksum extended TCP header and data. 835 */ 836 int 837 tcp_input_checksum(int af, struct mbuf *m, const struct tcphdr *th, int toff, 838 int off, int tlen) 839 { 840 841 /* 842 * XXX it's better to record and check if this mbuf is 843 * already checked. 844 */ 845 846 switch (af) { 847 #ifdef INET 848 case AF_INET: 849 switch (m->m_pkthdr.csum_flags & 850 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_TCPv4) | 851 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) { 852 case M_CSUM_TCPv4|M_CSUM_TCP_UDP_BAD: 853 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_bad); 854 goto badcsum; 855 856 case M_CSUM_TCPv4|M_CSUM_DATA: { 857 u_int32_t hw_csum = m->m_pkthdr.csum_data; 858 859 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_data); 860 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) { 861 const struct ip *ip = 862 mtod(m, const struct ip *); 863 864 hw_csum = in_cksum_phdr(ip->ip_src.s_addr, 865 ip->ip_dst.s_addr, 866 htons(hw_csum + tlen + off + IPPROTO_TCP)); 867 } 868 if ((hw_csum ^ 0xffff) != 0) 869 goto badcsum; 870 break; 871 } 872 873 case M_CSUM_TCPv4: 874 /* Checksum was okay. */ 875 TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_ok); 876 break; 877 878 default: 879 /* 880 * Must compute it ourselves. Maybe skip checksum 881 * on loopback interfaces. 882 */ 883 if (__predict_true(!(m->m_pkthdr.rcvif->if_flags & 884 IFF_LOOPBACK) || 885 tcp_do_loopback_cksum)) { 886 TCP_CSUM_COUNTER_INCR(&tcp_swcsum); 887 if (in4_cksum(m, IPPROTO_TCP, toff, 888 tlen + off) != 0) 889 goto badcsum; 890 } 891 break; 892 } 893 break; 894 #endif /* INET4 */ 895 896 #ifdef INET6 897 case AF_INET6: 898 if (__predict_true((m->m_flags & M_LOOP) == 0 || 899 tcp_do_loopback_cksum)) { 900 if (in6_cksum(m, IPPROTO_TCP, toff, tlen + off) != 0) 901 goto badcsum; 902 } 903 break; 904 #endif /* INET6 */ 905 } 906 907 return 0; 908 909 badcsum: 910 tcpstat.tcps_rcvbadsum++; 911 return -1; 912 } 913 914 /* 915 * TCP input routine, follows pages 65-76 of the 916 * protocol specification dated September, 1981 very closely. 917 */ 918 void 919 tcp_input(struct mbuf *m, ...) 920 { 921 struct tcphdr *th; 922 struct ip *ip; 923 struct inpcb *inp; 924 #ifdef INET6 925 struct ip6_hdr *ip6; 926 struct in6pcb *in6p; 927 #endif 928 u_int8_t *optp = NULL; 929 int optlen = 0; 930 int len, tlen, toff, hdroptlen = 0; 931 struct tcpcb *tp = 0; 932 int tiflags; 933 struct socket *so = NULL; 934 int todrop, dupseg, acked, ourfinisacked, needoutput = 0; 935 #ifdef TCP_DEBUG 936 short ostate = 0; 937 #endif 938 int iss = 0; 939 u_long tiwin; 940 struct tcp_opt_info opti; 941 int off, iphlen; 942 va_list ap; 943 int af; /* af on the wire */ 944 struct mbuf *tcp_saveti = NULL; 945 uint32_t ts_rtt; 946 947 MCLAIM(m, &tcp_rx_mowner); 948 va_start(ap, m); 949 toff = va_arg(ap, int); 950 (void)va_arg(ap, int); /* ignore value, advance ap */ 951 va_end(ap); 952 953 tcpstat.tcps_rcvtotal++; 954 955 bzero(&opti, sizeof(opti)); 956 opti.ts_present = 0; 957 opti.maxseg = 0; 958 959 /* 960 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN. 961 * 962 * TCP is, by definition, unicast, so we reject all 963 * multicast outright. 964 * 965 * Note, there are additional src/dst address checks in 966 * the AF-specific code below. 967 */ 968 if (m->m_flags & (M_BCAST|M_MCAST)) { 969 /* XXX stat */ 970 goto drop; 971 } 972 #ifdef INET6 973 if (m->m_flags & M_ANYCAST6) { 974 /* XXX stat */ 975 goto drop; 976 } 977 #endif 978 979 /* 980 * Get IP and TCP header. 981 * Note: IP leaves IP header in first mbuf. 982 */ 983 ip = mtod(m, struct ip *); 984 #ifdef INET6 985 ip6 = NULL; 986 #endif 987 switch (ip->ip_v) { 988 #ifdef INET 989 case 4: 990 af = AF_INET; 991 iphlen = sizeof(struct ip); 992 ip = mtod(m, struct ip *); 993 IP6_EXTHDR_GET(th, struct tcphdr *, m, toff, 994 sizeof(struct tcphdr)); 995 if (th == NULL) { 996 tcpstat.tcps_rcvshort++; 997 return; 998 } 999 /* We do the checksum after PCB lookup... */ 1000 len = ntohs(ip->ip_len); 1001 tlen = len - toff; 1002 break; 1003 #endif 1004 #ifdef INET6 1005 case 6: 1006 ip = NULL; 1007 iphlen = sizeof(struct ip6_hdr); 1008 af = AF_INET6; 1009 ip6 = mtod(m, struct ip6_hdr *); 1010 IP6_EXTHDR_GET(th, struct tcphdr *, m, toff, 1011 sizeof(struct tcphdr)); 1012 if (th == NULL) { 1013 tcpstat.tcps_rcvshort++; 1014 return; 1015 } 1016 1017 /* Be proactive about malicious use of IPv4 mapped address */ 1018 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 1019 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 1020 /* XXX stat */ 1021 goto drop; 1022 } 1023 1024 /* 1025 * Be proactive about unspecified IPv6 address in source. 1026 * As we use all-zero to indicate unbounded/unconnected pcb, 1027 * unspecified IPv6 address can be used to confuse us. 1028 * 1029 * Note that packets with unspecified IPv6 destination is 1030 * already dropped in ip6_input. 1031 */ 1032 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 1033 /* XXX stat */ 1034 goto drop; 1035 } 1036 1037 /* 1038 * Make sure destination address is not multicast. 1039 * Source address checked in ip6_input(). 1040 */ 1041 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 1042 /* XXX stat */ 1043 goto drop; 1044 } 1045 1046 /* We do the checksum after PCB lookup... */ 1047 len = m->m_pkthdr.len; 1048 tlen = len - toff; 1049 break; 1050 #endif 1051 default: 1052 m_freem(m); 1053 return; 1054 } 1055 1056 KASSERT(TCP_HDR_ALIGNED_P(th)); 1057 1058 /* 1059 * Check that TCP offset makes sense, 1060 * pull out TCP options and adjust length. XXX 1061 */ 1062 off = th->th_off << 2; 1063 if (off < sizeof (struct tcphdr) || off > tlen) { 1064 tcpstat.tcps_rcvbadoff++; 1065 goto drop; 1066 } 1067 tlen -= off; 1068 1069 /* 1070 * tcp_input() has been modified to use tlen to mean the TCP data 1071 * length throughout the function. Other functions can use 1072 * m->m_pkthdr.len as the basis for calculating the TCP data length. 1073 * rja 1074 */ 1075 1076 if (off > sizeof (struct tcphdr)) { 1077 IP6_EXTHDR_GET(th, struct tcphdr *, m, toff, off); 1078 if (th == NULL) { 1079 tcpstat.tcps_rcvshort++; 1080 return; 1081 } 1082 /* 1083 * NOTE: ip/ip6 will not be affected by m_pulldown() 1084 * (as they're before toff) and we don't need to update those. 1085 */ 1086 KASSERT(TCP_HDR_ALIGNED_P(th)); 1087 optlen = off - sizeof (struct tcphdr); 1088 optp = ((u_int8_t *)th) + sizeof(struct tcphdr); 1089 /* 1090 * Do quick retrieval of timestamp options ("options 1091 * prediction?"). If timestamp is the only option and it's 1092 * formatted as recommended in RFC 1323 appendix A, we 1093 * quickly get the values now and not bother calling 1094 * tcp_dooptions(), etc. 1095 */ 1096 if ((optlen == TCPOLEN_TSTAMP_APPA || 1097 (optlen > TCPOLEN_TSTAMP_APPA && 1098 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && 1099 *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) && 1100 (th->th_flags & TH_SYN) == 0) { 1101 opti.ts_present = 1; 1102 opti.ts_val = ntohl(*(u_int32_t *)(optp + 4)); 1103 opti.ts_ecr = ntohl(*(u_int32_t *)(optp + 8)); 1104 optp = NULL; /* we've parsed the options */ 1105 } 1106 } 1107 tiflags = th->th_flags; 1108 1109 /* 1110 * Locate pcb for segment. 1111 */ 1112 findpcb: 1113 inp = NULL; 1114 #ifdef INET6 1115 in6p = NULL; 1116 #endif 1117 switch (af) { 1118 #ifdef INET 1119 case AF_INET: 1120 inp = in_pcblookup_connect(&tcbtable, ip->ip_src, th->th_sport, 1121 ip->ip_dst, th->th_dport); 1122 if (inp == 0) { 1123 ++tcpstat.tcps_pcbhashmiss; 1124 inp = in_pcblookup_bind(&tcbtable, ip->ip_dst, th->th_dport); 1125 } 1126 #ifdef INET6 1127 if (inp == 0) { 1128 struct in6_addr s, d; 1129 1130 /* mapped addr case */ 1131 bzero(&s, sizeof(s)); 1132 s.s6_addr16[5] = htons(0xffff); 1133 bcopy(&ip->ip_src, &s.s6_addr32[3], sizeof(ip->ip_src)); 1134 bzero(&d, sizeof(d)); 1135 d.s6_addr16[5] = htons(0xffff); 1136 bcopy(&ip->ip_dst, &d.s6_addr32[3], sizeof(ip->ip_dst)); 1137 in6p = in6_pcblookup_connect(&tcbtable, &s, 1138 th->th_sport, &d, th->th_dport, 0); 1139 if (in6p == 0) { 1140 ++tcpstat.tcps_pcbhashmiss; 1141 in6p = in6_pcblookup_bind(&tcbtable, &d, 1142 th->th_dport, 0); 1143 } 1144 } 1145 #endif 1146 #ifndef INET6 1147 if (inp == 0) 1148 #else 1149 if (inp == 0 && in6p == 0) 1150 #endif 1151 { 1152 ++tcpstat.tcps_noport; 1153 if (tcp_log_refused && 1154 (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) { 1155 tcp4_log_refused(ip, th); 1156 } 1157 TCP_FIELDS_TO_HOST(th); 1158 goto dropwithreset_ratelim; 1159 } 1160 #if defined(IPSEC) || defined(FAST_IPSEC) 1161 if (inp && (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0 && 1162 ipsec4_in_reject(m, inp)) { 1163 ipsecstat.in_polvio++; 1164 goto drop; 1165 } 1166 #ifdef INET6 1167 else if (in6p && 1168 (in6p->in6p_socket->so_options & SO_ACCEPTCONN) == 0 && 1169 ipsec4_in_reject_so(m, in6p->in6p_socket)) { 1170 ipsecstat.in_polvio++; 1171 goto drop; 1172 } 1173 #endif 1174 #endif /*IPSEC*/ 1175 break; 1176 #endif /*INET*/ 1177 #ifdef INET6 1178 case AF_INET6: 1179 { 1180 int faith; 1181 1182 #if defined(NFAITH) && NFAITH > 0 1183 faith = faithprefix(&ip6->ip6_dst); 1184 #else 1185 faith = 0; 1186 #endif 1187 in6p = in6_pcblookup_connect(&tcbtable, &ip6->ip6_src, 1188 th->th_sport, &ip6->ip6_dst, th->th_dport, faith); 1189 if (in6p == NULL) { 1190 ++tcpstat.tcps_pcbhashmiss; 1191 in6p = in6_pcblookup_bind(&tcbtable, &ip6->ip6_dst, 1192 th->th_dport, faith); 1193 } 1194 if (in6p == NULL) { 1195 ++tcpstat.tcps_noport; 1196 if (tcp_log_refused && 1197 (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) { 1198 tcp6_log_refused(ip6, th); 1199 } 1200 TCP_FIELDS_TO_HOST(th); 1201 goto dropwithreset_ratelim; 1202 } 1203 #if defined(IPSEC) || defined(FAST_IPSEC) 1204 if ((in6p->in6p_socket->so_options & SO_ACCEPTCONN) == 0 && 1205 ipsec6_in_reject(m, in6p)) { 1206 ipsec6stat.in_polvio++; 1207 goto drop; 1208 } 1209 #endif /*IPSEC*/ 1210 break; 1211 } 1212 #endif 1213 } 1214 1215 /* 1216 * If the state is CLOSED (i.e., TCB does not exist) then 1217 * all data in the incoming segment is discarded. 1218 * If the TCB exists but is in CLOSED state, it is embryonic, 1219 * but should either do a listen or a connect soon. 1220 */ 1221 tp = NULL; 1222 so = NULL; 1223 if (inp) { 1224 tp = intotcpcb(inp); 1225 so = inp->inp_socket; 1226 } 1227 #ifdef INET6 1228 else if (in6p) { 1229 tp = in6totcpcb(in6p); 1230 so = in6p->in6p_socket; 1231 } 1232 #endif 1233 if (tp == 0) { 1234 TCP_FIELDS_TO_HOST(th); 1235 goto dropwithreset_ratelim; 1236 } 1237 if (tp->t_state == TCPS_CLOSED) 1238 goto drop; 1239 1240 /* 1241 * Checksum extended TCP header and data. 1242 */ 1243 if (tcp_input_checksum(af, m, th, toff, off, tlen)) 1244 goto badcsum; 1245 1246 TCP_FIELDS_TO_HOST(th); 1247 1248 /* Unscale the window into a 32-bit value. */ 1249 if ((tiflags & TH_SYN) == 0) 1250 tiwin = th->th_win << tp->snd_scale; 1251 else 1252 tiwin = th->th_win; 1253 1254 #ifdef INET6 1255 /* save packet options if user wanted */ 1256 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS)) { 1257 if (in6p->in6p_options) { 1258 m_freem(in6p->in6p_options); 1259 in6p->in6p_options = 0; 1260 } 1261 ip6_savecontrol(in6p, &in6p->in6p_options, ip6, m); 1262 } 1263 #endif 1264 1265 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 1266 union syn_cache_sa src; 1267 union syn_cache_sa dst; 1268 1269 bzero(&src, sizeof(src)); 1270 bzero(&dst, sizeof(dst)); 1271 switch (af) { 1272 #ifdef INET 1273 case AF_INET: 1274 src.sin.sin_len = sizeof(struct sockaddr_in); 1275 src.sin.sin_family = AF_INET; 1276 src.sin.sin_addr = ip->ip_src; 1277 src.sin.sin_port = th->th_sport; 1278 1279 dst.sin.sin_len = sizeof(struct sockaddr_in); 1280 dst.sin.sin_family = AF_INET; 1281 dst.sin.sin_addr = ip->ip_dst; 1282 dst.sin.sin_port = th->th_dport; 1283 break; 1284 #endif 1285 #ifdef INET6 1286 case AF_INET6: 1287 src.sin6.sin6_len = sizeof(struct sockaddr_in6); 1288 src.sin6.sin6_family = AF_INET6; 1289 src.sin6.sin6_addr = ip6->ip6_src; 1290 src.sin6.sin6_port = th->th_sport; 1291 1292 dst.sin6.sin6_len = sizeof(struct sockaddr_in6); 1293 dst.sin6.sin6_family = AF_INET6; 1294 dst.sin6.sin6_addr = ip6->ip6_dst; 1295 dst.sin6.sin6_port = th->th_dport; 1296 break; 1297 #endif /* INET6 */ 1298 default: 1299 goto badsyn; /*sanity*/ 1300 } 1301 1302 if (so->so_options & SO_DEBUG) { 1303 #ifdef TCP_DEBUG 1304 ostate = tp->t_state; 1305 #endif 1306 1307 tcp_saveti = NULL; 1308 if (iphlen + sizeof(struct tcphdr) > MHLEN) 1309 goto nosave; 1310 1311 if (m->m_len > iphlen && (m->m_flags & M_EXT) == 0) { 1312 tcp_saveti = m_copym(m, 0, iphlen, M_DONTWAIT); 1313 if (!tcp_saveti) 1314 goto nosave; 1315 } else { 1316 MGETHDR(tcp_saveti, M_DONTWAIT, MT_HEADER); 1317 if (!tcp_saveti) 1318 goto nosave; 1319 MCLAIM(m, &tcp_mowner); 1320 tcp_saveti->m_len = iphlen; 1321 m_copydata(m, 0, iphlen, 1322 mtod(tcp_saveti, caddr_t)); 1323 } 1324 1325 if (M_TRAILINGSPACE(tcp_saveti) < sizeof(struct tcphdr)) { 1326 m_freem(tcp_saveti); 1327 tcp_saveti = NULL; 1328 } else { 1329 tcp_saveti->m_len += sizeof(struct tcphdr); 1330 bcopy(th, mtod(tcp_saveti, caddr_t) + iphlen, 1331 sizeof(struct tcphdr)); 1332 } 1333 nosave:; 1334 } 1335 if (so->so_options & SO_ACCEPTCONN) { 1336 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 1337 if (tiflags & TH_RST) { 1338 syn_cache_reset(&src.sa, &dst.sa, th); 1339 } else if ((tiflags & (TH_ACK|TH_SYN)) == 1340 (TH_ACK|TH_SYN)) { 1341 /* 1342 * Received a SYN,ACK. This should 1343 * never happen while we are in 1344 * LISTEN. Send an RST. 1345 */ 1346 goto badsyn; 1347 } else if (tiflags & TH_ACK) { 1348 so = syn_cache_get(&src.sa, &dst.sa, 1349 th, toff, tlen, so, m); 1350 if (so == NULL) { 1351 /* 1352 * We don't have a SYN for 1353 * this ACK; send an RST. 1354 */ 1355 goto badsyn; 1356 } else if (so == 1357 (struct socket *)(-1)) { 1358 /* 1359 * We were unable to create 1360 * the connection. If the 1361 * 3-way handshake was 1362 * completed, and RST has 1363 * been sent to the peer. 1364 * Since the mbuf might be 1365 * in use for the reply, 1366 * do not free it. 1367 */ 1368 m = NULL; 1369 } else { 1370 /* 1371 * We have created a 1372 * full-blown connection. 1373 */ 1374 tp = NULL; 1375 inp = NULL; 1376 #ifdef INET6 1377 in6p = NULL; 1378 #endif 1379 switch (so->so_proto->pr_domain->dom_family) { 1380 #ifdef INET 1381 case AF_INET: 1382 inp = sotoinpcb(so); 1383 tp = intotcpcb(inp); 1384 break; 1385 #endif 1386 #ifdef INET6 1387 case AF_INET6: 1388 in6p = sotoin6pcb(so); 1389 tp = in6totcpcb(in6p); 1390 break; 1391 #endif 1392 } 1393 if (tp == NULL) 1394 goto badsyn; /*XXX*/ 1395 tiwin <<= tp->snd_scale; 1396 goto after_listen; 1397 } 1398 } else { 1399 /* 1400 * None of RST, SYN or ACK was set. 1401 * This is an invalid packet for a 1402 * TCB in LISTEN state. Send a RST. 1403 */ 1404 goto badsyn; 1405 } 1406 } else { 1407 /* 1408 * Received a SYN. 1409 */ 1410 1411 #ifdef INET6 1412 /* 1413 * If deprecated address is forbidden, we do 1414 * not accept SYN to deprecated interface 1415 * address to prevent any new inbound 1416 * connection from getting established. 1417 * When we do not accept SYN, we send a TCP 1418 * RST, with deprecated source address (instead 1419 * of dropping it). We compromise it as it is 1420 * much better for peer to send a RST, and 1421 * RST will be the final packet for the 1422 * exchange. 1423 * 1424 * If we do not forbid deprecated addresses, we 1425 * accept the SYN packet. RFC2462 does not 1426 * suggest dropping SYN in this case. 1427 * If we decipher RFC2462 5.5.4, it says like 1428 * this: 1429 * 1. use of deprecated addr with existing 1430 * communication is okay - "SHOULD continue 1431 * to be used" 1432 * 2. use of it with new communication: 1433 * (2a) "SHOULD NOT be used if alternate 1434 * address with sufficient scope is 1435 * available" 1436 * (2b) nothing mentioned otherwise. 1437 * Here we fall into (2b) case as we have no 1438 * choice in our source address selection - we 1439 * must obey the peer. 1440 * 1441 * The wording in RFC2462 is confusing, and 1442 * there are multiple description text for 1443 * deprecated address handling - worse, they 1444 * are not exactly the same. I believe 5.5.4 1445 * is the best one, so we follow 5.5.4. 1446 */ 1447 if (af == AF_INET6 && !ip6_use_deprecated) { 1448 struct in6_ifaddr *ia6; 1449 if ((ia6 = in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, 1450 &ip6->ip6_dst)) && 1451 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 1452 tp = NULL; 1453 goto dropwithreset; 1454 } 1455 } 1456 #endif 1457 1458 #ifdef IPSEC 1459 switch (af) { 1460 #ifdef INET 1461 case AF_INET: 1462 if (ipsec4_in_reject_so(m, so)) { 1463 ipsecstat.in_polvio++; 1464 tp = NULL; 1465 goto dropwithreset; 1466 } 1467 break; 1468 #endif 1469 #ifdef INET6 1470 case AF_INET6: 1471 if (ipsec6_in_reject_so(m, so)) { 1472 ipsec6stat.in_polvio++; 1473 tp = NULL; 1474 goto dropwithreset; 1475 } 1476 break; 1477 #endif 1478 } 1479 #endif 1480 1481 /* 1482 * LISTEN socket received a SYN 1483 * from itself? This can't possibly 1484 * be valid; drop the packet. 1485 */ 1486 if (th->th_sport == th->th_dport) { 1487 int i; 1488 1489 switch (af) { 1490 #ifdef INET 1491 case AF_INET: 1492 i = in_hosteq(ip->ip_src, ip->ip_dst); 1493 break; 1494 #endif 1495 #ifdef INET6 1496 case AF_INET6: 1497 i = IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst); 1498 break; 1499 #endif 1500 default: 1501 i = 1; 1502 } 1503 if (i) { 1504 tcpstat.tcps_badsyn++; 1505 goto drop; 1506 } 1507 } 1508 1509 /* 1510 * SYN looks ok; create compressed TCP 1511 * state for it. 1512 */ 1513 if (so->so_qlen <= so->so_qlimit && 1514 syn_cache_add(&src.sa, &dst.sa, th, tlen, 1515 so, m, optp, optlen, &opti)) 1516 m = NULL; 1517 } 1518 goto drop; 1519 } 1520 } 1521 1522 after_listen: 1523 #ifdef DIAGNOSTIC 1524 /* 1525 * Should not happen now that all embryonic connections 1526 * are handled with compressed state. 1527 */ 1528 if (tp->t_state == TCPS_LISTEN) 1529 panic("tcp_input: TCPS_LISTEN"); 1530 #endif 1531 1532 /* 1533 * Segment received on connection. 1534 * Reset idle time and keep-alive timer. 1535 */ 1536 tp->t_rcvtime = tcp_now; 1537 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1538 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 1539 1540 /* 1541 * Process options. 1542 */ 1543 #ifdef TCP_SIGNATURE 1544 if (optp || (tp->t_flags & TF_SIGNATURE)) 1545 #else 1546 if (optp) 1547 #endif 1548 if (tcp_dooptions(tp, optp, optlen, th, m, toff, &opti) < 0) 1549 goto drop; 1550 1551 if (TCP_SACK_ENABLED(tp)) { 1552 tcp_del_sackholes(tp, th); 1553 } 1554 1555 if (opti.ts_present && opti.ts_ecr) { 1556 /* 1557 * Calculate the RTT from the returned time stamp and the 1558 * connection's time base. If the time stamp is later than 1559 * the current time, or is extremely old, fall back to non-1323 1560 * RTT calculation. Since ts_ecr is unsigned, we can test both 1561 * at the same time. 1562 */ 1563 ts_rtt = TCP_TIMESTAMP(tp) - opti.ts_ecr + 1; 1564 if (ts_rtt > TCP_PAWS_IDLE) 1565 ts_rtt = 0; 1566 } else { 1567 ts_rtt = 0; 1568 } 1569 1570 /* 1571 * Header prediction: check for the two common cases 1572 * of a uni-directional data xfer. If the packet has 1573 * no control flags, is in-sequence, the window didn't 1574 * change and we're not retransmitting, it's a 1575 * candidate. If the length is zero and the ack moved 1576 * forward, we're the sender side of the xfer. Just 1577 * free the data acked & wake any higher level process 1578 * that was blocked waiting for space. If the length 1579 * is non-zero and the ack didn't move, we're the 1580 * receiver side. If we're getting packets in-order 1581 * (the reassembly queue is empty), add the data to 1582 * the socket buffer and note that we need a delayed ack. 1583 */ 1584 if (tp->t_state == TCPS_ESTABLISHED && 1585 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1586 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) && 1587 th->th_seq == tp->rcv_nxt && 1588 tiwin && tiwin == tp->snd_wnd && 1589 tp->snd_nxt == tp->snd_max) { 1590 1591 /* 1592 * If last ACK falls within this segment's sequence numbers, 1593 * record the timestamp. 1594 * NOTE: 1595 * 1) That the test incorporates suggestions from the latest 1596 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1597 * 2) That updating only on newer timestamps interferes with 1598 * our earlier PAWS tests, so this check should be solely 1599 * predicated on the sequence space of this segment. 1600 * 3) That we modify the segment boundary check to be 1601 * Last.ACK.Sent <= SEG.SEQ + SEG.Len 1602 * instead of RFC1323's 1603 * Last.ACK.Sent < SEG.SEQ + SEG.Len, 1604 * This modified check allows us to overcome RFC1323's 1605 * limitations as described in Stevens TCP/IP Illustrated 1606 * Vol. 2 p.869. In such cases, we can still calculate the 1607 * RTT correctly when RCV.NXT == Last.ACK.Sent. 1608 */ 1609 if (opti.ts_present && 1610 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 1611 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 1612 ((tiflags & (TH_SYN|TH_FIN)) != 0))) { 1613 tp->ts_recent_age = tcp_now; 1614 tp->ts_recent = opti.ts_val; 1615 } 1616 1617 if (tlen == 0) { 1618 /* Ack prediction. */ 1619 if (SEQ_GT(th->th_ack, tp->snd_una) && 1620 SEQ_LEQ(th->th_ack, tp->snd_max) && 1621 tp->snd_cwnd >= tp->snd_wnd && 1622 tp->t_partialacks < 0) { 1623 /* 1624 * this is a pure ack for outstanding data. 1625 */ 1626 ++tcpstat.tcps_predack; 1627 if (ts_rtt) 1628 tcp_xmit_timer(tp, ts_rtt); 1629 else if (tp->t_rtttime && 1630 SEQ_GT(th->th_ack, tp->t_rtseq)) 1631 tcp_xmit_timer(tp, 1632 tcp_now - tp->t_rtttime); 1633 acked = th->th_ack - tp->snd_una; 1634 tcpstat.tcps_rcvackpack++; 1635 tcpstat.tcps_rcvackbyte += acked; 1636 ND6_HINT(tp); 1637 1638 if (acked > (tp->t_lastoff - tp->t_inoff)) 1639 tp->t_lastm = NULL; 1640 sbdrop(&so->so_snd, acked); 1641 tp->t_lastoff -= acked; 1642 1643 ICMP_CHECK(tp, th, acked); 1644 1645 tp->snd_una = th->th_ack; 1646 tp->snd_fack = tp->snd_una; 1647 if (SEQ_LT(tp->snd_high, tp->snd_una)) 1648 tp->snd_high = tp->snd_una; 1649 m_freem(m); 1650 1651 /* 1652 * If all outstanding data are acked, stop 1653 * retransmit timer, otherwise restart timer 1654 * using current (possibly backed-off) value. 1655 * If process is waiting for space, 1656 * wakeup/selwakeup/signal. If data 1657 * are ready to send, let tcp_output 1658 * decide between more output or persist. 1659 */ 1660 if (tp->snd_una == tp->snd_max) 1661 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1662 else if (TCP_TIMER_ISARMED(tp, 1663 TCPT_PERSIST) == 0) 1664 TCP_TIMER_ARM(tp, TCPT_REXMT, 1665 tp->t_rxtcur); 1666 1667 sowwakeup(so); 1668 if (so->so_snd.sb_cc) 1669 (void) tcp_output(tp); 1670 if (tcp_saveti) 1671 m_freem(tcp_saveti); 1672 return; 1673 } 1674 } else if (th->th_ack == tp->snd_una && 1675 TAILQ_FIRST(&tp->segq) == NULL && 1676 tlen <= sbspace(&so->so_rcv)) { 1677 /* 1678 * this is a pure, in-sequence data packet 1679 * with nothing on the reassembly queue and 1680 * we have enough buffer space to take it. 1681 */ 1682 ++tcpstat.tcps_preddat; 1683 tp->rcv_nxt += tlen; 1684 tcpstat.tcps_rcvpack++; 1685 tcpstat.tcps_rcvbyte += tlen; 1686 ND6_HINT(tp); 1687 /* 1688 * Drop TCP, IP headers and TCP options then add data 1689 * to socket buffer. 1690 */ 1691 if (so->so_state & SS_CANTRCVMORE) 1692 m_freem(m); 1693 else { 1694 m_adj(m, toff + off); 1695 sbappendstream(&so->so_rcv, m); 1696 } 1697 sorwakeup(so); 1698 TCP_SETUP_ACK(tp, th); 1699 if (tp->t_flags & TF_ACKNOW) 1700 (void) tcp_output(tp); 1701 if (tcp_saveti) 1702 m_freem(tcp_saveti); 1703 return; 1704 } 1705 } 1706 1707 /* 1708 * Compute mbuf offset to TCP data segment. 1709 */ 1710 hdroptlen = toff + off; 1711 1712 /* 1713 * Calculate amount of space in receive window, 1714 * and then do TCP input processing. 1715 * Receive window is amount of space in rcv queue, 1716 * but not less than advertised window. 1717 */ 1718 { int win; 1719 1720 win = sbspace(&so->so_rcv); 1721 if (win < 0) 1722 win = 0; 1723 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1724 } 1725 1726 switch (tp->t_state) { 1727 case TCPS_LISTEN: 1728 /* 1729 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 1730 */ 1731 if (m->m_flags & (M_BCAST|M_MCAST)) 1732 goto drop; 1733 switch (af) { 1734 #ifdef INET6 1735 case AF_INET6: 1736 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 1737 goto drop; 1738 break; 1739 #endif /* INET6 */ 1740 case AF_INET: 1741 if (IN_MULTICAST(ip->ip_dst.s_addr) || 1742 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 1743 goto drop; 1744 break; 1745 } 1746 break; 1747 1748 /* 1749 * If the state is SYN_SENT: 1750 * if seg contains an ACK, but not for our SYN, drop the input. 1751 * if seg contains a RST, then drop the connection. 1752 * if seg does not contain SYN, then drop it. 1753 * Otherwise this is an acceptable SYN segment 1754 * initialize tp->rcv_nxt and tp->irs 1755 * if seg contains ack then advance tp->snd_una 1756 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1757 * arrange for segment to be acked (eventually) 1758 * continue processing rest of data/controls, beginning with URG 1759 */ 1760 case TCPS_SYN_SENT: 1761 if ((tiflags & TH_ACK) && 1762 (SEQ_LEQ(th->th_ack, tp->iss) || 1763 SEQ_GT(th->th_ack, tp->snd_max))) 1764 goto dropwithreset; 1765 if (tiflags & TH_RST) { 1766 if (tiflags & TH_ACK) 1767 tp = tcp_drop(tp, ECONNREFUSED); 1768 goto drop; 1769 } 1770 if ((tiflags & TH_SYN) == 0) 1771 goto drop; 1772 if (tiflags & TH_ACK) { 1773 tp->snd_una = th->th_ack; 1774 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1775 tp->snd_nxt = tp->snd_una; 1776 if (SEQ_LT(tp->snd_high, tp->snd_una)) 1777 tp->snd_high = tp->snd_una; 1778 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1779 } 1780 tp->irs = th->th_seq; 1781 tcp_rcvseqinit(tp); 1782 tp->t_flags |= TF_ACKNOW; 1783 tcp_mss_from_peer(tp, opti.maxseg); 1784 1785 /* 1786 * Initialize the initial congestion window. If we 1787 * had to retransmit the SYN, we must initialize cwnd 1788 * to 1 segment (i.e. the Loss Window). 1789 */ 1790 if (tp->t_flags & TF_SYN_REXMT) 1791 tp->snd_cwnd = tp->t_peermss; 1792 else { 1793 int ss = tcp_init_win; 1794 #ifdef INET 1795 if (inp != NULL && in_localaddr(inp->inp_faddr)) 1796 ss = tcp_init_win_local; 1797 #endif 1798 #ifdef INET6 1799 if (in6p != NULL && in6_localaddr(&in6p->in6p_faddr)) 1800 ss = tcp_init_win_local; 1801 #endif 1802 tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss); 1803 } 1804 1805 tcp_rmx_rtt(tp); 1806 if (tiflags & TH_ACK) { 1807 tcpstat.tcps_connects++; 1808 soisconnected(so); 1809 tcp_established(tp); 1810 /* Do window scaling on this connection? */ 1811 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1812 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1813 tp->snd_scale = tp->requested_s_scale; 1814 tp->rcv_scale = tp->request_r_scale; 1815 } 1816 TCP_REASS_LOCK(tp); 1817 (void) tcp_reass(tp, NULL, (struct mbuf *)0, &tlen); 1818 TCP_REASS_UNLOCK(tp); 1819 /* 1820 * if we didn't have to retransmit the SYN, 1821 * use its rtt as our initial srtt & rtt var. 1822 */ 1823 if (tp->t_rtttime) 1824 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 1825 } else 1826 tp->t_state = TCPS_SYN_RECEIVED; 1827 1828 /* 1829 * Advance th->th_seq to correspond to first data byte. 1830 * If data, trim to stay within window, 1831 * dropping FIN if necessary. 1832 */ 1833 th->th_seq++; 1834 if (tlen > tp->rcv_wnd) { 1835 todrop = tlen - tp->rcv_wnd; 1836 m_adj(m, -todrop); 1837 tlen = tp->rcv_wnd; 1838 tiflags &= ~TH_FIN; 1839 tcpstat.tcps_rcvpackafterwin++; 1840 tcpstat.tcps_rcvbyteafterwin += todrop; 1841 } 1842 tp->snd_wl1 = th->th_seq - 1; 1843 tp->rcv_up = th->th_seq; 1844 goto step6; 1845 1846 /* 1847 * If the state is SYN_RECEIVED: 1848 * If seg contains an ACK, but not for our SYN, drop the input 1849 * and generate an RST. See page 36, rfc793 1850 */ 1851 case TCPS_SYN_RECEIVED: 1852 if ((tiflags & TH_ACK) && 1853 (SEQ_LEQ(th->th_ack, tp->iss) || 1854 SEQ_GT(th->th_ack, tp->snd_max))) 1855 goto dropwithreset; 1856 break; 1857 } 1858 1859 /* 1860 * States other than LISTEN or SYN_SENT. 1861 * First check timestamp, if present. 1862 * Then check that at least some bytes of segment are within 1863 * receive window. If segment begins before rcv_nxt, 1864 * drop leading data (and SYN); if nothing left, just ack. 1865 * 1866 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1867 * and it's less than ts_recent, drop it. 1868 */ 1869 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && 1870 TSTMP_LT(opti.ts_val, tp->ts_recent)) { 1871 1872 /* Check to see if ts_recent is over 24 days old. */ 1873 if (tcp_now - tp->ts_recent_age > TCP_PAWS_IDLE) { 1874 /* 1875 * Invalidate ts_recent. If this segment updates 1876 * ts_recent, the age will be reset later and ts_recent 1877 * will get a valid value. If it does not, setting 1878 * ts_recent to zero will at least satisfy the 1879 * requirement that zero be placed in the timestamp 1880 * echo reply when ts_recent isn't valid. The 1881 * age isn't reset until we get a valid ts_recent 1882 * because we don't want out-of-order segments to be 1883 * dropped when ts_recent is old. 1884 */ 1885 tp->ts_recent = 0; 1886 } else { 1887 tcpstat.tcps_rcvduppack++; 1888 tcpstat.tcps_rcvdupbyte += tlen; 1889 tcpstat.tcps_pawsdrop++; 1890 tcp_new_dsack(tp, th->th_seq, tlen); 1891 goto dropafterack; 1892 } 1893 } 1894 1895 todrop = tp->rcv_nxt - th->th_seq; 1896 dupseg = FALSE; 1897 if (todrop > 0) { 1898 if (tiflags & TH_SYN) { 1899 tiflags &= ~TH_SYN; 1900 th->th_seq++; 1901 if (th->th_urp > 1) 1902 th->th_urp--; 1903 else { 1904 tiflags &= ~TH_URG; 1905 th->th_urp = 0; 1906 } 1907 todrop--; 1908 } 1909 if (todrop > tlen || 1910 (todrop == tlen && (tiflags & TH_FIN) == 0)) { 1911 /* 1912 * Any valid FIN or RST must be to the left of the 1913 * window. At this point the FIN or RST must be a 1914 * duplicate or out of sequence; drop it. 1915 */ 1916 if (tiflags & TH_RST) 1917 goto drop; 1918 tiflags &= ~(TH_FIN|TH_RST); 1919 /* 1920 * Send an ACK to resynchronize and drop any data. 1921 * But keep on processing for RST or ACK. 1922 */ 1923 tp->t_flags |= TF_ACKNOW; 1924 todrop = tlen; 1925 dupseg = TRUE; 1926 tcpstat.tcps_rcvdupbyte += todrop; 1927 tcpstat.tcps_rcvduppack++; 1928 } else if ((tiflags & TH_RST) && 1929 th->th_seq != tp->last_ack_sent) { 1930 /* 1931 * Test for reset before adjusting the sequence 1932 * number for overlapping data. 1933 */ 1934 goto dropafterack_ratelim; 1935 } else { 1936 tcpstat.tcps_rcvpartduppack++; 1937 tcpstat.tcps_rcvpartdupbyte += todrop; 1938 } 1939 tcp_new_dsack(tp, th->th_seq, todrop); 1940 hdroptlen += todrop; /*drop from head afterwards*/ 1941 th->th_seq += todrop; 1942 tlen -= todrop; 1943 if (th->th_urp > todrop) 1944 th->th_urp -= todrop; 1945 else { 1946 tiflags &= ~TH_URG; 1947 th->th_urp = 0; 1948 } 1949 } 1950 1951 /* 1952 * If new data are received on a connection after the 1953 * user processes are gone, then RST the other end. 1954 */ 1955 if ((so->so_state & SS_NOFDREF) && 1956 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1957 tp = tcp_close(tp); 1958 tcpstat.tcps_rcvafterclose++; 1959 goto dropwithreset; 1960 } 1961 1962 /* 1963 * If segment ends after window, drop trailing data 1964 * (and PUSH and FIN); if nothing left, just ACK. 1965 */ 1966 todrop = (th->th_seq + tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1967 if (todrop > 0) { 1968 tcpstat.tcps_rcvpackafterwin++; 1969 if (todrop >= tlen) { 1970 /* 1971 * The segment actually starts after the window. 1972 * th->th_seq + tlen - tp->rcv_nxt - tp->rcv_wnd >= tlen 1973 * th->th_seq - tp->rcv_nxt - tp->rcv_wnd >= 0 1974 * th->th_seq >= tp->rcv_nxt + tp->rcv_wnd 1975 */ 1976 tcpstat.tcps_rcvbyteafterwin += tlen; 1977 /* 1978 * If a new connection request is received 1979 * while in TIME_WAIT, drop the old connection 1980 * and start over if the sequence numbers 1981 * are above the previous ones. 1982 * 1983 * NOTE: We will checksum the packet again, and 1984 * so we need to put the header fields back into 1985 * network order! 1986 * XXX This kind of sucks, but we don't expect 1987 * XXX this to happen very often, so maybe it 1988 * XXX doesn't matter so much. 1989 */ 1990 if (tiflags & TH_SYN && 1991 tp->t_state == TCPS_TIME_WAIT && 1992 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1993 iss = tcp_new_iss(tp, tp->snd_nxt); 1994 tp = tcp_close(tp); 1995 TCP_FIELDS_TO_NET(th); 1996 goto findpcb; 1997 } 1998 /* 1999 * If window is closed can only take segments at 2000 * window edge, and have to drop data and PUSH from 2001 * incoming segments. Continue processing, but 2002 * remember to ack. Otherwise, drop segment 2003 * and (if not RST) ack. 2004 */ 2005 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2006 tp->t_flags |= TF_ACKNOW; 2007 tcpstat.tcps_rcvwinprobe++; 2008 } else 2009 goto dropafterack; 2010 } else 2011 tcpstat.tcps_rcvbyteafterwin += todrop; 2012 m_adj(m, -todrop); 2013 tlen -= todrop; 2014 tiflags &= ~(TH_PUSH|TH_FIN); 2015 } 2016 2017 /* 2018 * If last ACK falls within this segment's sequence numbers, 2019 * and the timestamp is newer, record it. 2020 */ 2021 if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) && 2022 SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2023 SEQ_LT(tp->last_ack_sent, th->th_seq + tlen + 2024 ((tiflags & (TH_SYN|TH_FIN)) != 0))) { 2025 tp->ts_recent_age = tcp_now; 2026 tp->ts_recent = opti.ts_val; 2027 } 2028 2029 /* 2030 * If the RST bit is set examine the state: 2031 * SYN_RECEIVED STATE: 2032 * If passive open, return to LISTEN state. 2033 * If active open, inform user that connection was refused. 2034 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 2035 * Inform user that connection was reset, and close tcb. 2036 * CLOSING, LAST_ACK, TIME_WAIT STATES 2037 * Close the tcb. 2038 */ 2039 if (tiflags & TH_RST) { 2040 if (th->th_seq != tp->last_ack_sent) 2041 goto dropafterack_ratelim; 2042 2043 switch (tp->t_state) { 2044 case TCPS_SYN_RECEIVED: 2045 so->so_error = ECONNREFUSED; 2046 goto close; 2047 2048 case TCPS_ESTABLISHED: 2049 case TCPS_FIN_WAIT_1: 2050 case TCPS_FIN_WAIT_2: 2051 case TCPS_CLOSE_WAIT: 2052 so->so_error = ECONNRESET; 2053 close: 2054 tp->t_state = TCPS_CLOSED; 2055 tcpstat.tcps_drops++; 2056 tp = tcp_close(tp); 2057 goto drop; 2058 2059 case TCPS_CLOSING: 2060 case TCPS_LAST_ACK: 2061 case TCPS_TIME_WAIT: 2062 tp = tcp_close(tp); 2063 goto drop; 2064 } 2065 } 2066 2067 /* 2068 * Since we've covered the SYN-SENT and SYN-RECEIVED states above 2069 * we must be in a synchronized state. RFC791 states (under RST 2070 * generation) that any unacceptable segment (an out-of-order SYN 2071 * qualifies) received in a synchronized state must elicit only an 2072 * empty acknowledgment segment ... and the connection remains in 2073 * the same state. 2074 */ 2075 if (tiflags & TH_SYN) { 2076 if (tp->rcv_nxt == th->th_seq) { 2077 tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack - 1, 2078 TH_ACK); 2079 if (tcp_saveti) 2080 m_freem(tcp_saveti); 2081 return; 2082 } 2083 2084 goto dropafterack_ratelim; 2085 } 2086 2087 /* 2088 * If the ACK bit is off we drop the segment and return. 2089 */ 2090 if ((tiflags & TH_ACK) == 0) { 2091 if (tp->t_flags & TF_ACKNOW) 2092 goto dropafterack; 2093 else 2094 goto drop; 2095 } 2096 2097 /* 2098 * Ack processing. 2099 */ 2100 switch (tp->t_state) { 2101 2102 /* 2103 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 2104 * ESTABLISHED state and continue processing, otherwise 2105 * send an RST. 2106 */ 2107 case TCPS_SYN_RECEIVED: 2108 if (SEQ_GT(tp->snd_una, th->th_ack) || 2109 SEQ_GT(th->th_ack, tp->snd_max)) 2110 goto dropwithreset; 2111 tcpstat.tcps_connects++; 2112 soisconnected(so); 2113 tcp_established(tp); 2114 /* Do window scaling? */ 2115 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2116 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2117 tp->snd_scale = tp->requested_s_scale; 2118 tp->rcv_scale = tp->request_r_scale; 2119 } 2120 TCP_REASS_LOCK(tp); 2121 (void) tcp_reass(tp, NULL, (struct mbuf *)0, &tlen); 2122 TCP_REASS_UNLOCK(tp); 2123 tp->snd_wl1 = th->th_seq - 1; 2124 /* fall into ... */ 2125 2126 /* 2127 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2128 * ACKs. If the ack is in the range 2129 * tp->snd_una < th->th_ack <= tp->snd_max 2130 * then advance tp->snd_una to th->th_ack and drop 2131 * data from the retransmission queue. If this ACK reflects 2132 * more up to date window information we update our window information. 2133 */ 2134 case TCPS_ESTABLISHED: 2135 case TCPS_FIN_WAIT_1: 2136 case TCPS_FIN_WAIT_2: 2137 case TCPS_CLOSE_WAIT: 2138 case TCPS_CLOSING: 2139 case TCPS_LAST_ACK: 2140 case TCPS_TIME_WAIT: 2141 2142 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2143 if (tlen == 0 && !dupseg && tiwin == tp->snd_wnd) { 2144 tcpstat.tcps_rcvdupack++; 2145 /* 2146 * If we have outstanding data (other than 2147 * a window probe), this is a completely 2148 * duplicate ack (ie, window info didn't 2149 * change), the ack is the biggest we've 2150 * seen and we've seen exactly our rexmt 2151 * threshhold of them, assume a packet 2152 * has been dropped and retransmit it. 2153 * Kludge snd_nxt & the congestion 2154 * window so we send only this one 2155 * packet. 2156 * 2157 * We know we're losing at the current 2158 * window size so do congestion avoidance 2159 * (set ssthresh to half the current window 2160 * and pull our congestion window back to 2161 * the new ssthresh). 2162 * 2163 * Dup acks mean that packets have left the 2164 * network (they're now cached at the receiver) 2165 * so bump cwnd by the amount in the receiver 2166 * to keep a constant cwnd packets in the 2167 * network. 2168 * 2169 * If we are using TCP/SACK, then enter 2170 * Fast Recovery if the receiver SACKs 2171 * data that is tcprexmtthresh * MSS 2172 * bytes past the last ACKed segment, 2173 * irrespective of the number of DupAcks. 2174 */ 2175 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 || 2176 th->th_ack != tp->snd_una) 2177 tp->t_dupacks = 0; 2178 else if (tp->t_partialacks < 0 && 2179 (++tp->t_dupacks == tcprexmtthresh || 2180 TCP_FACK_FASTRECOV(tp))) { 2181 tcp_seq onxt; 2182 u_int win; 2183 2184 if (tcp_do_newreno && 2185 SEQ_LT(th->th_ack, tp->snd_high)) { 2186 /* 2187 * False fast retransmit after 2188 * timeout. Do not enter fast 2189 * recovery. 2190 */ 2191 tp->t_dupacks = 0; 2192 break; 2193 } 2194 2195 onxt = tp->snd_nxt; 2196 win = min(tp->snd_wnd, tp->snd_cwnd) / 2197 2 / tp->t_segsz; 2198 if (win < 2) 2199 win = 2; 2200 tp->snd_ssthresh = win * tp->t_segsz; 2201 tp->snd_recover = tp->snd_max; 2202 tp->t_partialacks = 0; 2203 TCP_TIMER_DISARM(tp, TCPT_REXMT); 2204 tp->t_rtttime = 0; 2205 if (TCP_SACK_ENABLED(tp)) { 2206 tp->t_dupacks = tcprexmtthresh; 2207 tp->sack_newdata = tp->snd_nxt; 2208 tp->snd_cwnd = tp->t_segsz; 2209 (void) tcp_output(tp); 2210 goto drop; 2211 } 2212 tp->snd_nxt = th->th_ack; 2213 tp->snd_cwnd = tp->t_segsz; 2214 (void) tcp_output(tp); 2215 tp->snd_cwnd = tp->snd_ssthresh + 2216 tp->t_segsz * tp->t_dupacks; 2217 if (SEQ_GT(onxt, tp->snd_nxt)) 2218 tp->snd_nxt = onxt; 2219 goto drop; 2220 } else if (tp->t_dupacks > tcprexmtthresh) { 2221 tp->snd_cwnd += tp->t_segsz; 2222 (void) tcp_output(tp); 2223 goto drop; 2224 } 2225 } else { 2226 /* 2227 * If the ack appears to be very old, only 2228 * allow data that is in-sequence. This 2229 * makes it somewhat more difficult to insert 2230 * forged data by guessing sequence numbers. 2231 * Sent an ack to try to update the send 2232 * sequence number on the other side. 2233 */ 2234 if (tlen && th->th_seq != tp->rcv_nxt && 2235 SEQ_LT(th->th_ack, 2236 tp->snd_una - tp->max_sndwnd)) 2237 goto dropafterack; 2238 } 2239 break; 2240 } 2241 /* 2242 * If the congestion window was inflated to account 2243 * for the other side's cached packets, retract it. 2244 */ 2245 if (TCP_SACK_ENABLED(tp)) 2246 tcp_sack_newack(tp, th); 2247 else if (tcp_do_newreno) 2248 tcp_newreno_newack(tp, th); 2249 else 2250 tcp_reno_newack(tp, th); 2251 if (SEQ_GT(th->th_ack, tp->snd_max)) { 2252 tcpstat.tcps_rcvacktoomuch++; 2253 goto dropafterack; 2254 } 2255 acked = th->th_ack - tp->snd_una; 2256 tcpstat.tcps_rcvackpack++; 2257 tcpstat.tcps_rcvackbyte += acked; 2258 2259 /* 2260 * If we have a timestamp reply, update smoothed 2261 * round trip time. If no timestamp is present but 2262 * transmit timer is running and timed sequence 2263 * number was acked, update smoothed round trip time. 2264 * Since we now have an rtt measurement, cancel the 2265 * timer backoff (cf., Phil Karn's retransmit alg.). 2266 * Recompute the initial retransmit timer. 2267 */ 2268 if (ts_rtt) 2269 tcp_xmit_timer(tp, ts_rtt); 2270 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 2271 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 2272 2273 /* 2274 * If all outstanding data is acked, stop retransmit 2275 * timer and remember to restart (more output or persist). 2276 * If there is more data to be acked, restart retransmit 2277 * timer, using current (possibly backed-off) value. 2278 */ 2279 if (th->th_ack == tp->snd_max) { 2280 TCP_TIMER_DISARM(tp, TCPT_REXMT); 2281 needoutput = 1; 2282 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 2283 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 2284 /* 2285 * When new data is acked, open the congestion window. 2286 * If the window gives us less than ssthresh packets 2287 * in flight, open exponentially (segsz per packet). 2288 * Otherwise open linearly: segsz per window 2289 * (segsz^2 / cwnd per packet). 2290 * 2291 * If we are still in fast recovery (meaning we are using 2292 * NewReno and we have only received partial acks), do not 2293 * inflate the window yet. 2294 */ 2295 if (tp->t_partialacks < 0) { 2296 u_int cw = tp->snd_cwnd; 2297 u_int incr = tp->t_segsz; 2298 2299 if (cw >= tp->snd_ssthresh) 2300 incr = incr * incr / cw; 2301 tp->snd_cwnd = min(cw + incr, 2302 TCP_MAXWIN << tp->snd_scale); 2303 } 2304 ND6_HINT(tp); 2305 if (acked > so->so_snd.sb_cc) { 2306 tp->snd_wnd -= so->so_snd.sb_cc; 2307 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 2308 ourfinisacked = 1; 2309 } else { 2310 if (acked > (tp->t_lastoff - tp->t_inoff)) 2311 tp->t_lastm = NULL; 2312 sbdrop(&so->so_snd, acked); 2313 tp->t_lastoff -= acked; 2314 tp->snd_wnd -= acked; 2315 ourfinisacked = 0; 2316 } 2317 sowwakeup(so); 2318 2319 ICMP_CHECK(tp, th, acked); 2320 2321 tp->snd_una = th->th_ack; 2322 if (SEQ_GT(tp->snd_una, tp->snd_fack)) 2323 tp->snd_fack = tp->snd_una; 2324 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2325 tp->snd_nxt = tp->snd_una; 2326 if (SEQ_LT(tp->snd_high, tp->snd_una)) 2327 tp->snd_high = tp->snd_una; 2328 2329 switch (tp->t_state) { 2330 2331 /* 2332 * In FIN_WAIT_1 STATE in addition to the processing 2333 * for the ESTABLISHED state if our FIN is now acknowledged 2334 * then enter FIN_WAIT_2. 2335 */ 2336 case TCPS_FIN_WAIT_1: 2337 if (ourfinisacked) { 2338 /* 2339 * If we can't receive any more 2340 * data, then closing user can proceed. 2341 * Starting the timer is contrary to the 2342 * specification, but if we don't get a FIN 2343 * we'll hang forever. 2344 */ 2345 if (so->so_state & SS_CANTRCVMORE) { 2346 soisdisconnected(so); 2347 if (tcp_maxidle > 0) 2348 TCP_TIMER_ARM(tp, TCPT_2MSL, 2349 tcp_maxidle); 2350 } 2351 tp->t_state = TCPS_FIN_WAIT_2; 2352 } 2353 break; 2354 2355 /* 2356 * In CLOSING STATE in addition to the processing for 2357 * the ESTABLISHED state if the ACK acknowledges our FIN 2358 * then enter the TIME-WAIT state, otherwise ignore 2359 * the segment. 2360 */ 2361 case TCPS_CLOSING: 2362 if (ourfinisacked) { 2363 tp->t_state = TCPS_TIME_WAIT; 2364 tcp_canceltimers(tp); 2365 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2366 soisdisconnected(so); 2367 } 2368 break; 2369 2370 /* 2371 * In LAST_ACK, we may still be waiting for data to drain 2372 * and/or to be acked, as well as for the ack of our FIN. 2373 * If our FIN is now acknowledged, delete the TCB, 2374 * enter the closed state and return. 2375 */ 2376 case TCPS_LAST_ACK: 2377 if (ourfinisacked) { 2378 tp = tcp_close(tp); 2379 goto drop; 2380 } 2381 break; 2382 2383 /* 2384 * In TIME_WAIT state the only thing that should arrive 2385 * is a retransmission of the remote FIN. Acknowledge 2386 * it and restart the finack timer. 2387 */ 2388 case TCPS_TIME_WAIT: 2389 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2390 goto dropafterack; 2391 } 2392 } 2393 2394 step6: 2395 /* 2396 * Update window information. 2397 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2398 */ 2399 if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) || 2400 (tp->snd_wl1 == th->th_seq && SEQ_LT(tp->snd_wl2, th->th_ack)) || 2401 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))) { 2402 /* keep track of pure window updates */ 2403 if (tlen == 0 && 2404 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2405 tcpstat.tcps_rcvwinupd++; 2406 tp->snd_wnd = tiwin; 2407 tp->snd_wl1 = th->th_seq; 2408 tp->snd_wl2 = th->th_ack; 2409 if (tp->snd_wnd > tp->max_sndwnd) 2410 tp->max_sndwnd = tp->snd_wnd; 2411 needoutput = 1; 2412 } 2413 2414 /* 2415 * Process segments with URG. 2416 */ 2417 if ((tiflags & TH_URG) && th->th_urp && 2418 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2419 /* 2420 * This is a kludge, but if we receive and accept 2421 * random urgent pointers, we'll crash in 2422 * soreceive. It's hard to imagine someone 2423 * actually wanting to send this much urgent data. 2424 */ 2425 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2426 th->th_urp = 0; /* XXX */ 2427 tiflags &= ~TH_URG; /* XXX */ 2428 goto dodata; /* XXX */ 2429 } 2430 /* 2431 * If this segment advances the known urgent pointer, 2432 * then mark the data stream. This should not happen 2433 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2434 * a FIN has been received from the remote side. 2435 * In these states we ignore the URG. 2436 * 2437 * According to RFC961 (Assigned Protocols), 2438 * the urgent pointer points to the last octet 2439 * of urgent data. We continue, however, 2440 * to consider it to indicate the first octet 2441 * of data past the urgent section as the original 2442 * spec states (in one of two places). 2443 */ 2444 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2445 tp->rcv_up = th->th_seq + th->th_urp; 2446 so->so_oobmark = so->so_rcv.sb_cc + 2447 (tp->rcv_up - tp->rcv_nxt) - 1; 2448 if (so->so_oobmark == 0) 2449 so->so_state |= SS_RCVATMARK; 2450 sohasoutofband(so); 2451 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2452 } 2453 /* 2454 * Remove out of band data so doesn't get presented to user. 2455 * This can happen independent of advancing the URG pointer, 2456 * but if two URG's are pending at once, some out-of-band 2457 * data may creep in... ick. 2458 */ 2459 if (th->th_urp <= (u_int16_t) tlen 2460 #ifdef SO_OOBINLINE 2461 && (so->so_options & SO_OOBINLINE) == 0 2462 #endif 2463 ) 2464 tcp_pulloutofband(so, th, m, hdroptlen); 2465 } else 2466 /* 2467 * If no out of band data is expected, 2468 * pull receive urgent pointer along 2469 * with the receive window. 2470 */ 2471 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2472 tp->rcv_up = tp->rcv_nxt; 2473 dodata: /* XXX */ 2474 2475 /* 2476 * Process the segment text, merging it into the TCP sequencing queue, 2477 * and arranging for acknowledgement of receipt if necessary. 2478 * This process logically involves adjusting tp->rcv_wnd as data 2479 * is presented to the user (this happens in tcp_usrreq.c, 2480 * case PRU_RCVD). If a FIN has already been received on this 2481 * connection then we just ignore the text. 2482 */ 2483 if ((tlen || (tiflags & TH_FIN)) && 2484 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2485 /* 2486 * Insert segment ti into reassembly queue of tcp with 2487 * control block tp. Return TH_FIN if reassembly now includes 2488 * a segment with FIN. The macro form does the common case 2489 * inline (segment is the next to be received on an 2490 * established connection, and the queue is empty), 2491 * avoiding linkage into and removal from the queue and 2492 * repetition of various conversions. 2493 * Set DELACK for segments received in order, but ack 2494 * immediately when segments are out of order 2495 * (so fast retransmit can work). 2496 */ 2497 /* NOTE: this was TCP_REASS() macro, but used only once */ 2498 TCP_REASS_LOCK(tp); 2499 if (th->th_seq == tp->rcv_nxt && 2500 TAILQ_FIRST(&tp->segq) == NULL && 2501 tp->t_state == TCPS_ESTABLISHED) { 2502 TCP_SETUP_ACK(tp, th); 2503 tp->rcv_nxt += tlen; 2504 tiflags = th->th_flags & TH_FIN; 2505 tcpstat.tcps_rcvpack++; 2506 tcpstat.tcps_rcvbyte += tlen; 2507 ND6_HINT(tp); 2508 if (so->so_state & SS_CANTRCVMORE) 2509 m_freem(m); 2510 else { 2511 m_adj(m, hdroptlen); 2512 sbappendstream(&(so)->so_rcv, m); 2513 } 2514 sorwakeup(so); 2515 } else { 2516 m_adj(m, hdroptlen); 2517 tiflags = tcp_reass(tp, th, m, &tlen); 2518 tp->t_flags |= TF_ACKNOW; 2519 } 2520 TCP_REASS_UNLOCK(tp); 2521 2522 /* 2523 * Note the amount of data that peer has sent into 2524 * our window, in order to estimate the sender's 2525 * buffer size. 2526 */ 2527 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2528 } else { 2529 m_freem(m); 2530 m = NULL; 2531 tiflags &= ~TH_FIN; 2532 } 2533 2534 /* 2535 * If FIN is received ACK the FIN and let the user know 2536 * that the connection is closing. Ignore a FIN received before 2537 * the connection is fully established. 2538 */ 2539 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) { 2540 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2541 socantrcvmore(so); 2542 tp->t_flags |= TF_ACKNOW; 2543 tp->rcv_nxt++; 2544 } 2545 switch (tp->t_state) { 2546 2547 /* 2548 * In ESTABLISHED STATE enter the CLOSE_WAIT state. 2549 */ 2550 case TCPS_ESTABLISHED: 2551 tp->t_state = TCPS_CLOSE_WAIT; 2552 break; 2553 2554 /* 2555 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2556 * enter the CLOSING state. 2557 */ 2558 case TCPS_FIN_WAIT_1: 2559 tp->t_state = TCPS_CLOSING; 2560 break; 2561 2562 /* 2563 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2564 * starting the time-wait timer, turning off the other 2565 * standard timers. 2566 */ 2567 case TCPS_FIN_WAIT_2: 2568 tp->t_state = TCPS_TIME_WAIT; 2569 tcp_canceltimers(tp); 2570 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2571 soisdisconnected(so); 2572 break; 2573 2574 /* 2575 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2576 */ 2577 case TCPS_TIME_WAIT: 2578 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2579 break; 2580 } 2581 } 2582 #ifdef TCP_DEBUG 2583 if (so->so_options & SO_DEBUG) 2584 tcp_trace(TA_INPUT, ostate, tp, tcp_saveti, 0); 2585 #endif 2586 2587 /* 2588 * Return any desired output. 2589 */ 2590 if (needoutput || (tp->t_flags & TF_ACKNOW)) { 2591 (void) tcp_output(tp); 2592 } 2593 if (tcp_saveti) 2594 m_freem(tcp_saveti); 2595 return; 2596 2597 badsyn: 2598 /* 2599 * Received a bad SYN. Increment counters and dropwithreset. 2600 */ 2601 tcpstat.tcps_badsyn++; 2602 tp = NULL; 2603 goto dropwithreset; 2604 2605 dropafterack: 2606 /* 2607 * Generate an ACK dropping incoming segment if it occupies 2608 * sequence space, where the ACK reflects our state. 2609 */ 2610 if (tiflags & TH_RST) 2611 goto drop; 2612 goto dropafterack2; 2613 2614 dropafterack_ratelim: 2615 /* 2616 * We may want to rate-limit ACKs against SYN/RST attack. 2617 */ 2618 if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count, 2619 tcp_ackdrop_ppslim) == 0) { 2620 /* XXX stat */ 2621 goto drop; 2622 } 2623 /* ...fall into dropafterack2... */ 2624 2625 dropafterack2: 2626 m_freem(m); 2627 tp->t_flags |= TF_ACKNOW; 2628 (void) tcp_output(tp); 2629 if (tcp_saveti) 2630 m_freem(tcp_saveti); 2631 return; 2632 2633 dropwithreset_ratelim: 2634 /* 2635 * We may want to rate-limit RSTs in certain situations, 2636 * particularly if we are sending an RST in response to 2637 * an attempt to connect to or otherwise communicate with 2638 * a port for which we have no socket. 2639 */ 2640 if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count, 2641 tcp_rst_ppslim) == 0) { 2642 /* XXX stat */ 2643 goto drop; 2644 } 2645 /* ...fall into dropwithreset... */ 2646 2647 dropwithreset: 2648 /* 2649 * Generate a RST, dropping incoming segment. 2650 * Make ACK acceptable to originator of segment. 2651 */ 2652 if (tiflags & TH_RST) 2653 goto drop; 2654 2655 switch (af) { 2656 #ifdef INET6 2657 case AF_INET6: 2658 /* For following calls to tcp_respond */ 2659 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 2660 goto drop; 2661 break; 2662 #endif /* INET6 */ 2663 case AF_INET: 2664 if (IN_MULTICAST(ip->ip_dst.s_addr) || 2665 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2666 goto drop; 2667 } 2668 2669 if (tiflags & TH_ACK) 2670 (void)tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack, TH_RST); 2671 else { 2672 if (tiflags & TH_SYN) 2673 tlen++; 2674 (void)tcp_respond(tp, m, m, th, th->th_seq + tlen, (tcp_seq)0, 2675 TH_RST|TH_ACK); 2676 } 2677 if (tcp_saveti) 2678 m_freem(tcp_saveti); 2679 return; 2680 2681 badcsum: 2682 drop: 2683 /* 2684 * Drop space held by incoming segment and return. 2685 */ 2686 if (tp) { 2687 if (tp->t_inpcb) 2688 so = tp->t_inpcb->inp_socket; 2689 #ifdef INET6 2690 else if (tp->t_in6pcb) 2691 so = tp->t_in6pcb->in6p_socket; 2692 #endif 2693 else 2694 so = NULL; 2695 #ifdef TCP_DEBUG 2696 if (so && (so->so_options & SO_DEBUG) != 0) 2697 tcp_trace(TA_DROP, ostate, tp, tcp_saveti, 0); 2698 #endif 2699 } 2700 if (tcp_saveti) 2701 m_freem(tcp_saveti); 2702 m_freem(m); 2703 return; 2704 } 2705 2706 #ifdef TCP_SIGNATURE 2707 int 2708 tcp_signature_apply(void *fstate, caddr_t data, u_int len) 2709 { 2710 2711 MD5Update(fstate, (u_char *)data, len); 2712 return (0); 2713 } 2714 2715 struct secasvar * 2716 tcp_signature_getsav(struct mbuf *m, struct tcphdr *th) 2717 { 2718 struct secasvar *sav; 2719 #ifdef FAST_IPSEC 2720 union sockaddr_union dst; 2721 #endif 2722 struct ip *ip; 2723 struct ip6_hdr *ip6; 2724 2725 ip = mtod(m, struct ip *); 2726 switch (ip->ip_v) { 2727 case 4: 2728 ip = mtod(m, struct ip *); 2729 ip6 = NULL; 2730 break; 2731 case 6: 2732 ip = NULL; 2733 ip6 = mtod(m, struct ip6_hdr *); 2734 break; 2735 default: 2736 return (NULL); 2737 } 2738 2739 #ifdef FAST_IPSEC 2740 /* Extract the destination from the IP header in the mbuf. */ 2741 bzero(&dst, sizeof(union sockaddr_union)); 2742 dst.sa.sa_len = sizeof(struct sockaddr_in); 2743 dst.sa.sa_family = AF_INET; 2744 dst.sin.sin_addr = ip->ip_dst; 2745 2746 /* 2747 * Look up an SADB entry which matches the address of the peer. 2748 */ 2749 sav = KEY_ALLOCSA(&dst, IPPROTO_TCP, htonl(TCP_SIG_SPI)); 2750 #else 2751 if (ip) 2752 sav = key_allocsa(AF_INET, (caddr_t)&ip->ip_src, 2753 (caddr_t)&ip->ip_dst, IPPROTO_TCP, 2754 htonl(TCP_SIG_SPI), 0, 0); 2755 else 2756 sav = key_allocsa(AF_INET6, (caddr_t)&ip6->ip6_src, 2757 (caddr_t)&ip6->ip6_dst, IPPROTO_TCP, 2758 htonl(TCP_SIG_SPI), 0, 0); 2759 #endif 2760 2761 return (sav); /* freesav must be performed by caller */ 2762 } 2763 2764 int 2765 tcp_signature(struct mbuf *m, struct tcphdr *th, int thoff, 2766 struct secasvar *sav, char *sig) 2767 { 2768 MD5_CTX ctx; 2769 struct ip *ip; 2770 struct ipovly *ipovly; 2771 struct ip6_hdr *ip6; 2772 struct ippseudo ippseudo; 2773 struct ip6_hdr_pseudo ip6pseudo; 2774 struct tcphdr th0; 2775 int l, tcphdrlen; 2776 2777 if (sav == NULL) 2778 return (-1); 2779 2780 tcphdrlen = th->th_off * 4; 2781 2782 switch (mtod(m, struct ip *)->ip_v) { 2783 case 4: 2784 ip = mtod(m, struct ip *); 2785 ip6 = NULL; 2786 break; 2787 case 6: 2788 ip = NULL; 2789 ip6 = mtod(m, struct ip6_hdr *); 2790 break; 2791 default: 2792 return (-1); 2793 } 2794 2795 MD5Init(&ctx); 2796 2797 if (ip) { 2798 memset(&ippseudo, 0, sizeof(ippseudo)); 2799 ipovly = (struct ipovly *)ip; 2800 ippseudo.ippseudo_src = ipovly->ih_src; 2801 ippseudo.ippseudo_dst = ipovly->ih_dst; 2802 ippseudo.ippseudo_pad = 0; 2803 ippseudo.ippseudo_p = IPPROTO_TCP; 2804 ippseudo.ippseudo_len = htons(m->m_pkthdr.len - thoff); 2805 MD5Update(&ctx, (char *)&ippseudo, sizeof(ippseudo)); 2806 } else { 2807 memset(&ip6pseudo, 0, sizeof(ip6pseudo)); 2808 ip6pseudo.ip6ph_src = ip6->ip6_src; 2809 in6_clearscope(&ip6pseudo.ip6ph_src); 2810 ip6pseudo.ip6ph_dst = ip6->ip6_dst; 2811 in6_clearscope(&ip6pseudo.ip6ph_dst); 2812 ip6pseudo.ip6ph_len = htons(m->m_pkthdr.len - thoff); 2813 ip6pseudo.ip6ph_nxt = IPPROTO_TCP; 2814 MD5Update(&ctx, (char *)&ip6pseudo, sizeof(ip6pseudo)); 2815 } 2816 2817 th0 = *th; 2818 th0.th_sum = 0; 2819 MD5Update(&ctx, (char *)&th0, sizeof(th0)); 2820 2821 l = m->m_pkthdr.len - thoff - tcphdrlen; 2822 if (l > 0) 2823 m_apply(m, thoff + tcphdrlen, 2824 m->m_pkthdr.len - thoff - tcphdrlen, 2825 tcp_signature_apply, &ctx); 2826 2827 MD5Update(&ctx, _KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth)); 2828 MD5Final(sig, &ctx); 2829 2830 return (0); 2831 } 2832 #endif 2833 2834 int 2835 tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcphdr *th, 2836 struct mbuf *m, int toff, struct tcp_opt_info *oi) 2837 { 2838 u_int16_t mss; 2839 int opt, optlen = 0; 2840 #ifdef TCP_SIGNATURE 2841 caddr_t sigp = NULL; 2842 char sigbuf[TCP_SIGLEN]; 2843 struct secasvar *sav = NULL; 2844 #endif 2845 2846 for (; cp && cnt > 0; cnt -= optlen, cp += optlen) { 2847 opt = cp[0]; 2848 if (opt == TCPOPT_EOL) 2849 break; 2850 if (opt == TCPOPT_NOP) 2851 optlen = 1; 2852 else { 2853 if (cnt < 2) 2854 break; 2855 optlen = cp[1]; 2856 if (optlen < 2 || optlen > cnt) 2857 break; 2858 } 2859 switch (opt) { 2860 2861 default: 2862 continue; 2863 2864 case TCPOPT_MAXSEG: 2865 if (optlen != TCPOLEN_MAXSEG) 2866 continue; 2867 if (!(th->th_flags & TH_SYN)) 2868 continue; 2869 bcopy(cp + 2, &mss, sizeof(mss)); 2870 oi->maxseg = ntohs(mss); 2871 break; 2872 2873 case TCPOPT_WINDOW: 2874 if (optlen != TCPOLEN_WINDOW) 2875 continue; 2876 if (!(th->th_flags & TH_SYN)) 2877 continue; 2878 tp->t_flags |= TF_RCVD_SCALE; 2879 tp->requested_s_scale = cp[2]; 2880 if (tp->requested_s_scale > TCP_MAX_WINSHIFT) { 2881 #if 0 /*XXX*/ 2882 char *p; 2883 2884 if (ip) 2885 p = ntohl(ip->ip_src); 2886 #ifdef INET6 2887 else if (ip6) 2888 p = ip6_sprintf(&ip6->ip6_src); 2889 #endif 2890 else 2891 p = "(unknown)"; 2892 log(LOG_ERR, "TCP: invalid wscale %d from %s, " 2893 "assuming %d\n", 2894 tp->requested_s_scale, p, 2895 TCP_MAX_WINSHIFT); 2896 #else 2897 log(LOG_ERR, "TCP: invalid wscale %d, " 2898 "assuming %d\n", 2899 tp->requested_s_scale, 2900 TCP_MAX_WINSHIFT); 2901 #endif 2902 tp->requested_s_scale = TCP_MAX_WINSHIFT; 2903 } 2904 break; 2905 2906 case TCPOPT_TIMESTAMP: 2907 if (optlen != TCPOLEN_TIMESTAMP) 2908 continue; 2909 oi->ts_present = 1; 2910 bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val)); 2911 NTOHL(oi->ts_val); 2912 bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr)); 2913 NTOHL(oi->ts_ecr); 2914 2915 /* 2916 * A timestamp received in a SYN makes 2917 * it ok to send timestamp requests and replies. 2918 */ 2919 if (th->th_flags & TH_SYN) { 2920 tp->t_flags |= TF_RCVD_TSTMP; 2921 tp->ts_recent = oi->ts_val; 2922 tp->ts_recent_age = tcp_now; 2923 } 2924 break; 2925 2926 case TCPOPT_SACK_PERMITTED: 2927 if (optlen != TCPOLEN_SACK_PERMITTED) 2928 continue; 2929 if (!(th->th_flags & TH_SYN)) 2930 continue; 2931 if (tcp_do_sack) { 2932 tp->t_flags |= TF_SACK_PERMIT; 2933 tp->t_flags |= TF_WILL_SACK; 2934 } 2935 break; 2936 2937 case TCPOPT_SACK: 2938 tcp_sack_option(tp, th, cp, optlen); 2939 break; 2940 #ifdef TCP_SIGNATURE 2941 case TCPOPT_SIGNATURE: 2942 if (optlen != TCPOLEN_SIGNATURE) 2943 continue; 2944 if (sigp && bcmp(sigp, cp + 2, TCP_SIGLEN)) 2945 return (-1); 2946 2947 sigp = sigbuf; 2948 memcpy(sigbuf, cp + 2, TCP_SIGLEN); 2949 memset(cp + 2, 0, TCP_SIGLEN); 2950 tp->t_flags |= TF_SIGNATURE; 2951 break; 2952 #endif 2953 } 2954 } 2955 2956 #ifdef TCP_SIGNATURE 2957 if (tp->t_flags & TF_SIGNATURE) { 2958 2959 sav = tcp_signature_getsav(m, th); 2960 2961 if (sav == NULL && tp->t_state == TCPS_LISTEN) 2962 return (-1); 2963 } 2964 2965 if ((sigp ? TF_SIGNATURE : 0) ^ (tp->t_flags & TF_SIGNATURE)) { 2966 if (sav == NULL) 2967 return (-1); 2968 #ifdef FAST_IPSEC 2969 KEY_FREESAV(&sav); 2970 #else 2971 key_freesav(sav); 2972 #endif 2973 return (-1); 2974 } 2975 2976 if (sigp) { 2977 char sig[TCP_SIGLEN]; 2978 2979 TCP_FIELDS_TO_NET(th); 2980 if (tcp_signature(m, th, toff, sav, sig) < 0) { 2981 TCP_FIELDS_TO_HOST(th); 2982 if (sav == NULL) 2983 return (-1); 2984 #ifdef FAST_IPSEC 2985 KEY_FREESAV(&sav); 2986 #else 2987 key_freesav(sav); 2988 #endif 2989 return (-1); 2990 } 2991 TCP_FIELDS_TO_HOST(th); 2992 2993 if (bcmp(sig, sigp, TCP_SIGLEN)) { 2994 tcpstat.tcps_badsig++; 2995 if (sav == NULL) 2996 return (-1); 2997 #ifdef FAST_IPSEC 2998 KEY_FREESAV(&sav); 2999 #else 3000 key_freesav(sav); 3001 #endif 3002 return (-1); 3003 } else 3004 tcpstat.tcps_goodsig++; 3005 3006 key_sa_recordxfer(sav, m); 3007 #ifdef FAST_IPSEC 3008 KEY_FREESAV(&sav); 3009 #else 3010 key_freesav(sav); 3011 #endif 3012 } 3013 #endif 3014 3015 return (0); 3016 } 3017 3018 /* 3019 * Pull out of band byte out of a segment so 3020 * it doesn't appear in the user's data queue. 3021 * It is still reflected in the segment length for 3022 * sequencing purposes. 3023 */ 3024 void 3025 tcp_pulloutofband(struct socket *so, struct tcphdr *th, 3026 struct mbuf *m, int off) 3027 { 3028 int cnt = off + th->th_urp - 1; 3029 3030 while (cnt >= 0) { 3031 if (m->m_len > cnt) { 3032 char *cp = mtod(m, caddr_t) + cnt; 3033 struct tcpcb *tp = sototcpcb(so); 3034 3035 tp->t_iobc = *cp; 3036 tp->t_oobflags |= TCPOOB_HAVEDATA; 3037 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3038 m->m_len--; 3039 return; 3040 } 3041 cnt -= m->m_len; 3042 m = m->m_next; 3043 if (m == 0) 3044 break; 3045 } 3046 panic("tcp_pulloutofband"); 3047 } 3048 3049 /* 3050 * Collect new round-trip time estimate 3051 * and update averages and current timeout. 3052 */ 3053 void 3054 tcp_xmit_timer(struct tcpcb *tp, uint32_t rtt) 3055 { 3056 int32_t delta; 3057 3058 tcpstat.tcps_rttupdated++; 3059 if (tp->t_srtt != 0) { 3060 /* 3061 * srtt is stored as fixed point with 3 bits after the 3062 * binary point (i.e., scaled by 8). The following magic 3063 * is equivalent to the smoothing algorithm in rfc793 with 3064 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3065 * point). Adjust rtt to origin 0. 3066 */ 3067 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT); 3068 if ((tp->t_srtt += delta) <= 0) 3069 tp->t_srtt = 1 << 2; 3070 /* 3071 * We accumulate a smoothed rtt variance (actually, a 3072 * smoothed mean difference), then set the retransmit 3073 * timer to smoothed rtt + 4 times the smoothed variance. 3074 * rttvar is stored as fixed point with 2 bits after the 3075 * binary point (scaled by 4). The following is 3076 * equivalent to rfc793 smoothing with an alpha of .75 3077 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3078 * rfc793's wired-in beta. 3079 */ 3080 if (delta < 0) 3081 delta = -delta; 3082 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 3083 if ((tp->t_rttvar += delta) <= 0) 3084 tp->t_rttvar = 1 << 2; 3085 } else { 3086 /* 3087 * No rtt measurement yet - use the unsmoothed rtt. 3088 * Set the variance to half the rtt (so our first 3089 * retransmit happens at 3*rtt). 3090 */ 3091 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2); 3092 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1); 3093 } 3094 tp->t_rtttime = 0; 3095 tp->t_rxtshift = 0; 3096 3097 /* 3098 * the retransmit should happen at rtt + 4 * rttvar. 3099 * Because of the way we do the smoothing, srtt and rttvar 3100 * will each average +1/2 tick of bias. When we compute 3101 * the retransmit timer, we want 1/2 tick of rounding and 3102 * 1 extra tick because of +-1/2 tick uncertainty in the 3103 * firing of the timer. The bias will give us exactly the 3104 * 1.5 tick we need. But, because the bias is 3105 * statistical, we have to test that we don't drop below 3106 * the minimum feasible timer (which is 2 ticks). 3107 */ 3108 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 3109 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3110 3111 /* 3112 * We received an ack for a packet that wasn't retransmitted; 3113 * it is probably safe to discard any error indications we've 3114 * received recently. This isn't quite right, but close enough 3115 * for now (a route might have failed after we sent a segment, 3116 * and the return path might not be symmetrical). 3117 */ 3118 tp->t_softerror = 0; 3119 } 3120 3121 void 3122 tcp_reno_newack(struct tcpcb *tp, struct tcphdr *th) 3123 { 3124 if (tp->t_partialacks < 0) { 3125 /* 3126 * We were not in fast recovery. Reset the duplicate ack 3127 * counter. 3128 */ 3129 tp->t_dupacks = 0; 3130 } else { 3131 /* 3132 * Clamp the congestion window to the crossover point and 3133 * exit fast recovery. 3134 */ 3135 if (tp->snd_cwnd > tp->snd_ssthresh) 3136 tp->snd_cwnd = tp->snd_ssthresh; 3137 tp->t_partialacks = -1; 3138 tp->t_dupacks = 0; 3139 } 3140 } 3141 3142 /* 3143 * Implement the NewReno response to a new ack, checking for partial acks in 3144 * fast recovery. 3145 */ 3146 void 3147 tcp_newreno_newack(struct tcpcb *tp, struct tcphdr *th) 3148 { 3149 if (tp->t_partialacks < 0) { 3150 /* 3151 * We were not in fast recovery. Reset the duplicate ack 3152 * counter. 3153 */ 3154 tp->t_dupacks = 0; 3155 } else if (SEQ_LT(th->th_ack, tp->snd_recover)) { 3156 /* 3157 * This is a partial ack. Retransmit the first unacknowledged 3158 * segment and deflate the congestion window by the amount of 3159 * acknowledged data. Do not exit fast recovery. 3160 */ 3161 tcp_seq onxt = tp->snd_nxt; 3162 u_long ocwnd = tp->snd_cwnd; 3163 3164 /* 3165 * snd_una has not yet been updated and the socket's send 3166 * buffer has not yet drained off the ACK'd data, so we 3167 * have to leave snd_una as it was to get the correct data 3168 * offset in tcp_output(). 3169 */ 3170 if (++tp->t_partialacks == 1) 3171 TCP_TIMER_DISARM(tp, TCPT_REXMT); 3172 tp->t_rtttime = 0; 3173 tp->snd_nxt = th->th_ack; 3174 /* 3175 * Set snd_cwnd to one segment beyond ACK'd offset. snd_una 3176 * is not yet updated when we're called. 3177 */ 3178 tp->snd_cwnd = tp->t_segsz + (th->th_ack - tp->snd_una); 3179 (void) tcp_output(tp); 3180 tp->snd_cwnd = ocwnd; 3181 if (SEQ_GT(onxt, tp->snd_nxt)) 3182 tp->snd_nxt = onxt; 3183 /* 3184 * Partial window deflation. Relies on fact that tp->snd_una 3185 * not updated yet. 3186 */ 3187 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_segsz); 3188 } else { 3189 /* 3190 * Complete ack. Inflate the congestion window to ssthresh 3191 * and exit fast recovery. 3192 * 3193 * Window inflation should have left us with approx. 3194 * snd_ssthresh outstanding data. But in case we 3195 * would be inclined to send a burst, better to do 3196 * it via the slow start mechanism. 3197 */ 3198 if (SEQ_SUB(tp->snd_max, th->th_ack) < tp->snd_ssthresh) 3199 tp->snd_cwnd = SEQ_SUB(tp->snd_max, th->th_ack) 3200 + tp->t_segsz; 3201 else 3202 tp->snd_cwnd = tp->snd_ssthresh; 3203 tp->t_partialacks = -1; 3204 tp->t_dupacks = 0; 3205 } 3206 } 3207 3208 3209 /* 3210 * TCP compressed state engine. Currently used to hold compressed 3211 * state for SYN_RECEIVED. 3212 */ 3213 3214 u_long syn_cache_count; 3215 u_int32_t syn_hash1, syn_hash2; 3216 3217 #define SYN_HASH(sa, sp, dp) \ 3218 ((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \ 3219 ((u_int32_t)(sp)))^syn_hash2))) 3220 #ifndef INET6 3221 #define SYN_HASHALL(hash, src, dst) \ 3222 do { \ 3223 hash = SYN_HASH(&((const struct sockaddr_in *)(src))->sin_addr, \ 3224 ((const struct sockaddr_in *)(src))->sin_port, \ 3225 ((const struct sockaddr_in *)(dst))->sin_port); \ 3226 } while (/*CONSTCOND*/ 0) 3227 #else 3228 #define SYN_HASH6(sa, sp, dp) \ 3229 ((((sa)->s6_addr32[0] ^ (sa)->s6_addr32[3] ^ syn_hash1) * \ 3230 (((((u_int32_t)(dp))<<16) + ((u_int32_t)(sp)))^syn_hash2)) \ 3231 & 0x7fffffff) 3232 3233 #define SYN_HASHALL(hash, src, dst) \ 3234 do { \ 3235 switch ((src)->sa_family) { \ 3236 case AF_INET: \ 3237 hash = SYN_HASH(&((const struct sockaddr_in *)(src))->sin_addr, \ 3238 ((const struct sockaddr_in *)(src))->sin_port, \ 3239 ((const struct sockaddr_in *)(dst))->sin_port); \ 3240 break; \ 3241 case AF_INET6: \ 3242 hash = SYN_HASH6(&((const struct sockaddr_in6 *)(src))->sin6_addr, \ 3243 ((const struct sockaddr_in6 *)(src))->sin6_port, \ 3244 ((const struct sockaddr_in6 *)(dst))->sin6_port); \ 3245 break; \ 3246 default: \ 3247 hash = 0; \ 3248 } \ 3249 } while (/*CONSTCOND*/0) 3250 #endif /* INET6 */ 3251 3252 #define SYN_CACHE_RM(sc) \ 3253 do { \ 3254 TAILQ_REMOVE(&tcp_syn_cache[(sc)->sc_bucketidx].sch_bucket, \ 3255 (sc), sc_bucketq); \ 3256 (sc)->sc_tp = NULL; \ 3257 LIST_REMOVE((sc), sc_tpq); \ 3258 tcp_syn_cache[(sc)->sc_bucketidx].sch_length--; \ 3259 callout_stop(&(sc)->sc_timer); \ 3260 syn_cache_count--; \ 3261 } while (/*CONSTCOND*/0) 3262 3263 #define SYN_CACHE_PUT(sc) \ 3264 do { \ 3265 if ((sc)->sc_ipopts) \ 3266 (void) m_free((sc)->sc_ipopts); \ 3267 if ((sc)->sc_route4.ro_rt != NULL) \ 3268 RTFREE((sc)->sc_route4.ro_rt); \ 3269 if (callout_invoking(&(sc)->sc_timer)) \ 3270 (sc)->sc_flags |= SCF_DEAD; \ 3271 else \ 3272 pool_put(&syn_cache_pool, (sc)); \ 3273 } while (/*CONSTCOND*/0) 3274 3275 POOL_INIT(syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0, "synpl", NULL); 3276 3277 /* 3278 * We don't estimate RTT with SYNs, so each packet starts with the default 3279 * RTT and each timer step has a fixed timeout value. 3280 */ 3281 #define SYN_CACHE_TIMER_ARM(sc) \ 3282 do { \ 3283 TCPT_RANGESET((sc)->sc_rxtcur, \ 3284 TCPTV_SRTTDFLT * tcp_backoff[(sc)->sc_rxtshift], TCPTV_MIN, \ 3285 TCPTV_REXMTMAX); \ 3286 callout_reset(&(sc)->sc_timer, \ 3287 (sc)->sc_rxtcur * (hz / PR_SLOWHZ), syn_cache_timer, (sc)); \ 3288 } while (/*CONSTCOND*/0) 3289 3290 #define SYN_CACHE_TIMESTAMP(sc) (tcp_now - (sc)->sc_timebase) 3291 3292 void 3293 syn_cache_init(void) 3294 { 3295 int i; 3296 3297 /* Initialize the hash buckets. */ 3298 for (i = 0; i < tcp_syn_cache_size; i++) 3299 TAILQ_INIT(&tcp_syn_cache[i].sch_bucket); 3300 } 3301 3302 void 3303 syn_cache_insert(struct syn_cache *sc, struct tcpcb *tp) 3304 { 3305 struct syn_cache_head *scp; 3306 struct syn_cache *sc2; 3307 int s; 3308 3309 /* 3310 * If there are no entries in the hash table, reinitialize 3311 * the hash secrets. 3312 */ 3313 if (syn_cache_count == 0) { 3314 syn_hash1 = arc4random(); 3315 syn_hash2 = arc4random(); 3316 } 3317 3318 SYN_HASHALL(sc->sc_hash, &sc->sc_src.sa, &sc->sc_dst.sa); 3319 sc->sc_bucketidx = sc->sc_hash % tcp_syn_cache_size; 3320 scp = &tcp_syn_cache[sc->sc_bucketidx]; 3321 3322 /* 3323 * Make sure that we don't overflow the per-bucket 3324 * limit or the total cache size limit. 3325 */ 3326 s = splsoftnet(); 3327 if (scp->sch_length >= tcp_syn_bucket_limit) { 3328 tcpstat.tcps_sc_bucketoverflow++; 3329 /* 3330 * The bucket is full. Toss the oldest element in the 3331 * bucket. This will be the first entry in the bucket. 3332 */ 3333 sc2 = TAILQ_FIRST(&scp->sch_bucket); 3334 #ifdef DIAGNOSTIC 3335 /* 3336 * This should never happen; we should always find an 3337 * entry in our bucket. 3338 */ 3339 if (sc2 == NULL) 3340 panic("syn_cache_insert: bucketoverflow: impossible"); 3341 #endif 3342 SYN_CACHE_RM(sc2); 3343 SYN_CACHE_PUT(sc2); 3344 } else if (syn_cache_count >= tcp_syn_cache_limit) { 3345 struct syn_cache_head *scp2, *sce; 3346 3347 tcpstat.tcps_sc_overflowed++; 3348 /* 3349 * The cache is full. Toss the oldest entry in the 3350 * first non-empty bucket we can find. 3351 * 3352 * XXX We would really like to toss the oldest 3353 * entry in the cache, but we hope that this 3354 * condition doesn't happen very often. 3355 */ 3356 scp2 = scp; 3357 if (TAILQ_EMPTY(&scp2->sch_bucket)) { 3358 sce = &tcp_syn_cache[tcp_syn_cache_size]; 3359 for (++scp2; scp2 != scp; scp2++) { 3360 if (scp2 >= sce) 3361 scp2 = &tcp_syn_cache[0]; 3362 if (! TAILQ_EMPTY(&scp2->sch_bucket)) 3363 break; 3364 } 3365 #ifdef DIAGNOSTIC 3366 /* 3367 * This should never happen; we should always find a 3368 * non-empty bucket. 3369 */ 3370 if (scp2 == scp) 3371 panic("syn_cache_insert: cacheoverflow: " 3372 "impossible"); 3373 #endif 3374 } 3375 sc2 = TAILQ_FIRST(&scp2->sch_bucket); 3376 SYN_CACHE_RM(sc2); 3377 SYN_CACHE_PUT(sc2); 3378 } 3379 3380 /* 3381 * Initialize the entry's timer. 3382 */ 3383 sc->sc_rxttot = 0; 3384 sc->sc_rxtshift = 0; 3385 SYN_CACHE_TIMER_ARM(sc); 3386 3387 /* Link it from tcpcb entry */ 3388 LIST_INSERT_HEAD(&tp->t_sc, sc, sc_tpq); 3389 3390 /* Put it into the bucket. */ 3391 TAILQ_INSERT_TAIL(&scp->sch_bucket, sc, sc_bucketq); 3392 scp->sch_length++; 3393 syn_cache_count++; 3394 3395 tcpstat.tcps_sc_added++; 3396 splx(s); 3397 } 3398 3399 /* 3400 * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted. 3401 * If we have retransmitted an entry the maximum number of times, expire 3402 * that entry. 3403 */ 3404 void 3405 syn_cache_timer(void *arg) 3406 { 3407 struct syn_cache *sc = arg; 3408 int s; 3409 3410 s = splsoftnet(); 3411 callout_ack(&sc->sc_timer); 3412 3413 if (__predict_false(sc->sc_flags & SCF_DEAD)) { 3414 tcpstat.tcps_sc_delayed_free++; 3415 pool_put(&syn_cache_pool, sc); 3416 splx(s); 3417 return; 3418 } 3419 3420 if (__predict_false(sc->sc_rxtshift == TCP_MAXRXTSHIFT)) { 3421 /* Drop it -- too many retransmissions. */ 3422 goto dropit; 3423 } 3424 3425 /* 3426 * Compute the total amount of time this entry has 3427 * been on a queue. If this entry has been on longer 3428 * than the keep alive timer would allow, expire it. 3429 */ 3430 sc->sc_rxttot += sc->sc_rxtcur; 3431 if (sc->sc_rxttot >= TCPTV_KEEP_INIT) 3432 goto dropit; 3433 3434 tcpstat.tcps_sc_retransmitted++; 3435 (void) syn_cache_respond(sc, NULL); 3436 3437 /* Advance the timer back-off. */ 3438 sc->sc_rxtshift++; 3439 SYN_CACHE_TIMER_ARM(sc); 3440 3441 splx(s); 3442 return; 3443 3444 dropit: 3445 tcpstat.tcps_sc_timed_out++; 3446 SYN_CACHE_RM(sc); 3447 SYN_CACHE_PUT(sc); 3448 splx(s); 3449 } 3450 3451 /* 3452 * Remove syn cache created by the specified tcb entry, 3453 * because this does not make sense to keep them 3454 * (if there's no tcb entry, syn cache entry will never be used) 3455 */ 3456 void 3457 syn_cache_cleanup(struct tcpcb *tp) 3458 { 3459 struct syn_cache *sc, *nsc; 3460 int s; 3461 3462 s = splsoftnet(); 3463 3464 for (sc = LIST_FIRST(&tp->t_sc); sc != NULL; sc = nsc) { 3465 nsc = LIST_NEXT(sc, sc_tpq); 3466 3467 #ifdef DIAGNOSTIC 3468 if (sc->sc_tp != tp) 3469 panic("invalid sc_tp in syn_cache_cleanup"); 3470 #endif 3471 SYN_CACHE_RM(sc); 3472 SYN_CACHE_PUT(sc); 3473 } 3474 /* just for safety */ 3475 LIST_INIT(&tp->t_sc); 3476 3477 splx(s); 3478 } 3479 3480 /* 3481 * Find an entry in the syn cache. 3482 */ 3483 struct syn_cache * 3484 syn_cache_lookup(const struct sockaddr *src, const struct sockaddr *dst, 3485 struct syn_cache_head **headp) 3486 { 3487 struct syn_cache *sc; 3488 struct syn_cache_head *scp; 3489 u_int32_t hash; 3490 int s; 3491 3492 SYN_HASHALL(hash, src, dst); 3493 3494 scp = &tcp_syn_cache[hash % tcp_syn_cache_size]; 3495 *headp = scp; 3496 s = splsoftnet(); 3497 for (sc = TAILQ_FIRST(&scp->sch_bucket); sc != NULL; 3498 sc = TAILQ_NEXT(sc, sc_bucketq)) { 3499 if (sc->sc_hash != hash) 3500 continue; 3501 if (!bcmp(&sc->sc_src, src, src->sa_len) && 3502 !bcmp(&sc->sc_dst, dst, dst->sa_len)) { 3503 splx(s); 3504 return (sc); 3505 } 3506 } 3507 splx(s); 3508 return (NULL); 3509 } 3510 3511 /* 3512 * This function gets called when we receive an ACK for a 3513 * socket in the LISTEN state. We look up the connection 3514 * in the syn cache, and if its there, we pull it out of 3515 * the cache and turn it into a full-blown connection in 3516 * the SYN-RECEIVED state. 3517 * 3518 * The return values may not be immediately obvious, and their effects 3519 * can be subtle, so here they are: 3520 * 3521 * NULL SYN was not found in cache; caller should drop the 3522 * packet and send an RST. 3523 * 3524 * -1 We were unable to create the new connection, and are 3525 * aborting it. An ACK,RST is being sent to the peer 3526 * (unless we got screwey sequence numbners; see below), 3527 * because the 3-way handshake has been completed. Caller 3528 * should not free the mbuf, since we may be using it. If 3529 * we are not, we will free it. 3530 * 3531 * Otherwise, the return value is a pointer to the new socket 3532 * associated with the connection. 3533 */ 3534 struct socket * 3535 syn_cache_get(struct sockaddr *src, struct sockaddr *dst, 3536 struct tcphdr *th, unsigned int hlen, unsigned int tlen, 3537 struct socket *so, struct mbuf *m) 3538 { 3539 struct syn_cache *sc; 3540 struct syn_cache_head *scp; 3541 struct inpcb *inp = NULL; 3542 #ifdef INET6 3543 struct in6pcb *in6p = NULL; 3544 #endif 3545 struct tcpcb *tp = 0; 3546 struct mbuf *am; 3547 int s; 3548 struct socket *oso; 3549 3550 s = splsoftnet(); 3551 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) { 3552 splx(s); 3553 return (NULL); 3554 } 3555 3556 /* 3557 * Verify the sequence and ack numbers. Try getting the correct 3558 * response again. 3559 */ 3560 if ((th->th_ack != sc->sc_iss + 1) || 3561 SEQ_LEQ(th->th_seq, sc->sc_irs) || 3562 SEQ_GT(th->th_seq, sc->sc_irs + 1 + sc->sc_win)) { 3563 (void) syn_cache_respond(sc, m); 3564 splx(s); 3565 return ((struct socket *)(-1)); 3566 } 3567 3568 /* Remove this cache entry */ 3569 SYN_CACHE_RM(sc); 3570 splx(s); 3571 3572 /* 3573 * Ok, create the full blown connection, and set things up 3574 * as they would have been set up if we had created the 3575 * connection when the SYN arrived. If we can't create 3576 * the connection, abort it. 3577 */ 3578 /* 3579 * inp still has the OLD in_pcb stuff, set the 3580 * v6-related flags on the new guy, too. This is 3581 * done particularly for the case where an AF_INET6 3582 * socket is bound only to a port, and a v4 connection 3583 * comes in on that port. 3584 * we also copy the flowinfo from the original pcb 3585 * to the new one. 3586 */ 3587 oso = so; 3588 so = sonewconn(so, SS_ISCONNECTED); 3589 if (so == NULL) 3590 goto resetandabort; 3591 3592 switch (so->so_proto->pr_domain->dom_family) { 3593 #ifdef INET 3594 case AF_INET: 3595 inp = sotoinpcb(so); 3596 break; 3597 #endif 3598 #ifdef INET6 3599 case AF_INET6: 3600 in6p = sotoin6pcb(so); 3601 break; 3602 #endif 3603 } 3604 switch (src->sa_family) { 3605 #ifdef INET 3606 case AF_INET: 3607 if (inp) { 3608 inp->inp_laddr = ((struct sockaddr_in *)dst)->sin_addr; 3609 inp->inp_lport = ((struct sockaddr_in *)dst)->sin_port; 3610 inp->inp_options = ip_srcroute(); 3611 in_pcbstate(inp, INP_BOUND); 3612 if (inp->inp_options == NULL) { 3613 inp->inp_options = sc->sc_ipopts; 3614 sc->sc_ipopts = NULL; 3615 } 3616 } 3617 #ifdef INET6 3618 else if (in6p) { 3619 /* IPv4 packet to AF_INET6 socket */ 3620 bzero(&in6p->in6p_laddr, sizeof(in6p->in6p_laddr)); 3621 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff); 3622 bcopy(&((struct sockaddr_in *)dst)->sin_addr, 3623 &in6p->in6p_laddr.s6_addr32[3], 3624 sizeof(((struct sockaddr_in *)dst)->sin_addr)); 3625 in6p->in6p_lport = ((struct sockaddr_in *)dst)->sin_port; 3626 in6totcpcb(in6p)->t_family = AF_INET; 3627 if (sotoin6pcb(oso)->in6p_flags & IN6P_IPV6_V6ONLY) 3628 in6p->in6p_flags |= IN6P_IPV6_V6ONLY; 3629 else 3630 in6p->in6p_flags &= ~IN6P_IPV6_V6ONLY; 3631 in6_pcbstate(in6p, IN6P_BOUND); 3632 } 3633 #endif 3634 break; 3635 #endif 3636 #ifdef INET6 3637 case AF_INET6: 3638 if (in6p) { 3639 in6p->in6p_laddr = ((struct sockaddr_in6 *)dst)->sin6_addr; 3640 in6p->in6p_lport = ((struct sockaddr_in6 *)dst)->sin6_port; 3641 in6_pcbstate(in6p, IN6P_BOUND); 3642 } 3643 break; 3644 #endif 3645 } 3646 #ifdef INET6 3647 if (in6p && in6totcpcb(in6p)->t_family == AF_INET6 && sotoinpcb(oso)) { 3648 struct in6pcb *oin6p = sotoin6pcb(oso); 3649 /* inherit socket options from the listening socket */ 3650 in6p->in6p_flags |= (oin6p->in6p_flags & IN6P_CONTROLOPTS); 3651 if (in6p->in6p_flags & IN6P_CONTROLOPTS) { 3652 m_freem(in6p->in6p_options); 3653 in6p->in6p_options = 0; 3654 } 3655 ip6_savecontrol(in6p, &in6p->in6p_options, 3656 mtod(m, struct ip6_hdr *), m); 3657 } 3658 #endif 3659 3660 #if defined(IPSEC) || defined(FAST_IPSEC) 3661 /* 3662 * we make a copy of policy, instead of sharing the policy, 3663 * for better behavior in terms of SA lookup and dead SA removal. 3664 */ 3665 if (inp) { 3666 /* copy old policy into new socket's */ 3667 if (ipsec_copy_pcbpolicy(sotoinpcb(oso)->inp_sp, inp->inp_sp)) 3668 printf("tcp_input: could not copy policy\n"); 3669 } 3670 #ifdef INET6 3671 else if (in6p) { 3672 /* copy old policy into new socket's */ 3673 if (ipsec_copy_pcbpolicy(sotoin6pcb(oso)->in6p_sp, 3674 in6p->in6p_sp)) 3675 printf("tcp_input: could not copy policy\n"); 3676 } 3677 #endif 3678 #endif 3679 3680 /* 3681 * Give the new socket our cached route reference. 3682 */ 3683 if (inp) 3684 inp->inp_route = sc->sc_route4; /* struct assignment */ 3685 #ifdef INET6 3686 else 3687 in6p->in6p_route = sc->sc_route6; 3688 #endif 3689 sc->sc_route4.ro_rt = NULL; 3690 3691 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 3692 if (am == NULL) 3693 goto resetandabort; 3694 MCLAIM(am, &tcp_mowner); 3695 am->m_len = src->sa_len; 3696 bcopy(src, mtod(am, caddr_t), src->sa_len); 3697 if (inp) { 3698 if (in_pcbconnect(inp, am)) { 3699 (void) m_free(am); 3700 goto resetandabort; 3701 } 3702 } 3703 #ifdef INET6 3704 else if (in6p) { 3705 if (src->sa_family == AF_INET) { 3706 /* IPv4 packet to AF_INET6 socket */ 3707 struct sockaddr_in6 *sin6; 3708 sin6 = mtod(am, struct sockaddr_in6 *); 3709 am->m_len = sizeof(*sin6); 3710 bzero(sin6, sizeof(*sin6)); 3711 sin6->sin6_family = AF_INET6; 3712 sin6->sin6_len = sizeof(*sin6); 3713 sin6->sin6_port = ((struct sockaddr_in *)src)->sin_port; 3714 sin6->sin6_addr.s6_addr16[5] = htons(0xffff); 3715 bcopy(&((struct sockaddr_in *)src)->sin_addr, 3716 &sin6->sin6_addr.s6_addr32[3], 3717 sizeof(sin6->sin6_addr.s6_addr32[3])); 3718 } 3719 if (in6_pcbconnect(in6p, am)) { 3720 (void) m_free(am); 3721 goto resetandabort; 3722 } 3723 } 3724 #endif 3725 else { 3726 (void) m_free(am); 3727 goto resetandabort; 3728 } 3729 (void) m_free(am); 3730 3731 if (inp) 3732 tp = intotcpcb(inp); 3733 #ifdef INET6 3734 else if (in6p) 3735 tp = in6totcpcb(in6p); 3736 #endif 3737 else 3738 tp = NULL; 3739 tp->t_flags = sototcpcb(oso)->t_flags & TF_NODELAY; 3740 if (sc->sc_request_r_scale != 15) { 3741 tp->requested_s_scale = sc->sc_requested_s_scale; 3742 tp->request_r_scale = sc->sc_request_r_scale; 3743 tp->snd_scale = sc->sc_requested_s_scale; 3744 tp->rcv_scale = sc->sc_request_r_scale; 3745 tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE; 3746 } 3747 if (sc->sc_flags & SCF_TIMESTAMP) 3748 tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP; 3749 tp->ts_timebase = sc->sc_timebase; 3750 3751 tp->t_template = tcp_template(tp); 3752 if (tp->t_template == 0) { 3753 tp = tcp_drop(tp, ENOBUFS); /* destroys socket */ 3754 so = NULL; 3755 m_freem(m); 3756 goto abort; 3757 } 3758 3759 tp->iss = sc->sc_iss; 3760 tp->irs = sc->sc_irs; 3761 tcp_sendseqinit(tp); 3762 tcp_rcvseqinit(tp); 3763 tp->t_state = TCPS_SYN_RECEIVED; 3764 TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT); 3765 tcpstat.tcps_accepts++; 3766 3767 if ((sc->sc_flags & SCF_SACK_PERMIT) && tcp_do_sack) 3768 tp->t_flags |= TF_WILL_SACK; 3769 3770 #ifdef TCP_SIGNATURE 3771 if (sc->sc_flags & SCF_SIGNATURE) 3772 tp->t_flags |= TF_SIGNATURE; 3773 #endif 3774 3775 /* Initialize tp->t_ourmss before we deal with the peer's! */ 3776 tp->t_ourmss = sc->sc_ourmaxseg; 3777 tcp_mss_from_peer(tp, sc->sc_peermaxseg); 3778 3779 /* 3780 * Initialize the initial congestion window. If we 3781 * had to retransmit the SYN,ACK, we must initialize cwnd 3782 * to 1 segment (i.e. the Loss Window). 3783 */ 3784 if (sc->sc_rxtshift) 3785 tp->snd_cwnd = tp->t_peermss; 3786 else { 3787 int ss = tcp_init_win; 3788 #ifdef INET 3789 if (inp != NULL && in_localaddr(inp->inp_faddr)) 3790 ss = tcp_init_win_local; 3791 #endif 3792 #ifdef INET6 3793 if (in6p != NULL && in6_localaddr(&in6p->in6p_faddr)) 3794 ss = tcp_init_win_local; 3795 #endif 3796 tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss); 3797 } 3798 3799 tcp_rmx_rtt(tp); 3800 tp->snd_wl1 = sc->sc_irs; 3801 tp->rcv_up = sc->sc_irs + 1; 3802 3803 /* 3804 * This is what whould have happened in tcp_output() when 3805 * the SYN,ACK was sent. 3806 */ 3807 tp->snd_up = tp->snd_una; 3808 tp->snd_max = tp->snd_nxt = tp->iss+1; 3809 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 3810 if (sc->sc_win > 0 && SEQ_GT(tp->rcv_nxt + sc->sc_win, tp->rcv_adv)) 3811 tp->rcv_adv = tp->rcv_nxt + sc->sc_win; 3812 tp->last_ack_sent = tp->rcv_nxt; 3813 tp->t_partialacks = -1; 3814 tp->t_dupacks = 0; 3815 3816 tcpstat.tcps_sc_completed++; 3817 SYN_CACHE_PUT(sc); 3818 return (so); 3819 3820 resetandabort: 3821 (void)tcp_respond(NULL, m, m, th, (tcp_seq)0, th->th_ack, TH_RST); 3822 abort: 3823 if (so != NULL) 3824 (void) soabort(so); 3825 SYN_CACHE_PUT(sc); 3826 tcpstat.tcps_sc_aborted++; 3827 return ((struct socket *)(-1)); 3828 } 3829 3830 /* 3831 * This function is called when we get a RST for a 3832 * non-existent connection, so that we can see if the 3833 * connection is in the syn cache. If it is, zap it. 3834 */ 3835 3836 void 3837 syn_cache_reset(struct sockaddr *src, struct sockaddr *dst, struct tcphdr *th) 3838 { 3839 struct syn_cache *sc; 3840 struct syn_cache_head *scp; 3841 int s = splsoftnet(); 3842 3843 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) { 3844 splx(s); 3845 return; 3846 } 3847 if (SEQ_LT(th->th_seq, sc->sc_irs) || 3848 SEQ_GT(th->th_seq, sc->sc_irs+1)) { 3849 splx(s); 3850 return; 3851 } 3852 SYN_CACHE_RM(sc); 3853 splx(s); 3854 tcpstat.tcps_sc_reset++; 3855 SYN_CACHE_PUT(sc); 3856 } 3857 3858 void 3859 syn_cache_unreach(const struct sockaddr *src, const struct sockaddr *dst, 3860 struct tcphdr *th) 3861 { 3862 struct syn_cache *sc; 3863 struct syn_cache_head *scp; 3864 int s; 3865 3866 s = splsoftnet(); 3867 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) { 3868 splx(s); 3869 return; 3870 } 3871 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */ 3872 if (ntohl (th->th_seq) != sc->sc_iss) { 3873 splx(s); 3874 return; 3875 } 3876 3877 /* 3878 * If we've retransmitted 3 times and this is our second error, 3879 * we remove the entry. Otherwise, we allow it to continue on. 3880 * This prevents us from incorrectly nuking an entry during a 3881 * spurious network outage. 3882 * 3883 * See tcp_notify(). 3884 */ 3885 if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtshift < 3) { 3886 sc->sc_flags |= SCF_UNREACH; 3887 splx(s); 3888 return; 3889 } 3890 3891 SYN_CACHE_RM(sc); 3892 splx(s); 3893 tcpstat.tcps_sc_unreach++; 3894 SYN_CACHE_PUT(sc); 3895 } 3896 3897 /* 3898 * Given a LISTEN socket and an inbound SYN request, add 3899 * this to the syn cache, and send back a segment: 3900 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 3901 * to the source. 3902 * 3903 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN. 3904 * Doing so would require that we hold onto the data and deliver it 3905 * to the application. However, if we are the target of a SYN-flood 3906 * DoS attack, an attacker could send data which would eventually 3907 * consume all available buffer space if it were ACKed. By not ACKing 3908 * the data, we avoid this DoS scenario. 3909 */ 3910 3911 int 3912 syn_cache_add(struct sockaddr *src, struct sockaddr *dst, struct tcphdr *th, 3913 unsigned int hlen, struct socket *so, struct mbuf *m, u_char *optp, 3914 int optlen, struct tcp_opt_info *oi) 3915 { 3916 struct tcpcb tb, *tp; 3917 long win; 3918 struct syn_cache *sc; 3919 struct syn_cache_head *scp; 3920 struct mbuf *ipopts; 3921 struct tcp_opt_info opti; 3922 3923 tp = sototcpcb(so); 3924 3925 bzero(&opti, sizeof(opti)); 3926 3927 /* 3928 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 3929 * 3930 * Note this check is performed in tcp_input() very early on. 3931 */ 3932 3933 /* 3934 * Initialize some local state. 3935 */ 3936 win = sbspace(&so->so_rcv); 3937 if (win > TCP_MAXWIN) 3938 win = TCP_MAXWIN; 3939 3940 switch (src->sa_family) { 3941 #ifdef INET 3942 case AF_INET: 3943 /* 3944 * Remember the IP options, if any. 3945 */ 3946 ipopts = ip_srcroute(); 3947 break; 3948 #endif 3949 default: 3950 ipopts = NULL; 3951 } 3952 3953 #ifdef TCP_SIGNATURE 3954 if (optp || (tp->t_flags & TF_SIGNATURE)) 3955 #else 3956 if (optp) 3957 #endif 3958 { 3959 tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0; 3960 #ifdef TCP_SIGNATURE 3961 tb.t_flags |= (tp->t_flags & TF_SIGNATURE); 3962 #endif 3963 if (tcp_dooptions(&tb, optp, optlen, th, m, m->m_pkthdr.len - 3964 sizeof(struct tcphdr) - optlen - hlen, oi) < 0) 3965 return (0); 3966 } else 3967 tb.t_flags = 0; 3968 3969 /* 3970 * See if we already have an entry for this connection. 3971 * If we do, resend the SYN,ACK. We do not count this 3972 * as a retransmission (XXX though maybe we should). 3973 */ 3974 if ((sc = syn_cache_lookup(src, dst, &scp)) != NULL) { 3975 tcpstat.tcps_sc_dupesyn++; 3976 if (ipopts) { 3977 /* 3978 * If we were remembering a previous source route, 3979 * forget it and use the new one we've been given. 3980 */ 3981 if (sc->sc_ipopts) 3982 (void) m_free(sc->sc_ipopts); 3983 sc->sc_ipopts = ipopts; 3984 } 3985 sc->sc_timestamp = tb.ts_recent; 3986 if (syn_cache_respond(sc, m) == 0) { 3987 tcpstat.tcps_sndacks++; 3988 tcpstat.tcps_sndtotal++; 3989 } 3990 return (1); 3991 } 3992 3993 sc = pool_get(&syn_cache_pool, PR_NOWAIT); 3994 if (sc == NULL) { 3995 if (ipopts) 3996 (void) m_free(ipopts); 3997 return (0); 3998 } 3999 4000 /* 4001 * Fill in the cache, and put the necessary IP and TCP 4002 * options into the reply. 4003 */ 4004 bzero(sc, sizeof(struct syn_cache)); 4005 callout_init(&sc->sc_timer); 4006 bcopy(src, &sc->sc_src, src->sa_len); 4007 bcopy(dst, &sc->sc_dst, dst->sa_len); 4008 sc->sc_flags = 0; 4009 sc->sc_ipopts = ipopts; 4010 sc->sc_irs = th->th_seq; 4011 switch (src->sa_family) { 4012 #ifdef INET 4013 case AF_INET: 4014 { 4015 struct sockaddr_in *srcin = (void *) src; 4016 struct sockaddr_in *dstin = (void *) dst; 4017 4018 sc->sc_iss = tcp_new_iss1(&dstin->sin_addr, 4019 &srcin->sin_addr, dstin->sin_port, 4020 srcin->sin_port, sizeof(dstin->sin_addr), 0); 4021 break; 4022 } 4023 #endif /* INET */ 4024 #ifdef INET6 4025 case AF_INET6: 4026 { 4027 struct sockaddr_in6 *srcin6 = (void *) src; 4028 struct sockaddr_in6 *dstin6 = (void *) dst; 4029 4030 sc->sc_iss = tcp_new_iss1(&dstin6->sin6_addr, 4031 &srcin6->sin6_addr, dstin6->sin6_port, 4032 srcin6->sin6_port, sizeof(dstin6->sin6_addr), 0); 4033 break; 4034 } 4035 #endif /* INET6 */ 4036 } 4037 sc->sc_peermaxseg = oi->maxseg; 4038 sc->sc_ourmaxseg = tcp_mss_to_advertise(m->m_flags & M_PKTHDR ? 4039 m->m_pkthdr.rcvif : NULL, 4040 sc->sc_src.sa.sa_family); 4041 sc->sc_win = win; 4042 sc->sc_timebase = tcp_now; /* see tcp_newtcpcb() */ 4043 sc->sc_timestamp = tb.ts_recent; 4044 if ((tb.t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP)) == 4045 (TF_REQ_TSTMP|TF_RCVD_TSTMP)) 4046 sc->sc_flags |= SCF_TIMESTAMP; 4047 if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 4048 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 4049 sc->sc_requested_s_scale = tb.requested_s_scale; 4050 sc->sc_request_r_scale = 0; 4051 while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT && 4052 TCP_MAXWIN << sc->sc_request_r_scale < 4053 so->so_rcv.sb_hiwat) 4054 sc->sc_request_r_scale++; 4055 } else { 4056 sc->sc_requested_s_scale = 15; 4057 sc->sc_request_r_scale = 15; 4058 } 4059 if ((tb.t_flags & TF_SACK_PERMIT) && tcp_do_sack) 4060 sc->sc_flags |= SCF_SACK_PERMIT; 4061 #ifdef TCP_SIGNATURE 4062 if (tb.t_flags & TF_SIGNATURE) 4063 sc->sc_flags |= SCF_SIGNATURE; 4064 #endif 4065 sc->sc_tp = tp; 4066 if (syn_cache_respond(sc, m) == 0) { 4067 syn_cache_insert(sc, tp); 4068 tcpstat.tcps_sndacks++; 4069 tcpstat.tcps_sndtotal++; 4070 } else { 4071 SYN_CACHE_PUT(sc); 4072 tcpstat.tcps_sc_dropped++; 4073 } 4074 return (1); 4075 } 4076 4077 int 4078 syn_cache_respond(struct syn_cache *sc, struct mbuf *m) 4079 { 4080 struct route *ro; 4081 u_int8_t *optp; 4082 int optlen, error; 4083 u_int16_t tlen; 4084 struct ip *ip = NULL; 4085 #ifdef INET6 4086 struct ip6_hdr *ip6 = NULL; 4087 #endif 4088 struct tcpcb *tp; 4089 struct tcphdr *th; 4090 u_int hlen; 4091 struct socket *so; 4092 4093 switch (sc->sc_src.sa.sa_family) { 4094 case AF_INET: 4095 hlen = sizeof(struct ip); 4096 ro = &sc->sc_route4; 4097 break; 4098 #ifdef INET6 4099 case AF_INET6: 4100 hlen = sizeof(struct ip6_hdr); 4101 ro = (struct route *)&sc->sc_route6; 4102 break; 4103 #endif 4104 default: 4105 if (m) 4106 m_freem(m); 4107 return (EAFNOSUPPORT); 4108 } 4109 4110 /* Compute the size of the TCP options. */ 4111 optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) + 4112 ((sc->sc_flags & SCF_SACK_PERMIT) ? (TCPOLEN_SACK_PERMITTED + 2) : 0) + 4113 #ifdef TCP_SIGNATURE 4114 ((sc->sc_flags & SCF_SIGNATURE) ? (TCPOLEN_SIGNATURE + 2) : 0) + 4115 #endif 4116 ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0); 4117 4118 tlen = hlen + sizeof(struct tcphdr) + optlen; 4119 4120 /* 4121 * Create the IP+TCP header from scratch. 4122 */ 4123 if (m) 4124 m_freem(m); 4125 #ifdef DIAGNOSTIC 4126 if (max_linkhdr + tlen > MCLBYTES) 4127 return (ENOBUFS); 4128 #endif 4129 MGETHDR(m, M_DONTWAIT, MT_DATA); 4130 if (m && tlen > MHLEN) { 4131 MCLGET(m, M_DONTWAIT); 4132 if ((m->m_flags & M_EXT) == 0) { 4133 m_freem(m); 4134 m = NULL; 4135 } 4136 } 4137 if (m == NULL) 4138 return (ENOBUFS); 4139 MCLAIM(m, &tcp_tx_mowner); 4140 4141 /* Fixup the mbuf. */ 4142 m->m_data += max_linkhdr; 4143 m->m_len = m->m_pkthdr.len = tlen; 4144 if (sc->sc_tp) { 4145 tp = sc->sc_tp; 4146 if (tp->t_inpcb) 4147 so = tp->t_inpcb->inp_socket; 4148 #ifdef INET6 4149 else if (tp->t_in6pcb) 4150 so = tp->t_in6pcb->in6p_socket; 4151 #endif 4152 else 4153 so = NULL; 4154 } else 4155 so = NULL; 4156 m->m_pkthdr.rcvif = NULL; 4157 memset(mtod(m, u_char *), 0, tlen); 4158 4159 switch (sc->sc_src.sa.sa_family) { 4160 case AF_INET: 4161 ip = mtod(m, struct ip *); 4162 ip->ip_v = 4; 4163 ip->ip_dst = sc->sc_src.sin.sin_addr; 4164 ip->ip_src = sc->sc_dst.sin.sin_addr; 4165 ip->ip_p = IPPROTO_TCP; 4166 th = (struct tcphdr *)(ip + 1); 4167 th->th_dport = sc->sc_src.sin.sin_port; 4168 th->th_sport = sc->sc_dst.sin.sin_port; 4169 break; 4170 #ifdef INET6 4171 case AF_INET6: 4172 ip6 = mtod(m, struct ip6_hdr *); 4173 ip6->ip6_vfc = IPV6_VERSION; 4174 ip6->ip6_dst = sc->sc_src.sin6.sin6_addr; 4175 ip6->ip6_src = sc->sc_dst.sin6.sin6_addr; 4176 ip6->ip6_nxt = IPPROTO_TCP; 4177 /* ip6_plen will be updated in ip6_output() */ 4178 th = (struct tcphdr *)(ip6 + 1); 4179 th->th_dport = sc->sc_src.sin6.sin6_port; 4180 th->th_sport = sc->sc_dst.sin6.sin6_port; 4181 break; 4182 #endif 4183 default: 4184 th = NULL; 4185 } 4186 4187 th->th_seq = htonl(sc->sc_iss); 4188 th->th_ack = htonl(sc->sc_irs + 1); 4189 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 4190 th->th_flags = TH_SYN|TH_ACK; 4191 th->th_win = htons(sc->sc_win); 4192 /* th_sum already 0 */ 4193 /* th_urp already 0 */ 4194 4195 /* Tack on the TCP options. */ 4196 optp = (u_int8_t *)(th + 1); 4197 *optp++ = TCPOPT_MAXSEG; 4198 *optp++ = 4; 4199 *optp++ = (sc->sc_ourmaxseg >> 8) & 0xff; 4200 *optp++ = sc->sc_ourmaxseg & 0xff; 4201 4202 if (sc->sc_request_r_scale != 15) { 4203 *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 | 4204 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 | 4205 sc->sc_request_r_scale); 4206 optp += 4; 4207 } 4208 4209 if (sc->sc_flags & SCF_TIMESTAMP) { 4210 u_int32_t *lp = (u_int32_t *)(optp); 4211 /* Form timestamp option as shown in appendix A of RFC 1323. */ 4212 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 4213 *lp++ = htonl(SYN_CACHE_TIMESTAMP(sc)); 4214 *lp = htonl(sc->sc_timestamp); 4215 optp += TCPOLEN_TSTAMP_APPA; 4216 } 4217 4218 if (sc->sc_flags & SCF_SACK_PERMIT) { 4219 u_int8_t *p = optp; 4220 4221 /* Let the peer know that we will SACK. */ 4222 p[0] = TCPOPT_SACK_PERMITTED; 4223 p[1] = 2; 4224 p[2] = TCPOPT_NOP; 4225 p[3] = TCPOPT_NOP; 4226 optp += 4; 4227 } 4228 4229 #ifdef TCP_SIGNATURE 4230 if (sc->sc_flags & SCF_SIGNATURE) { 4231 struct secasvar *sav; 4232 u_int8_t *sigp; 4233 4234 sav = tcp_signature_getsav(m, th); 4235 4236 if (sav == NULL) { 4237 if (m) 4238 m_freem(m); 4239 return (EPERM); 4240 } 4241 4242 *optp++ = TCPOPT_SIGNATURE; 4243 *optp++ = TCPOLEN_SIGNATURE; 4244 sigp = optp; 4245 bzero(optp, TCP_SIGLEN); 4246 optp += TCP_SIGLEN; 4247 *optp++ = TCPOPT_NOP; 4248 *optp++ = TCPOPT_EOL; 4249 4250 (void)tcp_signature(m, th, hlen, sav, sigp); 4251 4252 key_sa_recordxfer(sav, m); 4253 #ifdef FAST_IPSEC 4254 KEY_FREESAV(&sav); 4255 #else 4256 key_freesav(sav); 4257 #endif 4258 } 4259 #endif 4260 4261 /* Compute the packet's checksum. */ 4262 switch (sc->sc_src.sa.sa_family) { 4263 case AF_INET: 4264 ip->ip_len = htons(tlen - hlen); 4265 th->th_sum = 0; 4266 th->th_sum = in4_cksum(m, IPPROTO_TCP, hlen, tlen - hlen); 4267 break; 4268 #ifdef INET6 4269 case AF_INET6: 4270 ip6->ip6_plen = htons(tlen - hlen); 4271 th->th_sum = 0; 4272 th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen); 4273 break; 4274 #endif 4275 } 4276 4277 /* 4278 * Fill in some straggling IP bits. Note the stack expects 4279 * ip_len to be in host order, for convenience. 4280 */ 4281 switch (sc->sc_src.sa.sa_family) { 4282 #ifdef INET 4283 case AF_INET: 4284 ip->ip_len = htons(tlen); 4285 ip->ip_ttl = ip_defttl; 4286 /* XXX tos? */ 4287 break; 4288 #endif 4289 #ifdef INET6 4290 case AF_INET6: 4291 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 4292 ip6->ip6_vfc |= IPV6_VERSION; 4293 ip6->ip6_plen = htons(tlen - hlen); 4294 /* ip6_hlim will be initialized afterwards */ 4295 /* XXX flowlabel? */ 4296 break; 4297 #endif 4298 } 4299 4300 /* XXX use IPsec policy on listening socket, on SYN ACK */ 4301 tp = sc->sc_tp; 4302 4303 switch (sc->sc_src.sa.sa_family) { 4304 #ifdef INET 4305 case AF_INET: 4306 error = ip_output(m, sc->sc_ipopts, ro, 4307 (ip_mtudisc ? IP_MTUDISC : 0), 4308 (struct ip_moptions *)NULL, so); 4309 break; 4310 #endif 4311 #ifdef INET6 4312 case AF_INET6: 4313 ip6->ip6_hlim = in6_selecthlim(NULL, 4314 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL); 4315 4316 error = ip6_output(m, NULL /*XXX*/, (struct route_in6 *)ro, 0, 4317 (struct ip6_moptions *)0, so, NULL); 4318 break; 4319 #endif 4320 default: 4321 error = EAFNOSUPPORT; 4322 break; 4323 } 4324 return (error); 4325 } 4326