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