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