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