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