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