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