1 /* $OpenBSD: tcp_input.c,v 1.178 2004/11/25 15:32:08 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 /* subtract out the tcp timestamp modulator */ 936 if (opti.ts_present) 937 opti.ts_ecr -= tp->ts_modulate; 938 939 #ifdef TCP_SACK 940 if (tp->sack_enable) { 941 tp->rcv_laststart = th->th_seq; /* last rec'vd segment*/ 942 tp->rcv_lastend = th->th_seq + tlen; 943 } 944 #endif /* TCP_SACK */ 945 #ifdef TCP_ECN 946 /* if congestion experienced, set ECE bit in subsequent packets. */ 947 if ((iptos & IPTOS_ECN_MASK) == IPTOS_ECN_CE) { 948 tp->t_flags |= TF_RCVD_CE; 949 tcpstat.tcps_ecn_rcvce++; 950 } 951 #endif 952 /* 953 * Header prediction: check for the two common cases 954 * of a uni-directional data xfer. If the packet has 955 * no control flags, is in-sequence, the window didn't 956 * change and we're not retransmitting, it's a 957 * candidate. If the length is zero and the ack moved 958 * forward, we're the sender side of the xfer. Just 959 * free the data acked & wake any higher level process 960 * that was blocked waiting for space. If the length 961 * is non-zero and the ack didn't move, we're the 962 * receiver side. If we're getting packets in-order 963 * (the reassembly queue is empty), add the data to 964 * the socket buffer and note that we need a delayed ack. 965 */ 966 if (tp->t_state == TCPS_ESTABLISHED && 967 #ifdef TCP_ECN 968 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ECE|TH_CWR|TH_ACK)) == TH_ACK && 969 #else 970 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 971 #endif 972 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) && 973 th->th_seq == tp->rcv_nxt && 974 tiwin && tiwin == tp->snd_wnd && 975 tp->snd_nxt == tp->snd_max) { 976 977 /* 978 * If last ACK falls within this segment's sequence numbers, 979 * record the timestamp. 980 * Fix from Braden, see Stevens p. 870 981 */ 982 if (opti.ts_present && SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 983 tp->ts_recent_age = tcp_now; 984 tp->ts_recent = opti.ts_val; 985 } 986 987 if (tlen == 0) { 988 if (SEQ_GT(th->th_ack, tp->snd_una) && 989 SEQ_LEQ(th->th_ack, tp->snd_max) && 990 tp->snd_cwnd >= tp->snd_wnd && 991 tp->t_dupacks == 0) { 992 /* 993 * this is a pure ack for outstanding data. 994 */ 995 ++tcpstat.tcps_predack; 996 if (opti.ts_present) 997 tcp_xmit_timer(tp, tcp_now-opti.ts_ecr+1); 998 else if (tp->t_rtttime && 999 SEQ_GT(th->th_ack, tp->t_rtseq)) 1000 tcp_xmit_timer(tp, 1001 tcp_now - tp->t_rtttime); 1002 acked = th->th_ack - tp->snd_una; 1003 tcpstat.tcps_rcvackpack++; 1004 tcpstat.tcps_rcvackbyte += acked; 1005 ND6_HINT(tp); 1006 sbdrop(&so->so_snd, acked); 1007 tp->snd_una = th->th_ack; 1008 #if defined(TCP_SACK) || defined(TCP_ECN) 1009 /* 1010 * We want snd_last to track snd_una so 1011 * as to avoid sequence wraparound problems 1012 * for very large transfers. 1013 */ 1014 #ifdef TCP_ECN 1015 if (SEQ_GT(tp->snd_una, tp->snd_last)) 1016 #endif 1017 tp->snd_last = tp->snd_una; 1018 #endif /* TCP_SACK */ 1019 #if defined(TCP_SACK) && defined(TCP_FACK) 1020 tp->snd_fack = tp->snd_una; 1021 tp->retran_data = 0; 1022 #endif /* TCP_FACK */ 1023 m_freem(m); 1024 1025 /* 1026 * If all outstanding data are acked, stop 1027 * retransmit timer, otherwise restart timer 1028 * using current (possibly backed-off) value. 1029 * If process is waiting for space, 1030 * wakeup/selwakeup/signal. If data 1031 * are ready to send, let tcp_output 1032 * decide between more output or persist. 1033 */ 1034 if (tp->snd_una == tp->snd_max) 1035 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1036 else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 1037 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1038 1039 if (sb_notify(&so->so_snd)) 1040 sowwakeup(so); 1041 if (so->so_snd.sb_cc) 1042 (void) tcp_output(tp); 1043 return; 1044 } 1045 } else if (th->th_ack == tp->snd_una && 1046 tp->segq.lh_first == NULL && 1047 tlen <= sbspace(&so->so_rcv)) { 1048 /* 1049 * This is a pure, in-sequence data packet 1050 * with nothing on the reassembly queue and 1051 * we have enough buffer space to take it. 1052 */ 1053 #ifdef TCP_SACK 1054 /* Clean receiver SACK report if present */ 1055 if (tp->sack_enable && tp->rcv_numsacks) 1056 tcp_clean_sackreport(tp); 1057 #endif /* TCP_SACK */ 1058 ++tcpstat.tcps_preddat; 1059 tp->rcv_nxt += tlen; 1060 tcpstat.tcps_rcvpack++; 1061 tcpstat.tcps_rcvbyte += tlen; 1062 ND6_HINT(tp); 1063 /* 1064 * Drop TCP, IP headers and TCP options then add data 1065 * to socket buffer. 1066 */ 1067 if (so->so_state & SS_CANTRCVMORE) 1068 m_freem(m); 1069 else { 1070 m_adj(m, iphlen + off); 1071 sbappendstream(&so->so_rcv, m); 1072 } 1073 sorwakeup(so); 1074 TCP_SETUP_ACK(tp, tiflags); 1075 if (tp->t_flags & TF_ACKNOW) 1076 (void) tcp_output(tp); 1077 return; 1078 } 1079 } 1080 1081 /* 1082 * Compute mbuf offset to TCP data segment. 1083 */ 1084 hdroptlen = iphlen + off; 1085 1086 /* 1087 * Calculate amount of space in receive window, 1088 * and then do TCP input processing. 1089 * Receive window is amount of space in rcv queue, 1090 * but not less than advertised window. 1091 */ 1092 { int win; 1093 1094 win = sbspace(&so->so_rcv); 1095 if (win < 0) 1096 win = 0; 1097 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1098 } 1099 1100 switch (tp->t_state) { 1101 1102 /* 1103 * If the state is SYN_RECEIVED: 1104 * if seg contains SYN/ACK, send an RST. 1105 * if seg contains an ACK, but not for our SYN/ACK, send an RST 1106 */ 1107 1108 case TCPS_SYN_RECEIVED: 1109 if (tiflags & TH_ACK) { 1110 if (tiflags & TH_SYN) { 1111 tcpstat.tcps_badsyn++; 1112 goto dropwithreset; 1113 } 1114 if (SEQ_LEQ(th->th_ack, tp->snd_una) || 1115 SEQ_GT(th->th_ack, tp->snd_max)) 1116 goto dropwithreset; 1117 } 1118 break; 1119 1120 /* 1121 * If the state is SYN_SENT: 1122 * if seg contains an ACK, but not for our SYN, drop the input. 1123 * if seg contains a RST, then drop the connection. 1124 * if seg does not contain SYN, then drop it. 1125 * Otherwise this is an acceptable SYN segment 1126 * initialize tp->rcv_nxt and tp->irs 1127 * if seg contains ack then advance tp->snd_una 1128 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1129 * arrange for segment to be acked (eventually) 1130 * continue processing rest of data/controls, beginning with URG 1131 */ 1132 case TCPS_SYN_SENT: 1133 if ((tiflags & TH_ACK) && 1134 (SEQ_LEQ(th->th_ack, tp->iss) || 1135 SEQ_GT(th->th_ack, tp->snd_max))) 1136 goto dropwithreset; 1137 if (tiflags & TH_RST) { 1138 #ifdef TCP_ECN 1139 /* if ECN is enabled, fall back to non-ecn at rexmit */ 1140 if (tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN)) 1141 goto drop; 1142 #endif 1143 if (tiflags & TH_ACK) 1144 tp = tcp_drop(tp, ECONNREFUSED); 1145 goto drop; 1146 } 1147 if ((tiflags & TH_SYN) == 0) 1148 goto drop; 1149 if (tiflags & TH_ACK) { 1150 tp->snd_una = th->th_ack; 1151 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1152 tp->snd_nxt = tp->snd_una; 1153 } 1154 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1155 tp->irs = th->th_seq; 1156 tcp_mss(tp, opti.maxseg); 1157 /* Reset initial window to 1 segment for retransmit */ 1158 if (tp->t_rxtshift > 0) 1159 tp->snd_cwnd = tp->t_maxseg; 1160 tcp_rcvseqinit(tp); 1161 tp->t_flags |= TF_ACKNOW; 1162 #ifdef TCP_SACK 1163 /* 1164 * If we've sent a SACK_PERMITTED option, and the peer 1165 * also replied with one, then TF_SACK_PERMIT should have 1166 * been set in tcp_dooptions(). If it was not, disable SACKs. 1167 */ 1168 if (tp->sack_enable) 1169 tp->sack_enable = tp->t_flags & TF_SACK_PERMIT; 1170 #endif 1171 #ifdef TCP_ECN 1172 /* 1173 * if ECE is set but CWR is not set for SYN-ACK, or 1174 * both ECE and CWR are set for simultaneous open, 1175 * peer is ECN capable. 1176 */ 1177 if (tcp_do_ecn) { 1178 if ((tiflags & (TH_ACK|TH_ECE|TH_CWR)) 1179 == (TH_ACK|TH_ECE) || 1180 (tiflags & (TH_ACK|TH_ECE|TH_CWR)) 1181 == (TH_ECE|TH_CWR)) { 1182 tp->t_flags |= TF_ECN_PERMIT; 1183 tiflags &= ~(TH_ECE|TH_CWR); 1184 tcpstat.tcps_ecn_accepts++; 1185 } 1186 } 1187 #endif 1188 1189 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 1190 tcpstat.tcps_connects++; 1191 soisconnected(so); 1192 tp->t_state = TCPS_ESTABLISHED; 1193 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 1194 /* Do window scaling on this connection? */ 1195 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1196 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1197 tp->snd_scale = tp->requested_s_scale; 1198 tp->rcv_scale = tp->request_r_scale; 1199 } 1200 tcp_reass_lock(tp); 1201 (void) tcp_reass(tp, (struct tcphdr *)0, 1202 (struct mbuf *)0, &tlen); 1203 tcp_reass_unlock(tp); 1204 /* 1205 * if we didn't have to retransmit the SYN, 1206 * use its rtt as our initial srtt & rtt var. 1207 */ 1208 if (tp->t_rtttime) 1209 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 1210 /* 1211 * Since new data was acked (the SYN), open the 1212 * congestion window by one MSS. We do this 1213 * here, because we won't go through the normal 1214 * ACK processing below. And since this is the 1215 * start of the connection, we know we are in 1216 * the exponential phase of slow-start. 1217 */ 1218 tp->snd_cwnd += tp->t_maxseg; 1219 } else 1220 tp->t_state = TCPS_SYN_RECEIVED; 1221 1222 #if 0 1223 trimthenstep6: 1224 #endif 1225 /* 1226 * Advance th->th_seq to correspond to first data byte. 1227 * If data, trim to stay within window, 1228 * dropping FIN if necessary. 1229 */ 1230 th->th_seq++; 1231 if (tlen > tp->rcv_wnd) { 1232 todrop = tlen - tp->rcv_wnd; 1233 m_adj(m, -todrop); 1234 tlen = tp->rcv_wnd; 1235 tiflags &= ~TH_FIN; 1236 tcpstat.tcps_rcvpackafterwin++; 1237 tcpstat.tcps_rcvbyteafterwin += todrop; 1238 } 1239 tp->snd_wl1 = th->th_seq - 1; 1240 tp->rcv_up = th->th_seq; 1241 goto step6; 1242 } 1243 1244 /* 1245 * States other than LISTEN or SYN_SENT. 1246 * First check timestamp, if present. 1247 * Then check that at least some bytes of segment are within 1248 * receive window. If segment begins before rcv_nxt, 1249 * drop leading data (and SYN); if nothing left, just ack. 1250 * 1251 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1252 * and it's less than opti.ts_recent, drop it. 1253 */ 1254 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && 1255 TSTMP_LT(opti.ts_val, tp->ts_recent)) { 1256 1257 /* Check to see if ts_recent is over 24 days old. */ 1258 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1259 /* 1260 * Invalidate ts_recent. If this segment updates 1261 * ts_recent, the age will be reset later and ts_recent 1262 * will get a valid value. If it does not, setting 1263 * ts_recent to zero will at least satisfy the 1264 * requirement that zero be placed in the timestamp 1265 * echo reply when ts_recent isn't valid. The 1266 * age isn't reset until we get a valid ts_recent 1267 * because we don't want out-of-order segments to be 1268 * dropped when ts_recent is old. 1269 */ 1270 tp->ts_recent = 0; 1271 } else { 1272 tcpstat.tcps_rcvduppack++; 1273 tcpstat.tcps_rcvdupbyte += tlen; 1274 tcpstat.tcps_pawsdrop++; 1275 goto dropafterack; 1276 } 1277 } 1278 1279 todrop = tp->rcv_nxt - th->th_seq; 1280 if (todrop > 0) { 1281 if (tiflags & TH_SYN) { 1282 tiflags &= ~TH_SYN; 1283 th->th_seq++; 1284 if (th->th_urp > 1) 1285 th->th_urp--; 1286 else 1287 tiflags &= ~TH_URG; 1288 todrop--; 1289 } 1290 if (todrop > tlen || 1291 (todrop == tlen && (tiflags & TH_FIN) == 0)) { 1292 /* 1293 * Any valid FIN must be to the left of the 1294 * window. At this point, FIN must be a 1295 * duplicate or out-of-sequence, so drop it. 1296 */ 1297 tiflags &= ~TH_FIN; 1298 /* 1299 * Send ACK to resynchronize, and drop any data, 1300 * but keep on processing for RST or ACK. 1301 */ 1302 tp->t_flags |= TF_ACKNOW; 1303 tcpstat.tcps_rcvdupbyte += todrop = tlen; 1304 tcpstat.tcps_rcvduppack++; 1305 } else { 1306 tcpstat.tcps_rcvpartduppack++; 1307 tcpstat.tcps_rcvpartdupbyte += todrop; 1308 } 1309 hdroptlen += todrop; /* drop from head afterwards */ 1310 th->th_seq += todrop; 1311 tlen -= todrop; 1312 if (th->th_urp > todrop) 1313 th->th_urp -= todrop; 1314 else { 1315 tiflags &= ~TH_URG; 1316 th->th_urp = 0; 1317 } 1318 } 1319 1320 /* 1321 * If new data are received on a connection after the 1322 * user processes are gone, then RST the other end. 1323 */ 1324 if ((so->so_state & SS_NOFDREF) && 1325 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1326 tp = tcp_close(tp); 1327 tcpstat.tcps_rcvafterclose++; 1328 goto dropwithreset; 1329 } 1330 1331 /* 1332 * If segment ends after window, drop trailing data 1333 * (and PUSH and FIN); if nothing left, just ACK. 1334 */ 1335 todrop = (th->th_seq + tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1336 if (todrop > 0) { 1337 tcpstat.tcps_rcvpackafterwin++; 1338 if (todrop >= tlen) { 1339 tcpstat.tcps_rcvbyteafterwin += tlen; 1340 /* 1341 * If a new connection request is received 1342 * while in TIME_WAIT, drop the old connection 1343 * and start over if the sequence numbers 1344 * are above the previous ones. 1345 */ 1346 if (tiflags & TH_SYN && 1347 tp->t_state == TCPS_TIME_WAIT && 1348 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1349 iss = tp->snd_nxt + TCP_ISSINCR; 1350 tp = tcp_close(tp); 1351 goto findpcb; 1352 } 1353 /* 1354 * If window is closed can only take segments at 1355 * window edge, and have to drop data and PUSH from 1356 * incoming segments. Continue processing, but 1357 * remember to ack. Otherwise, drop segment 1358 * and ack. 1359 */ 1360 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1361 tp->t_flags |= TF_ACKNOW; 1362 tcpstat.tcps_rcvwinprobe++; 1363 } else 1364 goto dropafterack; 1365 } else 1366 tcpstat.tcps_rcvbyteafterwin += todrop; 1367 m_adj(m, -todrop); 1368 tlen -= todrop; 1369 tiflags &= ~(TH_PUSH|TH_FIN); 1370 } 1371 1372 /* 1373 * If last ACK falls within this segment's sequence numbers, 1374 * record its timestamp. 1375 * Fix from Braden, see Stevens p. 870 1376 */ 1377 if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) && 1378 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1379 tp->ts_recent_age = tcp_now; 1380 tp->ts_recent = opti.ts_val; 1381 } 1382 1383 /* 1384 * If the RST bit is set examine the state: 1385 * SYN_RECEIVED STATE: 1386 * If passive open, return to LISTEN state. 1387 * If active open, inform user that connection was refused. 1388 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 1389 * Inform user that connection was reset, and close tcb. 1390 * CLOSING, LAST_ACK, TIME_WAIT STATES 1391 * Close the tcb. 1392 */ 1393 if (tiflags & TH_RST) { 1394 if (th->th_seq != tp->last_ack_sent) 1395 goto drop; 1396 1397 switch (tp->t_state) { 1398 case TCPS_SYN_RECEIVED: 1399 #ifdef TCP_ECN 1400 /* if ECN is enabled, fall back to non-ecn at rexmit */ 1401 if (tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN)) 1402 goto drop; 1403 #endif 1404 so->so_error = ECONNREFUSED; 1405 goto close; 1406 1407 case TCPS_ESTABLISHED: 1408 case TCPS_FIN_WAIT_1: 1409 case TCPS_FIN_WAIT_2: 1410 case TCPS_CLOSE_WAIT: 1411 so->so_error = ECONNRESET; 1412 close: 1413 tp->t_state = TCPS_CLOSED; 1414 tcpstat.tcps_drops++; 1415 tp = tcp_close(tp); 1416 goto drop; 1417 case TCPS_CLOSING: 1418 case TCPS_LAST_ACK: 1419 case TCPS_TIME_WAIT: 1420 tp = tcp_close(tp); 1421 goto drop; 1422 } 1423 } 1424 1425 /* 1426 * If a SYN is in the window, then this is an 1427 * error and we ACK and drop the packet. 1428 */ 1429 if (tiflags & TH_SYN) 1430 goto dropafterack_ratelim; 1431 1432 /* 1433 * If the ACK bit is off we drop the segment and return. 1434 */ 1435 if ((tiflags & TH_ACK) == 0) { 1436 if (tp->t_flags & TF_ACKNOW) 1437 goto dropafterack; 1438 else 1439 goto drop; 1440 } 1441 1442 /* 1443 * Ack processing. 1444 */ 1445 switch (tp->t_state) { 1446 1447 /* 1448 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1449 * ESTABLISHED state and continue processing. 1450 * The ACK was checked above. 1451 */ 1452 case TCPS_SYN_RECEIVED: 1453 tcpstat.tcps_connects++; 1454 soisconnected(so); 1455 tp->t_state = TCPS_ESTABLISHED; 1456 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 1457 /* Do window scaling? */ 1458 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1459 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1460 tp->snd_scale = tp->requested_s_scale; 1461 tp->rcv_scale = tp->request_r_scale; 1462 } 1463 tcp_reass_lock(tp); 1464 (void) tcp_reass(tp, (struct tcphdr *)0, (struct mbuf *)0, 1465 &tlen); 1466 tcp_reass_unlock(tp); 1467 tp->snd_wl1 = th->th_seq - 1; 1468 /* fall into ... */ 1469 1470 /* 1471 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1472 * ACKs. If the ack is in the range 1473 * tp->snd_una < th->th_ack <= tp->snd_max 1474 * then advance tp->snd_una to th->th_ack and drop 1475 * data from the retransmission queue. If this ACK reflects 1476 * more up to date window information we update our window information. 1477 */ 1478 case TCPS_ESTABLISHED: 1479 case TCPS_FIN_WAIT_1: 1480 case TCPS_FIN_WAIT_2: 1481 case TCPS_CLOSE_WAIT: 1482 case TCPS_CLOSING: 1483 case TCPS_LAST_ACK: 1484 case TCPS_TIME_WAIT: 1485 #ifdef TCP_ECN 1486 /* 1487 * if we receive ECE and are not already in recovery phase, 1488 * reduce cwnd by half but don't slow-start. 1489 * advance snd_last to snd_max not to reduce cwnd again 1490 * until all outstanding packets are acked. 1491 */ 1492 if (tcp_do_ecn && (tiflags & TH_ECE)) { 1493 if ((tp->t_flags & TF_ECN_PERMIT) && 1494 SEQ_GEQ(tp->snd_una, tp->snd_last)) { 1495 u_int win; 1496 1497 win = min(tp->snd_wnd, tp->snd_cwnd) / tp->t_maxseg; 1498 if (win > 1) { 1499 tp->snd_ssthresh = win / 2 * tp->t_maxseg; 1500 tp->snd_cwnd = tp->snd_ssthresh; 1501 tp->snd_last = tp->snd_max; 1502 tp->t_flags |= TF_SEND_CWR; 1503 tcpstat.tcps_cwr_ecn++; 1504 } 1505 } 1506 tcpstat.tcps_ecn_rcvece++; 1507 } 1508 /* 1509 * if we receive CWR, we know that the peer has reduced 1510 * its congestion window. stop sending ecn-echo. 1511 */ 1512 if ((tiflags & TH_CWR)) { 1513 tp->t_flags &= ~TF_RCVD_CE; 1514 tcpstat.tcps_ecn_rcvcwr++; 1515 } 1516 #endif /* TCP_ECN */ 1517 1518 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1519 /* 1520 * Duplicate/old ACK processing. 1521 * Increments t_dupacks: 1522 * Pure duplicate (same seq/ack/window, no data) 1523 * Doesn't affect t_dupacks: 1524 * Data packets. 1525 * Normal window updates (window opens) 1526 * Resets t_dupacks: 1527 * New data ACKed. 1528 * Window shrinks 1529 * Old ACK 1530 */ 1531 if (tlen) { 1532 /* Drop very old ACKs unless th_seq matches */ 1533 if (th->th_seq != tp->rcv_nxt && 1534 SEQ_LT(th->th_ack, 1535 tp->snd_una - tp->max_sndwnd)) { 1536 tcpstat.tcps_rcvacktooold++; 1537 goto drop; 1538 } 1539 break; 1540 } 1541 /* 1542 * If we get an old ACK, there is probably packet 1543 * reordering going on. Be conservative and reset 1544 * t_dupacks so that we are less agressive in 1545 * doing a fast retransmit. 1546 */ 1547 if (th->th_ack != tp->snd_una) { 1548 tp->t_dupacks = 0; 1549 break; 1550 } 1551 if (tiwin == tp->snd_wnd) { 1552 tcpstat.tcps_rcvdupack++; 1553 /* 1554 * If we have outstanding data (other than 1555 * a window probe), this is a completely 1556 * duplicate ack (ie, window info didn't 1557 * change), the ack is the biggest we've 1558 * seen and we've seen exactly our rexmt 1559 * threshhold of them, assume a packet 1560 * has been dropped and retransmit it. 1561 * Kludge snd_nxt & the congestion 1562 * window so we send only this one 1563 * packet. 1564 * 1565 * We know we're losing at the current 1566 * window size so do congestion avoidance 1567 * (set ssthresh to half the current window 1568 * and pull our congestion window back to 1569 * the new ssthresh). 1570 * 1571 * Dup acks mean that packets have left the 1572 * network (they're now cached at the receiver) 1573 * so bump cwnd by the amount in the receiver 1574 * to keep a constant cwnd packets in the 1575 * network. 1576 */ 1577 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0) 1578 tp->t_dupacks = 0; 1579 #if defined(TCP_SACK) && defined(TCP_FACK) 1580 /* 1581 * In FACK, can enter fast rec. if the receiver 1582 * reports a reass. queue longer than 3 segs. 1583 */ 1584 else if (++tp->t_dupacks == tcprexmtthresh || 1585 ((SEQ_GT(tp->snd_fack, tcprexmtthresh * 1586 tp->t_maxseg + tp->snd_una)) && 1587 SEQ_GT(tp->snd_una, tp->snd_last))) { 1588 #else 1589 else if (++tp->t_dupacks == tcprexmtthresh) { 1590 #endif /* TCP_FACK */ 1591 tcp_seq onxt = tp->snd_nxt; 1592 u_long win = 1593 ulmin(tp->snd_wnd, tp->snd_cwnd) / 1594 2 / tp->t_maxseg; 1595 1596 #if defined(TCP_SACK) || defined(TCP_ECN) 1597 if (SEQ_LT(th->th_ack, tp->snd_last)){ 1598 /* 1599 * False fast retx after 1600 * timeout. Do not cut window. 1601 */ 1602 tp->t_dupacks = 0; 1603 goto drop; 1604 } 1605 #endif 1606 if (win < 2) 1607 win = 2; 1608 tp->snd_ssthresh = win * tp->t_maxseg; 1609 #if defined(TCP_SACK) 1610 tp->snd_last = tp->snd_max; 1611 #endif 1612 #ifdef TCP_SACK 1613 if (tp->sack_enable) { 1614 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1615 tp->t_rtttime = 0; 1616 #ifdef TCP_ECN 1617 tp->t_flags |= TF_SEND_CWR; 1618 #endif 1619 #if 1 /* TCP_ECN */ 1620 tcpstat.tcps_cwr_frecovery++; 1621 #endif 1622 tcpstat.tcps_sndrexmitfast++; 1623 #if defined(TCP_SACK) && defined(TCP_FACK) 1624 tp->t_dupacks = tcprexmtthresh; 1625 (void) tcp_output(tp); 1626 /* 1627 * During FR, snd_cwnd is held 1628 * constant for FACK. 1629 */ 1630 tp->snd_cwnd = tp->snd_ssthresh; 1631 #else 1632 /* 1633 * tcp_output() will send 1634 * oldest SACK-eligible rtx. 1635 */ 1636 (void) tcp_output(tp); 1637 tp->snd_cwnd = tp->snd_ssthresh+ 1638 tp->t_maxseg * tp->t_dupacks; 1639 #endif /* TCP_FACK */ 1640 goto drop; 1641 } 1642 #endif /* TCP_SACK */ 1643 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1644 tp->t_rtttime = 0; 1645 tp->snd_nxt = th->th_ack; 1646 tp->snd_cwnd = tp->t_maxseg; 1647 #ifdef TCP_ECN 1648 tp->t_flags |= TF_SEND_CWR; 1649 #endif 1650 #if 1 /* TCP_ECN */ 1651 tcpstat.tcps_cwr_frecovery++; 1652 #endif 1653 tcpstat.tcps_sndrexmitfast++; 1654 (void) tcp_output(tp); 1655 1656 tp->snd_cwnd = tp->snd_ssthresh + 1657 tp->t_maxseg * tp->t_dupacks; 1658 if (SEQ_GT(onxt, tp->snd_nxt)) 1659 tp->snd_nxt = onxt; 1660 goto drop; 1661 } else if (tp->t_dupacks > tcprexmtthresh) { 1662 #if defined(TCP_SACK) && defined(TCP_FACK) 1663 /* 1664 * while (awnd < cwnd) 1665 * sendsomething(); 1666 */ 1667 if (tp->sack_enable) { 1668 if (tp->snd_awnd < tp->snd_cwnd) 1669 tcp_output(tp); 1670 goto drop; 1671 } 1672 #endif /* TCP_FACK */ 1673 tp->snd_cwnd += tp->t_maxseg; 1674 (void) tcp_output(tp); 1675 goto drop; 1676 } 1677 } else if (tiwin < tp->snd_wnd) { 1678 /* 1679 * The window was retracted! Previous dup 1680 * ACKs may have been due to packets arriving 1681 * after the shrunken window, not a missing 1682 * packet, so play it safe and reset t_dupacks 1683 */ 1684 tp->t_dupacks = 0; 1685 } 1686 break; 1687 } 1688 /* 1689 * If the congestion window was inflated to account 1690 * for the other side's cached packets, retract it. 1691 */ 1692 #if defined(TCP_SACK) 1693 if (tp->sack_enable) { 1694 if (tp->t_dupacks >= tcprexmtthresh) { 1695 /* Check for a partial ACK */ 1696 if (tcp_sack_partialack(tp, th)) { 1697 #if defined(TCP_SACK) && defined(TCP_FACK) 1698 /* Force call to tcp_output */ 1699 if (tp->snd_awnd < tp->snd_cwnd) 1700 needoutput = 1; 1701 #else 1702 tp->snd_cwnd += tp->t_maxseg; 1703 needoutput = 1; 1704 #endif /* TCP_FACK */ 1705 } else { 1706 /* Out of fast recovery */ 1707 tp->snd_cwnd = tp->snd_ssthresh; 1708 if (tcp_seq_subtract(tp->snd_max, 1709 th->th_ack) < tp->snd_ssthresh) 1710 tp->snd_cwnd = 1711 tcp_seq_subtract(tp->snd_max, 1712 th->th_ack); 1713 tp->t_dupacks = 0; 1714 #if defined(TCP_SACK) && defined(TCP_FACK) 1715 if (SEQ_GT(th->th_ack, tp->snd_fack)) 1716 tp->snd_fack = th->th_ack; 1717 #endif /* TCP_FACK */ 1718 } 1719 } 1720 } else { 1721 if (tp->t_dupacks >= tcprexmtthresh && 1722 !tcp_newreno(tp, th)) { 1723 /* Out of fast recovery */ 1724 tp->snd_cwnd = tp->snd_ssthresh; 1725 if (tcp_seq_subtract(tp->snd_max, th->th_ack) < 1726 tp->snd_ssthresh) 1727 tp->snd_cwnd = 1728 tcp_seq_subtract(tp->snd_max, 1729 th->th_ack); 1730 tp->t_dupacks = 0; 1731 } 1732 } 1733 if (tp->t_dupacks < tcprexmtthresh) 1734 tp->t_dupacks = 0; 1735 #else /* else no TCP_SACK */ 1736 if (tp->t_dupacks >= tcprexmtthresh && 1737 tp->snd_cwnd > tp->snd_ssthresh) 1738 tp->snd_cwnd = tp->snd_ssthresh; 1739 tp->t_dupacks = 0; 1740 #endif 1741 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1742 tcpstat.tcps_rcvacktoomuch++; 1743 goto dropafterack_ratelim; 1744 } 1745 acked = th->th_ack - tp->snd_una; 1746 tcpstat.tcps_rcvackpack++; 1747 tcpstat.tcps_rcvackbyte += acked; 1748 1749 /* 1750 * If we have a timestamp reply, update smoothed 1751 * round trip time. If no timestamp is present but 1752 * transmit timer is running and timed sequence 1753 * number was acked, update smoothed round trip time. 1754 * Since we now have an rtt measurement, cancel the 1755 * timer backoff (cf., Phil Karn's retransmit alg.). 1756 * Recompute the initial retransmit timer. 1757 */ 1758 if (opti.ts_present) 1759 tcp_xmit_timer(tp, tcp_now-opti.ts_ecr+1); 1760 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 1761 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime); 1762 1763 /* 1764 * If all outstanding data is acked, stop retransmit 1765 * timer and remember to restart (more output or persist). 1766 * If there is more data to be acked, restart retransmit 1767 * timer, using current (possibly backed-off) value. 1768 */ 1769 if (th->th_ack == tp->snd_max) { 1770 TCP_TIMER_DISARM(tp, TCPT_REXMT); 1771 needoutput = 1; 1772 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) 1773 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 1774 /* 1775 * When new data is acked, open the congestion window. 1776 * If the window gives us less than ssthresh packets 1777 * in flight, open exponentially (maxseg per packet). 1778 * Otherwise open linearly: maxseg per window 1779 * (maxseg^2 / cwnd per packet). 1780 */ 1781 { 1782 u_int cw = tp->snd_cwnd; 1783 u_int incr = tp->t_maxseg; 1784 1785 if (cw > tp->snd_ssthresh) 1786 incr = incr * incr / cw; 1787 #if defined (TCP_SACK) 1788 if (tp->t_dupacks < tcprexmtthresh) 1789 #endif 1790 tp->snd_cwnd = ulmin(cw + incr, TCP_MAXWIN<<tp->snd_scale); 1791 } 1792 ND6_HINT(tp); 1793 if (acked > so->so_snd.sb_cc) { 1794 tp->snd_wnd -= so->so_snd.sb_cc; 1795 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1796 ourfinisacked = 1; 1797 } else { 1798 sbdrop(&so->so_snd, acked); 1799 tp->snd_wnd -= acked; 1800 ourfinisacked = 0; 1801 } 1802 if (sb_notify(&so->so_snd)) 1803 sowwakeup(so); 1804 tp->snd_una = th->th_ack; 1805 #ifdef TCP_ECN 1806 /* sync snd_last with snd_una */ 1807 if (SEQ_GT(tp->snd_una, tp->snd_last)) 1808 tp->snd_last = tp->snd_una; 1809 #endif 1810 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1811 tp->snd_nxt = tp->snd_una; 1812 #if defined (TCP_SACK) && defined (TCP_FACK) 1813 if (SEQ_GT(tp->snd_una, tp->snd_fack)) { 1814 tp->snd_fack = tp->snd_una; 1815 /* Update snd_awnd for partial ACK 1816 * without any SACK blocks. 1817 */ 1818 tp->snd_awnd = tcp_seq_subtract(tp->snd_nxt, 1819 tp->snd_fack) + tp->retran_data; 1820 } 1821 #endif 1822 1823 switch (tp->t_state) { 1824 1825 /* 1826 * In FIN_WAIT_1 STATE in addition to the processing 1827 * for the ESTABLISHED state if our FIN is now acknowledged 1828 * then enter FIN_WAIT_2. 1829 */ 1830 case TCPS_FIN_WAIT_1: 1831 if (ourfinisacked) { 1832 /* 1833 * If we can't receive any more 1834 * data, then closing user can proceed. 1835 * Starting the timer is contrary to the 1836 * specification, but if we don't get a FIN 1837 * we'll hang forever. 1838 */ 1839 if (so->so_state & SS_CANTRCVMORE) { 1840 soisdisconnected(so); 1841 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle); 1842 } 1843 tp->t_state = TCPS_FIN_WAIT_2; 1844 } 1845 break; 1846 1847 /* 1848 * In CLOSING STATE in addition to the processing for 1849 * the ESTABLISHED state if the ACK acknowledges our FIN 1850 * then enter the TIME-WAIT state, otherwise ignore 1851 * the segment. 1852 */ 1853 case TCPS_CLOSING: 1854 if (ourfinisacked) { 1855 tp->t_state = TCPS_TIME_WAIT; 1856 tcp_canceltimers(tp); 1857 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 1858 soisdisconnected(so); 1859 } 1860 break; 1861 1862 /* 1863 * In LAST_ACK, we may still be waiting for data to drain 1864 * and/or to be acked, as well as for the ack of our FIN. 1865 * If our FIN is now acknowledged, delete the TCB, 1866 * enter the closed state and return. 1867 */ 1868 case TCPS_LAST_ACK: 1869 if (ourfinisacked) { 1870 tp = tcp_close(tp); 1871 goto drop; 1872 } 1873 break; 1874 1875 /* 1876 * In TIME_WAIT state the only thing that should arrive 1877 * is a retransmission of the remote FIN. Acknowledge 1878 * it and restart the finack timer. 1879 */ 1880 case TCPS_TIME_WAIT: 1881 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 1882 goto dropafterack; 1883 } 1884 } 1885 1886 step6: 1887 /* 1888 * Update window information. 1889 * Don't look at window if no ACK: TAC's send garbage on first SYN. 1890 */ 1891 if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) || 1892 (tp->snd_wl1 == th->th_seq && SEQ_LT(tp->snd_wl2, th->th_ack)) || 1893 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))) { 1894 /* keep track of pure window updates */ 1895 if (tlen == 0 && 1896 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 1897 tcpstat.tcps_rcvwinupd++; 1898 tp->snd_wnd = tiwin; 1899 tp->snd_wl1 = th->th_seq; 1900 tp->snd_wl2 = th->th_ack; 1901 if (tp->snd_wnd > tp->max_sndwnd) 1902 tp->max_sndwnd = tp->snd_wnd; 1903 needoutput = 1; 1904 } 1905 1906 /* 1907 * Process segments with URG. 1908 */ 1909 if ((tiflags & TH_URG) && th->th_urp && 1910 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1911 /* 1912 * This is a kludge, but if we receive and accept 1913 * random urgent pointers, we'll crash in 1914 * soreceive. It's hard to imagine someone 1915 * actually wanting to send this much urgent data. 1916 */ 1917 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 1918 th->th_urp = 0; /* XXX */ 1919 tiflags &= ~TH_URG; /* XXX */ 1920 goto dodata; /* XXX */ 1921 } 1922 /* 1923 * If this segment advances the known urgent pointer, 1924 * then mark the data stream. This should not happen 1925 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1926 * a FIN has been received from the remote side. 1927 * In these states we ignore the URG. 1928 * 1929 * According to RFC961 (Assigned Protocols), 1930 * the urgent pointer points to the last octet 1931 * of urgent data. We continue, however, 1932 * to consider it to indicate the first octet 1933 * of data past the urgent section as the original 1934 * spec states (in one of two places). 1935 */ 1936 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 1937 tp->rcv_up = th->th_seq + th->th_urp; 1938 so->so_oobmark = so->so_rcv.sb_cc + 1939 (tp->rcv_up - tp->rcv_nxt) - 1; 1940 if (so->so_oobmark == 0) 1941 so->so_state |= SS_RCVATMARK; 1942 sohasoutofband(so); 1943 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1944 } 1945 /* 1946 * Remove out of band data so doesn't get presented to user. 1947 * This can happen independent of advancing the URG pointer, 1948 * but if two URG's are pending at once, some out-of-band 1949 * data may creep in... ick. 1950 */ 1951 if (th->th_urp <= (u_int16_t) tlen 1952 #ifdef SO_OOBINLINE 1953 && (so->so_options & SO_OOBINLINE) == 0 1954 #endif 1955 ) 1956 tcp_pulloutofband(so, th->th_urp, m, hdroptlen); 1957 } else 1958 /* 1959 * If no out of band data is expected, 1960 * pull receive urgent pointer along 1961 * with the receive window. 1962 */ 1963 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1964 tp->rcv_up = tp->rcv_nxt; 1965 dodata: /* XXX */ 1966 1967 /* 1968 * Process the segment text, merging it into the TCP sequencing queue, 1969 * and arranging for acknowledgment of receipt if necessary. 1970 * This process logically involves adjusting tp->rcv_wnd as data 1971 * is presented to the user (this happens in tcp_usrreq.c, 1972 * case PRU_RCVD). If a FIN has already been received on this 1973 * connection then we just ignore the text. 1974 */ 1975 if ((tlen || (tiflags & TH_FIN)) && 1976 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1977 tcp_reass_lock(tp); 1978 if (th->th_seq == tp->rcv_nxt && tp->segq.lh_first == NULL && 1979 tp->t_state == TCPS_ESTABLISHED) { 1980 tcp_reass_unlock(tp); 1981 TCP_SETUP_ACK(tp, tiflags); 1982 tp->rcv_nxt += tlen; 1983 tiflags = th->th_flags & TH_FIN; 1984 tcpstat.tcps_rcvpack++; 1985 tcpstat.tcps_rcvbyte += tlen; 1986 ND6_HINT(tp); 1987 if (so->so_state & SS_CANTRCVMORE) 1988 m_freem(m); 1989 else { 1990 m_adj(m, hdroptlen); 1991 sbappendstream(&so->so_rcv, m); 1992 } 1993 sorwakeup(so); 1994 } else { 1995 m_adj(m, hdroptlen); 1996 tiflags = tcp_reass(tp, th, m, &tlen); 1997 tcp_reass_unlock(tp); 1998 tp->t_flags |= TF_ACKNOW; 1999 } 2000 #ifdef TCP_SACK 2001 if (tp->sack_enable) 2002 tcp_update_sack_list(tp); 2003 #endif 2004 2005 /* 2006 * variable len never referenced again in modern BSD, 2007 * so why bother computing it ?? 2008 */ 2009 #if 0 2010 /* 2011 * Note the amount of data that peer has sent into 2012 * our window, in order to estimate the sender's 2013 * buffer size. 2014 */ 2015 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2016 #endif /* 0 */ 2017 } else { 2018 m_freem(m); 2019 tiflags &= ~TH_FIN; 2020 } 2021 2022 /* 2023 * If FIN is received ACK the FIN and let the user know 2024 * that the connection is closing. Ignore a FIN received before 2025 * the connection is fully established. 2026 */ 2027 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) { 2028 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2029 socantrcvmore(so); 2030 tp->t_flags |= TF_ACKNOW; 2031 tp->rcv_nxt++; 2032 } 2033 switch (tp->t_state) { 2034 2035 /* 2036 * In ESTABLISHED STATE enter the CLOSE_WAIT state. 2037 */ 2038 case TCPS_ESTABLISHED: 2039 tp->t_state = TCPS_CLOSE_WAIT; 2040 break; 2041 2042 /* 2043 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2044 * enter the CLOSING state. 2045 */ 2046 case TCPS_FIN_WAIT_1: 2047 tp->t_state = TCPS_CLOSING; 2048 break; 2049 2050 /* 2051 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2052 * starting the time-wait timer, turning off the other 2053 * standard timers. 2054 */ 2055 case TCPS_FIN_WAIT_2: 2056 tp->t_state = TCPS_TIME_WAIT; 2057 tcp_canceltimers(tp); 2058 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2059 soisdisconnected(so); 2060 break; 2061 2062 /* 2063 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2064 */ 2065 case TCPS_TIME_WAIT: 2066 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL); 2067 break; 2068 } 2069 } 2070 if (so->so_options & SO_DEBUG) { 2071 switch (tp->pf) { 2072 #ifdef INET6 2073 case PF_INET6: 2074 tcp_trace(TA_INPUT, ostate, tp, (caddr_t) &tcp_saveti6, 2075 0, tlen); 2076 break; 2077 #endif /* INET6 */ 2078 case PF_INET: 2079 tcp_trace(TA_INPUT, ostate, tp, (caddr_t) &tcp_saveti, 2080 0, tlen); 2081 break; 2082 } 2083 } 2084 2085 /* 2086 * Return any desired output. 2087 */ 2088 if (needoutput || (tp->t_flags & TF_ACKNOW)) { 2089 (void) tcp_output(tp); 2090 } 2091 return; 2092 2093 badsyn: 2094 /* 2095 * Received a bad SYN. Increment counters and dropwithreset. 2096 */ 2097 tcpstat.tcps_badsyn++; 2098 tp = NULL; 2099 goto dropwithreset; 2100 2101 dropafterack_ratelim: 2102 if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count, 2103 tcp_ackdrop_ppslim) == 0) { 2104 /* XXX stat */ 2105 goto drop; 2106 } 2107 /* ...fall into dropafterack... */ 2108 2109 dropafterack: 2110 /* 2111 * Generate an ACK dropping incoming segment if it occupies 2112 * sequence space, where the ACK reflects our state. 2113 */ 2114 if (tiflags & TH_RST) 2115 goto drop; 2116 m_freem(m); 2117 tp->t_flags |= TF_ACKNOW; 2118 (void) tcp_output(tp); 2119 return; 2120 2121 dropwithreset_ratelim: 2122 /* 2123 * We may want to rate-limit RSTs in certain situations, 2124 * particularly if we are sending an RST in response to 2125 * an attempt to connect to or otherwise communicate with 2126 * a port for which we have no socket. 2127 */ 2128 if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count, 2129 tcp_rst_ppslim) == 0) { 2130 /* XXX stat */ 2131 goto drop; 2132 } 2133 /* ...fall into dropwithreset... */ 2134 2135 dropwithreset: 2136 /* 2137 * Generate a RST, dropping incoming segment. 2138 * Make ACK acceptable to originator of segment. 2139 * Don't bother to respond to RST. 2140 */ 2141 if (tiflags & TH_RST) 2142 goto drop; 2143 if (tiflags & TH_ACK) { 2144 tcp_respond(tp, mtod(m, caddr_t), m, (tcp_seq)0, th->th_ack, 2145 TH_RST); 2146 } else { 2147 if (tiflags & TH_SYN) 2148 tlen++; 2149 tcp_respond(tp, mtod(m, caddr_t), m, th->th_seq + tlen, 2150 (tcp_seq)0, TH_RST|TH_ACK); 2151 } 2152 return; 2153 2154 drop: 2155 /* 2156 * Drop space held by incoming segment and return. 2157 */ 2158 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) { 2159 switch (tp->pf) { 2160 #ifdef INET6 2161 case PF_INET6: 2162 tcp_trace(TA_DROP, ostate, tp, (caddr_t) &tcp_saveti6, 2163 0, tlen); 2164 break; 2165 #endif /* INET6 */ 2166 case PF_INET: 2167 tcp_trace(TA_DROP, ostate, tp, (caddr_t) &tcp_saveti, 2168 0, tlen); 2169 break; 2170 } 2171 } 2172 2173 m_freem(m); 2174 return; 2175 } 2176 2177 int 2178 tcp_dooptions(tp, cp, cnt, th, m, iphlen, oi) 2179 struct tcpcb *tp; 2180 u_char *cp; 2181 int cnt; 2182 struct tcphdr *th; 2183 struct mbuf *m; 2184 int iphlen; 2185 struct tcp_opt_info *oi; 2186 { 2187 u_int16_t mss = 0; 2188 int opt, optlen; 2189 #ifdef TCP_SIGNATURE 2190 caddr_t sigp = NULL; 2191 struct tdb *tdb = NULL; 2192 #endif /* TCP_SIGNATURE */ 2193 2194 for (; cp && cnt > 0; cnt -= optlen, cp += optlen) { 2195 opt = cp[0]; 2196 if (opt == TCPOPT_EOL) 2197 break; 2198 if (opt == TCPOPT_NOP) 2199 optlen = 1; 2200 else { 2201 if (cnt < 2) 2202 break; 2203 optlen = cp[1]; 2204 if (optlen < 2 || optlen > cnt) 2205 break; 2206 } 2207 switch (opt) { 2208 2209 default: 2210 continue; 2211 2212 case TCPOPT_MAXSEG: 2213 if (optlen != TCPOLEN_MAXSEG) 2214 continue; 2215 if (!(th->th_flags & TH_SYN)) 2216 continue; 2217 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 2218 NTOHS(mss); 2219 oi->maxseg = mss; 2220 break; 2221 2222 case TCPOPT_WINDOW: 2223 if (optlen != TCPOLEN_WINDOW) 2224 continue; 2225 if (!(th->th_flags & TH_SYN)) 2226 continue; 2227 tp->t_flags |= TF_RCVD_SCALE; 2228 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2229 break; 2230 2231 case TCPOPT_TIMESTAMP: 2232 if (optlen != TCPOLEN_TIMESTAMP) 2233 continue; 2234 oi->ts_present = 1; 2235 bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val)); 2236 NTOHL(oi->ts_val); 2237 bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr)); 2238 NTOHL(oi->ts_ecr); 2239 2240 /* 2241 * A timestamp received in a SYN makes 2242 * it ok to send timestamp requests and replies. 2243 */ 2244 if (th->th_flags & TH_SYN) { 2245 tp->t_flags |= TF_RCVD_TSTMP; 2246 tp->ts_recent = oi->ts_val; 2247 tp->ts_recent_age = tcp_now; 2248 } 2249 break; 2250 2251 #ifdef TCP_SACK 2252 case TCPOPT_SACK_PERMITTED: 2253 if (!tp->sack_enable || optlen!=TCPOLEN_SACK_PERMITTED) 2254 continue; 2255 if (th->th_flags & TH_SYN) 2256 /* MUST only be set on SYN */ 2257 tp->t_flags |= TF_SACK_PERMIT; 2258 break; 2259 case TCPOPT_SACK: 2260 if (tcp_sack_option(tp, th, cp, optlen)) 2261 continue; 2262 break; 2263 #endif 2264 #ifdef TCP_SIGNATURE 2265 case TCPOPT_SIGNATURE: 2266 if (optlen != TCPOLEN_SIGNATURE) 2267 continue; 2268 2269 if (sigp && bcmp(sigp, cp + 2, 16)) 2270 return (-1); 2271 2272 sigp = cp + 2; 2273 break; 2274 #endif /* TCP_SIGNATURE */ 2275 } 2276 } 2277 2278 #ifdef TCP_SIGNATURE 2279 if (tp->t_flags & TF_SIGNATURE) { 2280 union sockaddr_union src, dst; 2281 2282 memset(&src, 0, sizeof(union sockaddr_union)); 2283 memset(&dst, 0, sizeof(union sockaddr_union)); 2284 2285 switch (tp->pf) { 2286 case 0: 2287 #ifdef INET 2288 case AF_INET: 2289 src.sa.sa_len = sizeof(struct sockaddr_in); 2290 src.sa.sa_family = AF_INET; 2291 src.sin.sin_addr = mtod(m, struct ip *)->ip_src; 2292 dst.sa.sa_len = sizeof(struct sockaddr_in); 2293 dst.sa.sa_family = AF_INET; 2294 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst; 2295 break; 2296 #endif 2297 #ifdef INET6 2298 case AF_INET6: 2299 src.sa.sa_len = sizeof(struct sockaddr_in6); 2300 src.sa.sa_family = AF_INET6; 2301 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src; 2302 dst.sa.sa_len = sizeof(struct sockaddr_in6); 2303 dst.sa.sa_family = AF_INET6; 2304 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst; 2305 break; 2306 #endif /* INET6 */ 2307 } 2308 2309 tdb = gettdbbysrcdst(0, &src, &dst, IPPROTO_TCP); 2310 2311 /* 2312 * We don't have an SA for this peer, so we turn off 2313 * TF_SIGNATURE on the listen socket 2314 */ 2315 if (tdb == NULL && tp->t_state == TCPS_LISTEN) 2316 tp->t_flags &= ~TF_SIGNATURE; 2317 2318 } 2319 2320 if ((sigp ? TF_SIGNATURE : 0) ^ (tp->t_flags & TF_SIGNATURE)) { 2321 tcpstat.tcps_rcvbadsig++; 2322 return (-1); 2323 } 2324 2325 if (sigp) { 2326 char sig[16]; 2327 2328 if (tdb == NULL) { 2329 tcpstat.tcps_rcvbadsig++; 2330 return (-1); 2331 } 2332 2333 if (tcp_signature(tdb, tp->pf, m, th, iphlen, 1, sig) < 0) 2334 return (-1); 2335 2336 if (bcmp(sig, sigp, 16)) { 2337 tcpstat.tcps_rcvbadsig++; 2338 return (-1); 2339 } 2340 2341 tcpstat.tcps_rcvgoodsig++; 2342 } 2343 #endif /* TCP_SIGNATURE */ 2344 2345 return (0); 2346 } 2347 2348 #if defined(TCP_SACK) 2349 u_long 2350 tcp_seq_subtract(a, b) 2351 u_long a, b; 2352 { 2353 return ((long)(a - b)); 2354 } 2355 #endif 2356 2357 2358 #ifdef TCP_SACK 2359 /* 2360 * This function is called upon receipt of new valid data (while not in header 2361 * prediction mode), and it updates the ordered list of sacks. 2362 */ 2363 void 2364 tcp_update_sack_list(tp) 2365 struct tcpcb *tp; 2366 { 2367 /* 2368 * First reported block MUST be the most recent one. Subsequent 2369 * blocks SHOULD be in the order in which they arrived at the 2370 * receiver. These two conditions make the implementation fully 2371 * compliant with RFC 2018. 2372 */ 2373 int i, j = 0, count = 0, lastpos = -1; 2374 struct sackblk sack, firstsack, temp[MAX_SACK_BLKS]; 2375 2376 /* First clean up current list of sacks */ 2377 for (i = 0; i < tp->rcv_numsacks; i++) { 2378 sack = tp->sackblks[i]; 2379 if (sack.start == 0 && sack.end == 0) { 2380 count++; /* count = number of blocks to be discarded */ 2381 continue; 2382 } 2383 if (SEQ_LEQ(sack.end, tp->rcv_nxt)) { 2384 tp->sackblks[i].start = tp->sackblks[i].end = 0; 2385 count++; 2386 } else { 2387 temp[j].start = tp->sackblks[i].start; 2388 temp[j++].end = tp->sackblks[i].end; 2389 } 2390 } 2391 tp->rcv_numsacks -= count; 2392 if (tp->rcv_numsacks == 0) { /* no sack blocks currently (fast path) */ 2393 tcp_clean_sackreport(tp); 2394 if (SEQ_LT(tp->rcv_nxt, tp->rcv_laststart)) { 2395 /* ==> need first sack block */ 2396 tp->sackblks[0].start = tp->rcv_laststart; 2397 tp->sackblks[0].end = tp->rcv_lastend; 2398 tp->rcv_numsacks = 1; 2399 } 2400 return; 2401 } 2402 /* Otherwise, sack blocks are already present. */ 2403 for (i = 0; i < tp->rcv_numsacks; i++) 2404 tp->sackblks[i] = temp[i]; /* first copy back sack list */ 2405 if (SEQ_GEQ(tp->rcv_nxt, tp->rcv_lastend)) 2406 return; /* sack list remains unchanged */ 2407 /* 2408 * From here, segment just received should be (part of) the 1st sack. 2409 * Go through list, possibly coalescing sack block entries. 2410 */ 2411 firstsack.start = tp->rcv_laststart; 2412 firstsack.end = tp->rcv_lastend; 2413 for (i = 0; i < tp->rcv_numsacks; i++) { 2414 sack = tp->sackblks[i]; 2415 if (SEQ_LT(sack.end, firstsack.start) || 2416 SEQ_GT(sack.start, firstsack.end)) 2417 continue; /* no overlap */ 2418 if (sack.start == firstsack.start && sack.end == firstsack.end){ 2419 /* 2420 * identical block; delete it here since we will 2421 * move it to the front of the list. 2422 */ 2423 tp->sackblks[i].start = tp->sackblks[i].end = 0; 2424 lastpos = i; /* last posn with a zero entry */ 2425 continue; 2426 } 2427 if (SEQ_LEQ(sack.start, firstsack.start)) 2428 firstsack.start = sack.start; /* merge blocks */ 2429 if (SEQ_GEQ(sack.end, firstsack.end)) 2430 firstsack.end = sack.end; /* merge blocks */ 2431 tp->sackblks[i].start = tp->sackblks[i].end = 0; 2432 lastpos = i; /* last posn with a zero entry */ 2433 } 2434 if (lastpos != -1) { /* at least one merge */ 2435 for (i = 0, j = 1; i < tp->rcv_numsacks; i++) { 2436 sack = tp->sackblks[i]; 2437 if (sack.start == 0 && sack.end == 0) 2438 continue; 2439 temp[j++] = sack; 2440 } 2441 tp->rcv_numsacks = j; /* including first blk (added later) */ 2442 for (i = 1; i < tp->rcv_numsacks; i++) /* now copy back */ 2443 tp->sackblks[i] = temp[i]; 2444 } else { /* no merges -- shift sacks by 1 */ 2445 if (tp->rcv_numsacks < MAX_SACK_BLKS) 2446 tp->rcv_numsacks++; 2447 for (i = tp->rcv_numsacks-1; i > 0; i--) 2448 tp->sackblks[i] = tp->sackblks[i-1]; 2449 } 2450 tp->sackblks[0] = firstsack; 2451 return; 2452 } 2453 2454 /* 2455 * Process the TCP SACK option. Returns 1 if tcp_dooptions() should continue, 2456 * and 0 otherwise, if the option was fine. tp->snd_holes is an ordered list 2457 * of holes (oldest to newest, in terms of the sequence space). 2458 */ 2459 int 2460 tcp_sack_option(struct tcpcb *tp, struct tcphdr *th, u_char *cp, int optlen) 2461 { 2462 int tmp_olen; 2463 u_char *tmp_cp; 2464 struct sackhole *cur, *p, *temp; 2465 2466 if (!tp->sack_enable) 2467 return (1); 2468 2469 /* Note: TCPOLEN_SACK must be 2*sizeof(tcp_seq) */ 2470 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 2471 return (1); 2472 tmp_cp = cp + 2; 2473 tmp_olen = optlen - 2; 2474 if (tp->snd_numholes < 0) 2475 tp->snd_numholes = 0; 2476 if (tp->t_maxseg == 0) 2477 panic("tcp_sack_option"); /* Should never happen */ 2478 while (tmp_olen > 0) { 2479 struct sackblk sack; 2480 2481 bcopy(tmp_cp, (char *) &(sack.start), sizeof(tcp_seq)); 2482 NTOHL(sack.start); 2483 bcopy(tmp_cp + sizeof(tcp_seq), 2484 (char *) &(sack.end), sizeof(tcp_seq)); 2485 NTOHL(sack.end); 2486 tmp_olen -= TCPOLEN_SACK; 2487 tmp_cp += TCPOLEN_SACK; 2488 if (SEQ_LEQ(sack.end, sack.start)) 2489 continue; /* bad SACK fields */ 2490 if (SEQ_LEQ(sack.end, tp->snd_una)) 2491 continue; /* old block */ 2492 #if defined(TCP_SACK) && defined(TCP_FACK) 2493 /* Updates snd_fack. */ 2494 if (SEQ_GT(sack.end, tp->snd_fack)) 2495 tp->snd_fack = sack.end; 2496 #endif /* TCP_FACK */ 2497 if (SEQ_GT(th->th_ack, tp->snd_una)) { 2498 if (SEQ_LT(sack.start, th->th_ack)) 2499 continue; 2500 } 2501 if (SEQ_GT(sack.end, tp->snd_max)) 2502 continue; 2503 if (tp->snd_holes == NULL) { /* first hole */ 2504 tp->snd_holes = (struct sackhole *) 2505 pool_get(&sackhl_pool, PR_NOWAIT); 2506 if (tp->snd_holes == NULL) { 2507 /* ENOBUFS, so ignore SACKed block for now*/ 2508 continue; 2509 } 2510 cur = tp->snd_holes; 2511 cur->start = th->th_ack; 2512 cur->end = sack.start; 2513 cur->rxmit = cur->start; 2514 cur->next = NULL; 2515 tp->snd_numholes = 1; 2516 tp->rcv_lastsack = sack.end; 2517 /* 2518 * dups is at least one. If more data has been 2519 * SACKed, it can be greater than one. 2520 */ 2521 cur->dups = min(tcprexmtthresh, 2522 ((sack.end - cur->end)/tp->t_maxseg)); 2523 if (cur->dups < 1) 2524 cur->dups = 1; 2525 continue; /* with next sack block */ 2526 } 2527 /* Go thru list of holes: p = previous, cur = current */ 2528 p = cur = tp->snd_holes; 2529 while (cur) { 2530 if (SEQ_LEQ(sack.end, cur->start)) 2531 /* SACKs data before the current hole */ 2532 break; /* no use going through more holes */ 2533 if (SEQ_GEQ(sack.start, cur->end)) { 2534 /* SACKs data beyond the current hole */ 2535 cur->dups++; 2536 if (((sack.end - cur->end)/tp->t_maxseg) >= 2537 tcprexmtthresh) 2538 cur->dups = tcprexmtthresh; 2539 p = cur; 2540 cur = cur->next; 2541 continue; 2542 } 2543 if (SEQ_LEQ(sack.start, cur->start)) { 2544 /* Data acks at least the beginning of hole */ 2545 #if defined(TCP_SACK) && defined(TCP_FACK) 2546 if (SEQ_GT(sack.end, cur->rxmit)) 2547 tp->retran_data -= 2548 tcp_seq_subtract(cur->rxmit, 2549 cur->start); 2550 else 2551 tp->retran_data -= 2552 tcp_seq_subtract(sack.end, 2553 cur->start); 2554 #endif /* TCP_FACK */ 2555 if (SEQ_GEQ(sack.end, cur->end)) { 2556 /* Acks entire hole, so delete hole */ 2557 if (p != cur) { 2558 p->next = cur->next; 2559 pool_put(&sackhl_pool, cur); 2560 cur = p->next; 2561 } else { 2562 cur = cur->next; 2563 pool_put(&sackhl_pool, p); 2564 p = cur; 2565 tp->snd_holes = p; 2566 } 2567 tp->snd_numholes--; 2568 continue; 2569 } 2570 /* otherwise, move start of hole forward */ 2571 cur->start = sack.end; 2572 cur->rxmit = max (cur->rxmit, cur->start); 2573 p = cur; 2574 cur = cur->next; 2575 continue; 2576 } 2577 /* move end of hole backward */ 2578 if (SEQ_GEQ(sack.end, cur->end)) { 2579 #if defined(TCP_SACK) && defined(TCP_FACK) 2580 if (SEQ_GT(cur->rxmit, sack.start)) 2581 tp->retran_data -= 2582 tcp_seq_subtract(cur->rxmit, 2583 sack.start); 2584 #endif /* TCP_FACK */ 2585 cur->end = sack.start; 2586 cur->rxmit = min(cur->rxmit, cur->end); 2587 cur->dups++; 2588 if (((sack.end - cur->end)/tp->t_maxseg) >= 2589 tcprexmtthresh) 2590 cur->dups = tcprexmtthresh; 2591 p = cur; 2592 cur = cur->next; 2593 continue; 2594 } 2595 if (SEQ_LT(cur->start, sack.start) && 2596 SEQ_GT(cur->end, sack.end)) { 2597 /* 2598 * ACKs some data in middle of a hole; need to 2599 * split current hole 2600 */ 2601 temp = (struct sackhole *) 2602 pool_get(&sackhl_pool, PR_NOWAIT); 2603 if (temp == NULL) 2604 continue; /* ENOBUFS */ 2605 #if defined(TCP_SACK) && defined(TCP_FACK) 2606 if (SEQ_GT(cur->rxmit, sack.end)) 2607 tp->retran_data -= 2608 tcp_seq_subtract(sack.end, 2609 sack.start); 2610 else if (SEQ_GT(cur->rxmit, sack.start)) 2611 tp->retran_data -= 2612 tcp_seq_subtract(cur->rxmit, 2613 sack.start); 2614 #endif /* TCP_FACK */ 2615 temp->next = cur->next; 2616 temp->start = sack.end; 2617 temp->end = cur->end; 2618 temp->dups = cur->dups; 2619 temp->rxmit = max(cur->rxmit, temp->start); 2620 cur->end = sack.start; 2621 cur->rxmit = min(cur->rxmit, cur->end); 2622 cur->dups++; 2623 if (((sack.end - cur->end)/tp->t_maxseg) >= 2624 tcprexmtthresh) 2625 cur->dups = tcprexmtthresh; 2626 cur->next = temp; 2627 p = temp; 2628 cur = p->next; 2629 tp->snd_numholes++; 2630 } 2631 } 2632 /* At this point, p points to the last hole on the list */ 2633 if (SEQ_LT(tp->rcv_lastsack, sack.start)) { 2634 /* 2635 * Need to append new hole at end. 2636 * Last hole is p (and it's not NULL). 2637 */ 2638 temp = (struct sackhole *) 2639 pool_get(&sackhl_pool, PR_NOWAIT); 2640 if (temp == NULL) 2641 continue; /* ENOBUFS */ 2642 temp->start = tp->rcv_lastsack; 2643 temp->end = sack.start; 2644 temp->dups = min(tcprexmtthresh, 2645 ((sack.end - sack.start)/tp->t_maxseg)); 2646 if (temp->dups < 1) 2647 temp->dups = 1; 2648 temp->rxmit = temp->start; 2649 temp->next = 0; 2650 p->next = temp; 2651 tp->rcv_lastsack = sack.end; 2652 tp->snd_numholes++; 2653 } 2654 } 2655 #if defined(TCP_SACK) && defined(TCP_FACK) 2656 /* 2657 * Update retran_data and snd_awnd. Go through the list of 2658 * holes. Increment retran_data by (hole->rxmit - hole->start). 2659 */ 2660 tp->retran_data = 0; 2661 cur = tp->snd_holes; 2662 while (cur) { 2663 tp->retran_data += cur->rxmit - cur->start; 2664 cur = cur->next; 2665 } 2666 tp->snd_awnd = tcp_seq_subtract(tp->snd_nxt, tp->snd_fack) + 2667 tp->retran_data; 2668 #endif /* TCP_FACK */ 2669 2670 return (0); 2671 } 2672 2673 /* 2674 * Delete stale (i.e, cumulatively ack'd) holes. Hole is deleted only if 2675 * it is completely acked; otherwise, tcp_sack_option(), called from 2676 * tcp_dooptions(), will fix up the hole. 2677 */ 2678 void 2679 tcp_del_sackholes(tp, th) 2680 struct tcpcb *tp; 2681 struct tcphdr *th; 2682 { 2683 if (tp->sack_enable && tp->t_state != TCPS_LISTEN) { 2684 /* max because this could be an older ack just arrived */ 2685 tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ? 2686 th->th_ack : tp->snd_una; 2687 struct sackhole *cur = tp->snd_holes; 2688 struct sackhole *prev; 2689 while (cur) 2690 if (SEQ_LEQ(cur->end, lastack)) { 2691 prev = cur; 2692 cur = cur->next; 2693 pool_put(&sackhl_pool, prev); 2694 tp->snd_numholes--; 2695 } else if (SEQ_LT(cur->start, lastack)) { 2696 cur->start = lastack; 2697 if (SEQ_LT(cur->rxmit, cur->start)) 2698 cur->rxmit = cur->start; 2699 break; 2700 } else 2701 break; 2702 tp->snd_holes = cur; 2703 } 2704 } 2705 2706 /* 2707 * Delete all receiver-side SACK information. 2708 */ 2709 void 2710 tcp_clean_sackreport(tp) 2711 struct tcpcb *tp; 2712 { 2713 int i; 2714 2715 tp->rcv_numsacks = 0; 2716 for (i = 0; i < MAX_SACK_BLKS; i++) 2717 tp->sackblks[i].start = tp->sackblks[i].end=0; 2718 2719 } 2720 2721 /* 2722 * Checks for partial ack. If partial ack arrives, turn off retransmission 2723 * timer, deflate the window, do not clear tp->t_dupacks, and return 1. 2724 * If the ack advances at least to tp->snd_last, return 0. 2725 */ 2726 int 2727 tcp_sack_partialack(tp, th) 2728 struct tcpcb *tp; 2729 struct tcphdr *th; 2730 { 2731 if (SEQ_LT(th->th_ack, tp->snd_last)) { 2732 /* Turn off retx. timer (will start again next segment) */ 2733 TCP_TIMER_DISARM(tp, TCPT_REXMT); 2734 tp->t_rtttime = 0; 2735 #ifndef TCP_FACK 2736 /* 2737 * Partial window deflation. This statement relies on the 2738 * fact that tp->snd_una has not been updated yet. In FACK 2739 * hold snd_cwnd constant during fast recovery. 2740 */ 2741 if (tp->snd_cwnd > (th->th_ack - tp->snd_una)) { 2742 tp->snd_cwnd -= th->th_ack - tp->snd_una; 2743 tp->snd_cwnd += tp->t_maxseg; 2744 } else 2745 tp->snd_cwnd = tp->t_maxseg; 2746 #endif 2747 return (1); 2748 } 2749 return (0); 2750 } 2751 #endif /* TCP_SACK */ 2752 2753 /* 2754 * Pull out of band byte out of a segment so 2755 * it doesn't appear in the user's data queue. 2756 * It is still reflected in the segment length for 2757 * sequencing purposes. 2758 */ 2759 void 2760 tcp_pulloutofband(so, urgent, m, off) 2761 struct socket *so; 2762 u_int urgent; 2763 struct mbuf *m; 2764 int off; 2765 { 2766 int cnt = off + urgent - 1; 2767 2768 while (cnt >= 0) { 2769 if (m->m_len > cnt) { 2770 char *cp = mtod(m, caddr_t) + cnt; 2771 struct tcpcb *tp = sototcpcb(so); 2772 2773 tp->t_iobc = *cp; 2774 tp->t_oobflags |= TCPOOB_HAVEDATA; 2775 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2776 m->m_len--; 2777 return; 2778 } 2779 cnt -= m->m_len; 2780 m = m->m_next; 2781 if (m == 0) 2782 break; 2783 } 2784 panic("tcp_pulloutofband"); 2785 } 2786 2787 /* 2788 * Collect new round-trip time estimate 2789 * and update averages and current timeout. 2790 */ 2791 void 2792 tcp_xmit_timer(tp, rtt) 2793 struct tcpcb *tp; 2794 short rtt; 2795 { 2796 short delta; 2797 short rttmin; 2798 2799 tcpstat.tcps_rttupdated++; 2800 --rtt; 2801 if (tp->t_srtt != 0) { 2802 /* 2803 * srtt is stored as fixed point with 3 bits after the 2804 * binary point (i.e., scaled by 8). The following magic 2805 * is equivalent to the smoothing algorithm in rfc793 with 2806 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2807 * point). Adjust rtt to origin 0. 2808 */ 2809 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT); 2810 if ((tp->t_srtt += delta) <= 0) 2811 tp->t_srtt = 1; 2812 /* 2813 * We accumulate a smoothed rtt variance (actually, a 2814 * smoothed mean difference), then set the retransmit 2815 * timer to smoothed rtt + 4 times the smoothed variance. 2816 * rttvar is stored as fixed point with 2 bits after the 2817 * binary point (scaled by 4). The following is 2818 * equivalent to rfc793 smoothing with an alpha of .75 2819 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2820 * rfc793's wired-in beta. 2821 */ 2822 if (delta < 0) 2823 delta = -delta; 2824 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 2825 if ((tp->t_rttvar += delta) <= 0) 2826 tp->t_rttvar = 1; 2827 } else { 2828 /* 2829 * No rtt measurement yet - use the unsmoothed rtt. 2830 * Set the variance to half the rtt (so our first 2831 * retransmit happens at 3*rtt). 2832 */ 2833 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2); 2834 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1); 2835 } 2836 tp->t_rtttime = 0; 2837 tp->t_rxtshift = 0; 2838 2839 /* 2840 * the retransmit should happen at rtt + 4 * rttvar. 2841 * Because of the way we do the smoothing, srtt and rttvar 2842 * will each average +1/2 tick of bias. When we compute 2843 * the retransmit timer, we want 1/2 tick of rounding and 2844 * 1 extra tick because of +-1/2 tick uncertainty in the 2845 * firing of the timer. The bias will give us exactly the 2846 * 1.5 tick we need. But, because the bias is 2847 * statistical, we have to test that we don't drop below 2848 * the minimum feasible timer (which is 2 ticks). 2849 */ 2850 if (tp->t_rttmin > rtt + 2) 2851 rttmin = tp->t_rttmin; 2852 else 2853 rttmin = rtt + 2; 2854 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), rttmin, TCPTV_REXMTMAX); 2855 2856 /* 2857 * We received an ack for a packet that wasn't retransmitted; 2858 * it is probably safe to discard any error indications we've 2859 * received recently. This isn't quite right, but close enough 2860 * for now (a route might have failed after we sent a segment, 2861 * and the return path might not be symmetrical). 2862 */ 2863 tp->t_softerror = 0; 2864 } 2865 2866 /* 2867 * Determine a reasonable value for maxseg size. 2868 * If the route is known, check route for mtu. 2869 * If none, use an mss that can be handled on the outgoing 2870 * interface without forcing IP to fragment; if bigger than 2871 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2872 * to utilize large mbufs. If no route is found, route has no mtu, 2873 * or the destination isn't local, use a default, hopefully conservative 2874 * size (usually 512 or the default IP max size, but no more than the mtu 2875 * of the interface), as we can't discover anything about intervening 2876 * gateways or networks. We also initialize the congestion/slow start 2877 * window to be a single segment if the destination isn't local. 2878 * While looking at the routing entry, we also initialize other path-dependent 2879 * parameters from pre-set or cached values in the routing entry. 2880 * 2881 * Also take into account the space needed for options that we 2882 * send regularly. Make maxseg shorter by that amount to assure 2883 * that we can send maxseg amount of data even when the options 2884 * are present. Store the upper limit of the length of options plus 2885 * data in maxopd. 2886 * 2887 * NOTE: offer == -1 indicates that the maxseg size changed due to 2888 * Path MTU discovery. 2889 */ 2890 int 2891 tcp_mss(tp, offer) 2892 struct tcpcb *tp; 2893 int offer; 2894 { 2895 struct rtentry *rt; 2896 struct ifnet *ifp; 2897 int mss, mssopt; 2898 int iphlen; 2899 struct inpcb *inp; 2900 2901 inp = tp->t_inpcb; 2902 2903 mssopt = mss = tcp_mssdflt; 2904 2905 rt = in_pcbrtentry(inp); 2906 2907 if (rt == NULL) 2908 goto out; 2909 2910 ifp = rt->rt_ifp; 2911 2912 switch (tp->pf) { 2913 #ifdef INET6 2914 case AF_INET6: 2915 iphlen = sizeof(struct ip6_hdr); 2916 break; 2917 #endif 2918 case AF_INET: 2919 iphlen = sizeof(struct ip); 2920 break; 2921 default: 2922 /* the family does not support path MTU discovery */ 2923 goto out; 2924 } 2925 2926 #ifdef RTV_MTU 2927 /* 2928 * if there's an mtu associated with the route and we support 2929 * path MTU discovery for the underlying protocol family, use it. 2930 */ 2931 if (rt->rt_rmx.rmx_mtu) { 2932 /* 2933 * One may wish to lower MSS to take into account options, 2934 * especially security-related options. 2935 */ 2936 if (tp->pf == AF_INET6 && rt->rt_rmx.rmx_mtu < IPV6_MMTU) { 2937 /* 2938 * RFC2460 section 5, last paragraph: if path MTU is 2939 * smaller than 1280, use 1280 as packet size and 2940 * attach fragment header. 2941 */ 2942 mss = IPV6_MMTU - iphlen - sizeof(struct ip6_frag) - 2943 sizeof(struct tcphdr); 2944 } else 2945 mss = rt->rt_rmx.rmx_mtu - iphlen - sizeof(struct tcphdr); 2946 } else 2947 #endif /* RTV_MTU */ 2948 if (!ifp) 2949 /* 2950 * ifp may be null and rmx_mtu may be zero in certain 2951 * v6 cases (e.g., if ND wasn't able to resolve the 2952 * destination host. 2953 */ 2954 goto out; 2955 else if (ifp->if_flags & IFF_LOOPBACK) 2956 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 2957 else if (tp->pf == AF_INET) { 2958 if (ip_mtudisc) 2959 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 2960 else if (inp && in_localaddr(inp->inp_faddr)) 2961 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 2962 } 2963 #ifdef INET6 2964 else if (tp->pf == AF_INET6) { 2965 /* 2966 * for IPv6, path MTU discovery is always turned on, 2967 * or the node must use packet size <= 1280. 2968 */ 2969 mss = IN6_LINKMTU(ifp) - iphlen - sizeof(struct tcphdr); 2970 } 2971 #endif /* INET6 */ 2972 2973 /* Calculate the value that we offer in TCPOPT_MAXSEG */ 2974 if (offer != -1) { 2975 #ifndef INET6 2976 mssopt = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 2977 #else 2978 if (tp->pf == AF_INET6) 2979 mssopt = IN6_LINKMTU(ifp) - iphlen - 2980 sizeof(struct tcphdr); 2981 else 2982 mssopt = ifp->if_mtu - iphlen - sizeof(struct tcphdr); 2983 #endif 2984 2985 mssopt = max(tcp_mssdflt, mssopt); 2986 } 2987 2988 out: 2989 /* 2990 * The current mss, t_maxseg, is initialized to the default value. 2991 * If we compute a smaller value, reduce the current mss. 2992 * If we compute a larger value, return it for use in sending 2993 * a max seg size option, but don't store it for use 2994 * unless we received an offer at least that large from peer. 2995 * 2996 * However, do not accept offers lower than the minimum of 2997 * the interface MTU and 216. 2998 */ 2999 if (offer > 0) 3000 tp->t_peermss = offer; 3001 if (tp->t_peermss) 3002 mss = min(mss, max(tp->t_peermss, 216)); 3003 3004 /* sanity - at least max opt. space */ 3005 mss = max(mss, 64); 3006 3007 /* 3008 * maxopd stores the maximum length of data AND options 3009 * in a segment; maxseg is the amount of data in a normal 3010 * segment. We need to store this value (maxopd) apart 3011 * from maxseg, because now every segment carries options 3012 * and thus we normally have somewhat less data in segments. 3013 */ 3014 tp->t_maxopd = mss; 3015 3016 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3017 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 3018 mss -= TCPOLEN_TSTAMP_APPA; 3019 #ifdef TCP_SIGNATURE 3020 if (tp->t_flags & TF_SIGNATURE) 3021 mss -= TCPOLEN_SIGLEN; 3022 #endif 3023 3024 if (offer == -1) { 3025 /* mss changed due to Path MTU discovery */ 3026 if (mss < tp->t_maxseg) { 3027 /* 3028 * Follow suggestion in RFC 2414 to reduce the 3029 * congestion window by the ratio of the old 3030 * segment size to the new segment size. 3031 */ 3032 tp->snd_cwnd = ulmax((tp->snd_cwnd / tp->t_maxseg) * 3033 mss, mss); 3034 } 3035 } else if (tcp_do_rfc3390) { 3036 /* increase initial window */ 3037 tp->snd_cwnd = ulmin(4 * mss, ulmax(2 * mss, 4380)); 3038 } else 3039 tp->snd_cwnd = mss; 3040 3041 tp->t_maxseg = mss; 3042 3043 return (offer != -1 ? mssopt : mss); 3044 } 3045 3046 /* 3047 * Set connection variables based on the effective MSS. 3048 * We are passed the TCPCB for the actual connection. If we 3049 * are the server, we are called by the compressed state engine 3050 * when the 3-way handshake is complete. If we are the client, 3051 * we are called when we receive the SYN,ACK from the server. 3052 * 3053 * NOTE: The t_maxseg value must be initialized in the TCPCB 3054 * before this routine is called! 3055 */ 3056 void 3057 tcp_mss_update(tp) 3058 struct tcpcb *tp; 3059 { 3060 int mss; 3061 u_long bufsize; 3062 struct rtentry *rt; 3063 struct socket *so; 3064 3065 so = tp->t_inpcb->inp_socket; 3066 mss = tp->t_maxseg; 3067 3068 rt = in_pcbrtentry(tp->t_inpcb); 3069 3070 if (rt == NULL) 3071 return; 3072 3073 bufsize = so->so_snd.sb_hiwat; 3074 if (bufsize < mss) { 3075 mss = bufsize; 3076 /* Update t_maxseg and t_maxopd */ 3077 tcp_mss(tp, mss); 3078 } else { 3079 bufsize = roundup(bufsize, mss); 3080 if (bufsize > sb_max) 3081 bufsize = sb_max; 3082 (void)sbreserve(&so->so_snd, bufsize); 3083 } 3084 3085 bufsize = so->so_rcv.sb_hiwat; 3086 if (bufsize > mss) { 3087 bufsize = roundup(bufsize, mss); 3088 if (bufsize > sb_max) 3089 bufsize = sb_max; 3090 (void)sbreserve(&so->so_rcv, bufsize); 3091 } 3092 3093 } 3094 3095 #if defined (TCP_SACK) 3096 /* 3097 * Checks for partial ack. If partial ack arrives, force the retransmission 3098 * of the next unacknowledged segment, do not clear tp->t_dupacks, and return 3099 * 1. By setting snd_nxt to ti_ack, this forces retransmission timer to 3100 * be started again. If the ack advances at least to tp->snd_last, return 0. 3101 */ 3102 int 3103 tcp_newreno(tp, th) 3104 struct tcpcb *tp; 3105 struct tcphdr *th; 3106 { 3107 if (SEQ_LT(th->th_ack, tp->snd_last)) { 3108 /* 3109 * snd_una has not been updated and the socket send buffer 3110 * not yet drained of the acked data, so we have to leave 3111 * snd_una as it was to get the correct data offset in 3112 * tcp_output(). 3113 */ 3114 tcp_seq onxt = tp->snd_nxt; 3115 u_long ocwnd = tp->snd_cwnd; 3116 TCP_TIMER_DISARM(tp, TCPT_REXMT); 3117 tp->t_rtttime = 0; 3118 tp->snd_nxt = th->th_ack; 3119 /* 3120 * Set snd_cwnd to one segment beyond acknowledged offset 3121 * (tp->snd_una not yet updated when this function is called) 3122 */ 3123 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3124 (void) tcp_output(tp); 3125 tp->snd_cwnd = ocwnd; 3126 if (SEQ_GT(onxt, tp->snd_nxt)) 3127 tp->snd_nxt = onxt; 3128 /* 3129 * Partial window deflation. Relies on fact that tp->snd_una 3130 * not updated yet. 3131 */ 3132 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 3133 return 1; 3134 } 3135 return 0; 3136 } 3137 #endif /* TCP_SACK */ 3138 3139 static int 3140 tcp_mss_adv(struct ifnet *ifp, int af) 3141 { 3142 int mss = 0; 3143 int iphlen; 3144 3145 switch (af) { 3146 case AF_INET: 3147 if (ifp != NULL) 3148 mss = ifp->if_mtu; 3149 iphlen = sizeof(struct ip); 3150 break; 3151 #ifdef INET6 3152 case AF_INET6: 3153 if (ifp != NULL) 3154 mss = IN6_LINKMTU(ifp); 3155 iphlen = sizeof(struct ip6_hdr); 3156 break; 3157 #endif 3158 } 3159 mss = mss - iphlen - sizeof(struct tcphdr); 3160 return (max(mss, tcp_mssdflt)); 3161 } 3162 3163 /* 3164 * TCP compressed state engine. Currently used to hold compressed 3165 * state for SYN_RECEIVED. 3166 */ 3167 3168 u_long syn_cache_count; 3169 u_int32_t syn_hash1, syn_hash2; 3170 3171 #define SYN_HASH(sa, sp, dp) \ 3172 ((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \ 3173 ((u_int32_t)(sp)))^syn_hash2))) 3174 #ifndef INET6 3175 #define SYN_HASHALL(hash, src, dst) \ 3176 do { \ 3177 hash = SYN_HASH(&((struct sockaddr_in *)(src))->sin_addr, \ 3178 ((struct sockaddr_in *)(src))->sin_port, \ 3179 ((struct sockaddr_in *)(dst))->sin_port); \ 3180 } while (/*CONSTCOND*/ 0) 3181 #else 3182 #define SYN_HASH6(sa, sp, dp) \ 3183 ((((sa)->s6_addr32[0] ^ (sa)->s6_addr32[3] ^ syn_hash1) * \ 3184 (((((u_int32_t)(dp))<<16) + ((u_int32_t)(sp)))^syn_hash2)) \ 3185 & 0x7fffffff) 3186 3187 #define SYN_HASHALL(hash, src, dst) \ 3188 do { \ 3189 switch ((src)->sa_family) { \ 3190 case AF_INET: \ 3191 hash = SYN_HASH(&((struct sockaddr_in *)(src))->sin_addr, \ 3192 ((struct sockaddr_in *)(src))->sin_port, \ 3193 ((struct sockaddr_in *)(dst))->sin_port); \ 3194 break; \ 3195 case AF_INET6: \ 3196 hash = SYN_HASH6(&((struct sockaddr_in6 *)(src))->sin6_addr, \ 3197 ((struct sockaddr_in6 *)(src))->sin6_port, \ 3198 ((struct sockaddr_in6 *)(dst))->sin6_port); \ 3199 break; \ 3200 default: \ 3201 hash = 0; \ 3202 } \ 3203 } while (/*CONSTCOND*/0) 3204 #endif /* INET6 */ 3205 3206 #define SYN_CACHE_RM(sc) \ 3207 do { \ 3208 (sc)->sc_flags |= SCF_DEAD; \ 3209 TAILQ_REMOVE(&tcp_syn_cache[(sc)->sc_bucketidx].sch_bucket, \ 3210 (sc), sc_bucketq); \ 3211 (sc)->sc_tp = NULL; \ 3212 LIST_REMOVE((sc), sc_tpq); \ 3213 tcp_syn_cache[(sc)->sc_bucketidx].sch_length--; \ 3214 timeout_del(&(sc)->sc_timer); \ 3215 syn_cache_count--; \ 3216 } while (/*CONSTCOND*/0) 3217 3218 #define SYN_CACHE_PUT(sc) \ 3219 do { \ 3220 if ((sc)->sc_ipopts) \ 3221 (void) m_free((sc)->sc_ipopts); \ 3222 if ((sc)->sc_route4.ro_rt != NULL) \ 3223 RTFREE((sc)->sc_route4.ro_rt); \ 3224 timeout_set(&(sc)->sc_timer, syn_cache_reaper, (sc)); \ 3225 timeout_add(&(sc)->sc_timer, 0); \ 3226 } while (/*CONSTCOND*/0) 3227 3228 struct pool syn_cache_pool; 3229 3230 /* 3231 * We don't estimate RTT with SYNs, so each packet starts with the default 3232 * RTT and each timer step has a fixed timeout value. 3233 */ 3234 #define SYN_CACHE_TIMER_ARM(sc) \ 3235 do { \ 3236 TCPT_RANGESET((sc)->sc_rxtcur, \ 3237 TCPTV_SRTTDFLT * tcp_backoff[(sc)->sc_rxtshift], TCPTV_MIN, \ 3238 TCPTV_REXMTMAX); \ 3239 if (!timeout_initialized(&(sc)->sc_timer)) \ 3240 timeout_set(&(sc)->sc_timer, syn_cache_timer, (sc)); \ 3241 timeout_add(&(sc)->sc_timer, (sc)->sc_rxtcur * (hz / PR_SLOWHZ)); \ 3242 } while (/*CONSTCOND*/0) 3243 3244 #define SYN_CACHE_TIMESTAMP(sc) tcp_now + (sc)->sc_modulate 3245 3246 void 3247 syn_cache_init() 3248 { 3249 int i; 3250 3251 /* Initialize the hash buckets. */ 3252 for (i = 0; i < tcp_syn_cache_size; i++) 3253 TAILQ_INIT(&tcp_syn_cache[i].sch_bucket); 3254 3255 /* Initialize the syn cache pool. */ 3256 pool_init(&syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0, 3257 "synpl", NULL); 3258 } 3259 3260 void 3261 syn_cache_insert(sc, tp) 3262 struct syn_cache *sc; 3263 struct tcpcb *tp; 3264 { 3265 struct syn_cache_head *scp; 3266 struct syn_cache *sc2; 3267 int s; 3268 3269 /* 3270 * If there are no entries in the hash table, reinitialize 3271 * the hash secrets. 3272 */ 3273 if (syn_cache_count == 0) { 3274 syn_hash1 = arc4random(); 3275 syn_hash2 = arc4random(); 3276 } 3277 3278 SYN_HASHALL(sc->sc_hash, &sc->sc_src.sa, &sc->sc_dst.sa); 3279 sc->sc_bucketidx = sc->sc_hash % tcp_syn_cache_size; 3280 scp = &tcp_syn_cache[sc->sc_bucketidx]; 3281 3282 /* 3283 * Make sure that we don't overflow the per-bucket 3284 * limit or the total cache size limit. 3285 */ 3286 s = splsoftnet(); 3287 if (scp->sch_length >= tcp_syn_bucket_limit) { 3288 tcpstat.tcps_sc_bucketoverflow++; 3289 /* 3290 * The bucket is full. Toss the oldest element in the 3291 * bucket. This will be the first entry in the bucket. 3292 */ 3293 sc2 = TAILQ_FIRST(&scp->sch_bucket); 3294 #ifdef DIAGNOSTIC 3295 /* 3296 * This should never happen; we should always find an 3297 * entry in our bucket. 3298 */ 3299 if (sc2 == NULL) 3300 panic("syn_cache_insert: bucketoverflow: impossible"); 3301 #endif 3302 SYN_CACHE_RM(sc2); 3303 SYN_CACHE_PUT(sc2); 3304 } else if (syn_cache_count >= tcp_syn_cache_limit) { 3305 struct syn_cache_head *scp2, *sce; 3306 3307 tcpstat.tcps_sc_overflowed++; 3308 /* 3309 * The cache is full. Toss the oldest entry in the 3310 * first non-empty bucket we can find. 3311 * 3312 * XXX We would really like to toss the oldest 3313 * entry in the cache, but we hope that this 3314 * condition doesn't happen very often. 3315 */ 3316 scp2 = scp; 3317 if (TAILQ_EMPTY(&scp2->sch_bucket)) { 3318 sce = &tcp_syn_cache[tcp_syn_cache_size]; 3319 for (++scp2; scp2 != scp; scp2++) { 3320 if (scp2 >= sce) 3321 scp2 = &tcp_syn_cache[0]; 3322 if (! TAILQ_EMPTY(&scp2->sch_bucket)) 3323 break; 3324 } 3325 #ifdef DIAGNOSTIC 3326 /* 3327 * This should never happen; we should always find a 3328 * non-empty bucket. 3329 */ 3330 if (scp2 == scp) 3331 panic("syn_cache_insert: cacheoverflow: " 3332 "impossible"); 3333 #endif 3334 } 3335 sc2 = TAILQ_FIRST(&scp2->sch_bucket); 3336 SYN_CACHE_RM(sc2); 3337 SYN_CACHE_PUT(sc2); 3338 } 3339 3340 /* 3341 * Initialize the entry's timer. 3342 */ 3343 sc->sc_rxttot = 0; 3344 sc->sc_rxtshift = 0; 3345 SYN_CACHE_TIMER_ARM(sc); 3346 3347 /* Link it from tcpcb entry */ 3348 LIST_INSERT_HEAD(&tp->t_sc, sc, sc_tpq); 3349 3350 /* Put it into the bucket. */ 3351 TAILQ_INSERT_TAIL(&scp->sch_bucket, sc, sc_bucketq); 3352 scp->sch_length++; 3353 syn_cache_count++; 3354 3355 tcpstat.tcps_sc_added++; 3356 splx(s); 3357 } 3358 3359 /* 3360 * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted. 3361 * If we have retransmitted an entry the maximum number of times, expire 3362 * that entry. 3363 */ 3364 void 3365 syn_cache_timer(void *arg) 3366 { 3367 struct syn_cache *sc = arg; 3368 int s; 3369 3370 s = splsoftnet(); 3371 if (sc->sc_flags & SCF_DEAD) { 3372 splx(s); 3373 return; 3374 } 3375 3376 if (__predict_false(sc->sc_rxtshift == TCP_MAXRXTSHIFT)) { 3377 /* Drop it -- too many retransmissions. */ 3378 goto dropit; 3379 } 3380 3381 /* 3382 * Compute the total amount of time this entry has 3383 * been on a queue. If this entry has been on longer 3384 * than the keep alive timer would allow, expire it. 3385 */ 3386 sc->sc_rxttot += sc->sc_rxtcur; 3387 if (sc->sc_rxttot >= tcptv_keep_init) 3388 goto dropit; 3389 3390 tcpstat.tcps_sc_retransmitted++; 3391 (void) syn_cache_respond(sc, NULL); 3392 3393 /* Advance the timer back-off. */ 3394 sc->sc_rxtshift++; 3395 SYN_CACHE_TIMER_ARM(sc); 3396 3397 splx(s); 3398 return; 3399 3400 dropit: 3401 tcpstat.tcps_sc_timed_out++; 3402 SYN_CACHE_RM(sc); 3403 SYN_CACHE_PUT(sc); 3404 splx(s); 3405 } 3406 3407 void 3408 syn_cache_reaper(void *arg) 3409 { 3410 struct syn_cache *sc = arg; 3411 int s; 3412 3413 s = splsoftnet(); 3414 pool_put(&syn_cache_pool, (sc)); 3415 splx(s); 3416 return; 3417 } 3418 3419 /* 3420 * Remove syn cache created by the specified tcb entry, 3421 * because this does not make sense to keep them 3422 * (if there's no tcb entry, syn cache entry will never be used) 3423 */ 3424 void 3425 syn_cache_cleanup(tp) 3426 struct tcpcb *tp; 3427 { 3428 struct syn_cache *sc, *nsc; 3429 int s; 3430 3431 s = splsoftnet(); 3432 3433 for (sc = LIST_FIRST(&tp->t_sc); sc != NULL; sc = nsc) { 3434 nsc = LIST_NEXT(sc, sc_tpq); 3435 3436 #ifdef DIAGNOSTIC 3437 if (sc->sc_tp != tp) 3438 panic("invalid sc_tp in syn_cache_cleanup"); 3439 #endif 3440 SYN_CACHE_RM(sc); 3441 SYN_CACHE_PUT(sc); 3442 } 3443 /* just for safety */ 3444 LIST_INIT(&tp->t_sc); 3445 3446 splx(s); 3447 } 3448 3449 /* 3450 * Find an entry in the syn cache. 3451 */ 3452 struct syn_cache * 3453 syn_cache_lookup(src, dst, headp) 3454 struct sockaddr *src; 3455 struct sockaddr *dst; 3456 struct syn_cache_head **headp; 3457 { 3458 struct syn_cache *sc; 3459 struct syn_cache_head *scp; 3460 u_int32_t hash; 3461 int s; 3462 3463 SYN_HASHALL(hash, src, dst); 3464 3465 scp = &tcp_syn_cache[hash % tcp_syn_cache_size]; 3466 *headp = scp; 3467 s = splsoftnet(); 3468 for (sc = TAILQ_FIRST(&scp->sch_bucket); sc != NULL; 3469 sc = TAILQ_NEXT(sc, sc_bucketq)) { 3470 if (sc->sc_hash != hash) 3471 continue; 3472 if (!bcmp(&sc->sc_src, src, src->sa_len) && 3473 !bcmp(&sc->sc_dst, dst, dst->sa_len)) { 3474 splx(s); 3475 return (sc); 3476 } 3477 } 3478 splx(s); 3479 return (NULL); 3480 } 3481 3482 /* 3483 * This function gets called when we receive an ACK for a 3484 * socket in the LISTEN state. We look up the connection 3485 * in the syn cache, and if its there, we pull it out of 3486 * the cache and turn it into a full-blown connection in 3487 * the SYN-RECEIVED state. 3488 * 3489 * The return values may not be immediately obvious, and their effects 3490 * can be subtle, so here they are: 3491 * 3492 * NULL SYN was not found in cache; caller should drop the 3493 * packet and send an RST. 3494 * 3495 * -1 We were unable to create the new connection, and are 3496 * aborting it. An ACK,RST is being sent to the peer 3497 * (unless we got screwey sequence numbners; see below), 3498 * because the 3-way handshake has been completed. Caller 3499 * should not free the mbuf, since we may be using it. If 3500 * we are not, we will free it. 3501 * 3502 * Otherwise, the return value is a pointer to the new socket 3503 * associated with the connection. 3504 */ 3505 struct socket * 3506 syn_cache_get(src, dst, th, hlen, tlen, so, m) 3507 struct sockaddr *src; 3508 struct sockaddr *dst; 3509 struct tcphdr *th; 3510 unsigned int hlen, tlen; 3511 struct socket *so; 3512 struct mbuf *m; 3513 { 3514 struct syn_cache *sc; 3515 struct syn_cache_head *scp; 3516 struct inpcb *inp = NULL; 3517 struct tcpcb *tp = 0; 3518 struct mbuf *am; 3519 int s; 3520 struct socket *oso; 3521 3522 s = splsoftnet(); 3523 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) { 3524 splx(s); 3525 return (NULL); 3526 } 3527 3528 /* 3529 * Verify the sequence and ack numbers. Try getting the correct 3530 * response again. 3531 */ 3532 if ((th->th_ack != sc->sc_iss + 1) || 3533 SEQ_LEQ(th->th_seq, sc->sc_irs) || 3534 SEQ_GT(th->th_seq, sc->sc_irs + 1 + sc->sc_win)) { 3535 (void) syn_cache_respond(sc, m); 3536 splx(s); 3537 return ((struct socket *)(-1)); 3538 } 3539 3540 /* Remove this cache entry */ 3541 SYN_CACHE_RM(sc); 3542 splx(s); 3543 3544 /* 3545 * Ok, create the full blown connection, and set things up 3546 * as they would have been set up if we had created the 3547 * connection when the SYN arrived. If we can't create 3548 * the connection, abort it. 3549 */ 3550 oso = so; 3551 so = sonewconn(so, SS_ISCONNECTED); 3552 if (so == NULL) 3553 goto resetandabort; 3554 3555 inp = sotoinpcb(oso); 3556 #ifdef IPSEC 3557 /* 3558 * We need to copy the required security levels 3559 * from the old pcb. Ditto for any other 3560 * IPsec-related information. 3561 */ 3562 { 3563 struct inpcb *newinp = (struct inpcb *)so->so_pcb; 3564 bcopy(inp->inp_seclevel, newinp->inp_seclevel, 3565 sizeof(inp->inp_seclevel)); 3566 newinp->inp_secrequire = inp->inp_secrequire; 3567 if (inp->inp_ipo != NULL) { 3568 newinp->inp_ipo = inp->inp_ipo; 3569 inp->inp_ipo->ipo_ref_count++; 3570 } 3571 if (inp->inp_ipsec_remotecred != NULL) { 3572 newinp->inp_ipsec_remotecred = inp->inp_ipsec_remotecred; 3573 inp->inp_ipsec_remotecred->ref_count++; 3574 } 3575 if (inp->inp_ipsec_remoteauth != NULL) { 3576 newinp->inp_ipsec_remoteauth 3577 = inp->inp_ipsec_remoteauth; 3578 inp->inp_ipsec_remoteauth->ref_count++; 3579 } 3580 } 3581 #endif /* IPSEC */ 3582 #ifdef INET6 3583 /* 3584 * inp still has the OLD in_pcb stuff, set the 3585 * v6-related flags on the new guy, too. 3586 */ 3587 { 3588 int flags = inp->inp_flags; 3589 struct inpcb *oldinpcb = inp; 3590 3591 inp = (struct inpcb *)so->so_pcb; 3592 inp->inp_flags |= (flags & INP_IPV6); 3593 if ((inp->inp_flags & INP_IPV6) != 0) { 3594 inp->inp_ipv6.ip6_hlim = 3595 oldinpcb->inp_ipv6.ip6_hlim; 3596 } 3597 } 3598 #else /* INET6 */ 3599 inp = (struct inpcb *)so->so_pcb; 3600 #endif /* INET6 */ 3601 3602 inp->inp_lport = th->th_dport; 3603 switch (src->sa_family) { 3604 #ifdef INET6 3605 case AF_INET6: 3606 inp->inp_laddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr; 3607 break; 3608 #endif /* INET6 */ 3609 case AF_INET: 3610 3611 inp->inp_laddr = ((struct sockaddr_in *)dst)->sin_addr; 3612 inp->inp_options = ip_srcroute(); 3613 if (inp->inp_options == NULL) { 3614 inp->inp_options = sc->sc_ipopts; 3615 sc->sc_ipopts = NULL; 3616 } 3617 break; 3618 } 3619 in_pcbrehash(inp); 3620 3621 /* 3622 * Give the new socket our cached route reference. 3623 */ 3624 if (src->sa_family == AF_INET) 3625 inp->inp_route = sc->sc_route4; /* struct assignment */ 3626 #ifdef INET6 3627 else 3628 inp->inp_route6 = sc->sc_route6; 3629 #endif 3630 sc->sc_route4.ro_rt = NULL; 3631 3632 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 3633 if (am == NULL) 3634 goto resetandabort; 3635 am->m_len = src->sa_len; 3636 bcopy(src, mtod(am, caddr_t), src->sa_len); 3637 3638 switch (src->sa_family) { 3639 case AF_INET: 3640 /* drop IPv4 packet to AF_INET6 socket */ 3641 if (inp->inp_flags & INP_IPV6) { 3642 (void) m_free(am); 3643 goto resetandabort; 3644 } 3645 if (in_pcbconnect(inp, am)) { 3646 (void) m_free(am); 3647 goto resetandabort; 3648 } 3649 break; 3650 #ifdef INET6 3651 case AF_INET6: 3652 if (in6_pcbconnect(inp, am)) { 3653 (void) m_free(am); 3654 goto resetandabort; 3655 } 3656 break; 3657 #endif 3658 } 3659 (void) m_free(am); 3660 3661 tp = intotcpcb(inp); 3662 tp->t_flags = sototcpcb(oso)->t_flags & TF_NODELAY; 3663 if (sc->sc_request_r_scale != 15) { 3664 tp->requested_s_scale = sc->sc_requested_s_scale; 3665 tp->request_r_scale = sc->sc_request_r_scale; 3666 tp->snd_scale = sc->sc_requested_s_scale; 3667 tp->rcv_scale = sc->sc_request_r_scale; 3668 tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE; 3669 } 3670 if (sc->sc_flags & SCF_TIMESTAMP) 3671 tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP; 3672 3673 tp->t_template = tcp_template(tp); 3674 if (tp->t_template == 0) { 3675 tp = tcp_drop(tp, ENOBUFS); /* destroys socket */ 3676 so = NULL; 3677 m_freem(m); 3678 goto abort; 3679 } 3680 #ifdef TCP_SACK 3681 tp->sack_enable = sc->sc_flags & SCF_SACK_PERMIT; 3682 #endif 3683 3684 tp->ts_modulate = sc->sc_modulate; 3685 tp->iss = sc->sc_iss; 3686 tp->irs = sc->sc_irs; 3687 tcp_sendseqinit(tp); 3688 #if defined (TCP_SACK) || defined(TCP_ECN) 3689 tp->snd_last = tp->snd_una; 3690 #endif /* TCP_SACK */ 3691 #if defined(TCP_SACK) && defined(TCP_FACK) 3692 tp->snd_fack = tp->snd_una; 3693 tp->retran_data = 0; 3694 tp->snd_awnd = 0; 3695 #endif /* TCP_FACK */ 3696 #ifdef TCP_ECN 3697 if (sc->sc_flags & SCF_ECN_PERMIT) { 3698 tp->t_flags |= TF_ECN_PERMIT; 3699 tcpstat.tcps_ecn_accepts++; 3700 } 3701 #endif 3702 #ifdef TCP_SACK 3703 if (sc->sc_flags & SCF_SACK_PERMIT) 3704 tp->t_flags |= TF_SACK_PERMIT; 3705 #endif 3706 #ifdef TCP_SIGNATURE 3707 if (sc->sc_flags & SCF_SIGNATURE) 3708 tp->t_flags |= TF_SIGNATURE; 3709 #endif 3710 tcp_rcvseqinit(tp); 3711 tp->t_state = TCPS_SYN_RECEIVED; 3712 tp->t_rcvtime = tcp_now; 3713 TCP_TIMER_ARM(tp, TCPT_KEEP, tcptv_keep_init); 3714 tcpstat.tcps_accepts++; 3715 3716 tcp_mss(tp, sc->sc_peermaxseg); /* sets t_maxseg */ 3717 if (sc->sc_peermaxseg) 3718 tcp_mss_update(tp); 3719 /* Reset initial window to 1 segment for retransmit */ 3720 if (sc->sc_rxtshift > 0) 3721 tp->snd_cwnd = tp->t_maxseg; 3722 tp->snd_wl1 = sc->sc_irs; 3723 tp->rcv_up = sc->sc_irs + 1; 3724 3725 /* 3726 * This is what whould have happened in tcp_output() when 3727 * the SYN,ACK was sent. 3728 */ 3729 tp->snd_up = tp->snd_una; 3730 tp->snd_max = tp->snd_nxt = tp->iss+1; 3731 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur); 3732 if (sc->sc_win > 0 && SEQ_GT(tp->rcv_nxt + sc->sc_win, tp->rcv_adv)) 3733 tp->rcv_adv = tp->rcv_nxt + sc->sc_win; 3734 tp->last_ack_sent = tp->rcv_nxt; 3735 3736 tcpstat.tcps_sc_completed++; 3737 SYN_CACHE_PUT(sc); 3738 return (so); 3739 3740 resetandabort: 3741 tcp_respond(NULL, mtod(m, caddr_t), m, (tcp_seq)0, th->th_ack, TH_RST); 3742 abort: 3743 if (so != NULL) 3744 (void) soabort(so); 3745 SYN_CACHE_PUT(sc); 3746 tcpstat.tcps_sc_aborted++; 3747 return ((struct socket *)(-1)); 3748 } 3749 3750 /* 3751 * This function is called when we get a RST for a 3752 * non-existent connection, so that we can see if the 3753 * connection is in the syn cache. If it is, zap it. 3754 */ 3755 3756 void 3757 syn_cache_reset(src, dst, th) 3758 struct sockaddr *src; 3759 struct sockaddr *dst; 3760 struct tcphdr *th; 3761 { 3762 struct syn_cache *sc; 3763 struct syn_cache_head *scp; 3764 int s = splsoftnet(); 3765 3766 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) { 3767 splx(s); 3768 return; 3769 } 3770 if (SEQ_LT(th->th_seq, sc->sc_irs) || 3771 SEQ_GT(th->th_seq, sc->sc_irs+1)) { 3772 splx(s); 3773 return; 3774 } 3775 SYN_CACHE_RM(sc); 3776 splx(s); 3777 tcpstat.tcps_sc_reset++; 3778 SYN_CACHE_PUT(sc); 3779 } 3780 3781 void 3782 syn_cache_unreach(src, dst, th) 3783 struct sockaddr *src; 3784 struct sockaddr *dst; 3785 struct tcphdr *th; 3786 { 3787 struct syn_cache *sc; 3788 struct syn_cache_head *scp; 3789 int s; 3790 3791 s = splsoftnet(); 3792 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) { 3793 splx(s); 3794 return; 3795 } 3796 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */ 3797 if (ntohl (th->th_seq) != sc->sc_iss) { 3798 splx(s); 3799 return; 3800 } 3801 3802 /* 3803 * If we've retransmitted 3 times and this is our second error, 3804 * we remove the entry. Otherwise, we allow it to continue on. 3805 * This prevents us from incorrectly nuking an entry during a 3806 * spurious network outage. 3807 * 3808 * See tcp_notify(). 3809 */ 3810 if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtshift < 3) { 3811 sc->sc_flags |= SCF_UNREACH; 3812 splx(s); 3813 return; 3814 } 3815 3816 SYN_CACHE_RM(sc); 3817 splx(s); 3818 tcpstat.tcps_sc_unreach++; 3819 SYN_CACHE_PUT(sc); 3820 } 3821 3822 /* 3823 * Given a LISTEN socket and an inbound SYN request, add 3824 * this to the syn cache, and send back a segment: 3825 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 3826 * to the source. 3827 * 3828 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN. 3829 * Doing so would require that we hold onto the data and deliver it 3830 * to the application. However, if we are the target of a SYN-flood 3831 * DoS attack, an attacker could send data which would eventually 3832 * consume all available buffer space if it were ACKed. By not ACKing 3833 * the data, we avoid this DoS scenario. 3834 */ 3835 3836 int 3837 syn_cache_add(src, dst, th, iphlen, so, m, optp, optlen, oi) 3838 struct sockaddr *src; 3839 struct sockaddr *dst; 3840 struct tcphdr *th; 3841 unsigned int iphlen; 3842 struct socket *so; 3843 struct mbuf *m; 3844 u_char *optp; 3845 int optlen; 3846 struct tcp_opt_info *oi; 3847 { 3848 struct tcpcb tb, *tp; 3849 long win; 3850 struct syn_cache *sc; 3851 struct syn_cache_head *scp; 3852 struct mbuf *ipopts; 3853 3854 tp = sototcpcb(so); 3855 3856 /* 3857 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 3858 * 3859 * Note this check is performed in tcp_input() very early on. 3860 */ 3861 3862 /* 3863 * Initialize some local state. 3864 */ 3865 win = sbspace(&so->so_rcv); 3866 if (win > TCP_MAXWIN) 3867 win = TCP_MAXWIN; 3868 3869 #ifdef TCP_SIGNATURE 3870 if (optp || (tp->t_flags & TF_SIGNATURE)) { 3871 #else 3872 if (optp) { 3873 #endif 3874 tb.pf = tp->pf; 3875 #ifdef TCP_SACK 3876 tb.sack_enable = tcp_do_sack; 3877 #endif 3878 tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0; 3879 #ifdef TCP_SIGNATURE 3880 tb.t_state = TCPS_LISTEN; 3881 if (tp->t_flags & TF_SIGNATURE) 3882 tb.t_flags |= TF_SIGNATURE; 3883 #endif 3884 if (tcp_dooptions(&tb, optp, optlen, th, m, iphlen, oi)) 3885 return (0); 3886 } else 3887 tb.t_flags = 0; 3888 3889 switch (src->sa_family) { 3890 #ifdef INET 3891 case AF_INET: 3892 /* 3893 * Remember the IP options, if any. 3894 */ 3895 ipopts = ip_srcroute(); 3896 break; 3897 #endif 3898 default: 3899 ipopts = NULL; 3900 } 3901 3902 /* 3903 * See if we already have an entry for this connection. 3904 * If we do, resend the SYN,ACK. We do not count this 3905 * as a retransmission (XXX though maybe we should). 3906 */ 3907 if ((sc = syn_cache_lookup(src, dst, &scp)) != NULL) { 3908 tcpstat.tcps_sc_dupesyn++; 3909 if (ipopts) { 3910 /* 3911 * If we were remembering a previous source route, 3912 * forget it and use the new one we've been given. 3913 */ 3914 if (sc->sc_ipopts) 3915 (void) m_free(sc->sc_ipopts); 3916 sc->sc_ipopts = ipopts; 3917 } 3918 sc->sc_timestamp = tb.ts_recent; 3919 if (syn_cache_respond(sc, m) == 0) { 3920 tcpstat.tcps_sndacks++; 3921 tcpstat.tcps_sndtotal++; 3922 } 3923 return (1); 3924 } 3925 3926 sc = pool_get(&syn_cache_pool, PR_NOWAIT); 3927 if (sc == NULL) { 3928 if (ipopts) 3929 (void) m_free(ipopts); 3930 return (0); 3931 } 3932 3933 /* 3934 * Fill in the cache, and put the necessary IP and TCP 3935 * options into the reply. 3936 */ 3937 bzero(sc, sizeof(struct syn_cache)); 3938 bzero(&sc->sc_timer, sizeof(sc->sc_timer)); 3939 bcopy(src, &sc->sc_src, src->sa_len); 3940 bcopy(dst, &sc->sc_dst, dst->sa_len); 3941 sc->sc_flags = 0; 3942 sc->sc_ipopts = ipopts; 3943 sc->sc_irs = th->th_seq; 3944 3945 #ifdef TCP_COMPAT_42 3946 tcp_iss += TCP_ISSINCR/2; 3947 sc->sc_iss = tcp_iss; 3948 #else 3949 sc->sc_iss = tcp_rndiss_next(); 3950 #endif 3951 sc->sc_peermaxseg = oi->maxseg; 3952 sc->sc_ourmaxseg = tcp_mss_adv(m->m_flags & M_PKTHDR ? 3953 m->m_pkthdr.rcvif : NULL, sc->sc_src.sa.sa_family); 3954 sc->sc_win = win; 3955 sc->sc_timestamp = tb.ts_recent; 3956 if ((tb.t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP)) == 3957 (TF_REQ_TSTMP|TF_RCVD_TSTMP)) 3958 sc->sc_flags |= SCF_TIMESTAMP; 3959 if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 3960 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 3961 sc->sc_requested_s_scale = tb.requested_s_scale; 3962 sc->sc_request_r_scale = 0; 3963 while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT && 3964 TCP_MAXWIN << sc->sc_request_r_scale < 3965 so->so_rcv.sb_hiwat) 3966 sc->sc_request_r_scale++; 3967 } else { 3968 sc->sc_requested_s_scale = 15; 3969 sc->sc_request_r_scale = 15; 3970 } 3971 #ifdef TCP_ECN 3972 /* 3973 * if both ECE and CWR flag bits are set, peer is ECN capable. 3974 */ 3975 if (tcp_do_ecn && 3976 (th->th_flags & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) 3977 sc->sc_flags |= SCF_ECN_PERMIT; 3978 #endif 3979 #ifdef TCP_SACK 3980 /* 3981 * Set SCF_SACK_PERMIT if peer did send a SACK_PERMITTED option 3982 * (i.e., if tcp_dooptions() did set TF_SACK_PERMIT). 3983 */ 3984 if (tb.sack_enable && (tb.t_flags & TF_SACK_PERMIT)) 3985 sc->sc_flags |= SCF_SACK_PERMIT; 3986 #endif 3987 #ifdef TCP_SIGNATURE 3988 if (tb.t_flags & TF_SIGNATURE) 3989 sc->sc_flags |= SCF_SIGNATURE; 3990 #endif 3991 sc->sc_tp = tp; 3992 if (syn_cache_respond(sc, m) == 0) { 3993 syn_cache_insert(sc, tp); 3994 tcpstat.tcps_sndacks++; 3995 tcpstat.tcps_sndtotal++; 3996 } else { 3997 SYN_CACHE_PUT(sc); 3998 tcpstat.tcps_sc_dropped++; 3999 } 4000 return (1); 4001 } 4002 4003 int 4004 syn_cache_respond(sc, m) 4005 struct syn_cache *sc; 4006 struct mbuf *m; 4007 { 4008 struct route *ro; 4009 u_int8_t *optp; 4010 int optlen, error; 4011 u_int16_t tlen; 4012 struct ip *ip = NULL; 4013 #ifdef INET6 4014 struct ip6_hdr *ip6 = NULL; 4015 #endif 4016 struct tcphdr *th; 4017 u_int hlen; 4018 struct inpcb *inp; 4019 4020 switch (sc->sc_src.sa.sa_family) { 4021 case AF_INET: 4022 hlen = sizeof(struct ip); 4023 ro = &sc->sc_route4; 4024 break; 4025 #ifdef INET6 4026 case AF_INET6: 4027 hlen = sizeof(struct ip6_hdr); 4028 ro = (struct route *)&sc->sc_route6; 4029 break; 4030 #endif 4031 default: 4032 if (m) 4033 m_freem(m); 4034 return (EAFNOSUPPORT); 4035 } 4036 4037 /* Compute the size of the TCP options. */ 4038 optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) + 4039 #ifdef TCP_SACK 4040 ((sc->sc_flags & SCF_SACK_PERMIT) ? 4 : 0) + 4041 #endif 4042 #ifdef TCP_SIGNATURE 4043 ((sc->sc_flags & SCF_SIGNATURE) ? TCPOLEN_SIGLEN : 0) + 4044 #endif 4045 ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0); 4046 4047 tlen = hlen + sizeof(struct tcphdr) + optlen; 4048 4049 /* 4050 * Create the IP+TCP header from scratch. 4051 */ 4052 if (m) 4053 m_freem(m); 4054 #ifdef DIAGNOSTIC 4055 if (max_linkhdr + tlen > MCLBYTES) 4056 return (ENOBUFS); 4057 #endif 4058 MGETHDR(m, M_DONTWAIT, MT_DATA); 4059 if (m && max_linkhdr + tlen > MHLEN) { 4060 MCLGET(m, M_DONTWAIT); 4061 if ((m->m_flags & M_EXT) == 0) { 4062 m_freem(m); 4063 m = NULL; 4064 } 4065 } 4066 if (m == NULL) 4067 return (ENOBUFS); 4068 4069 /* Fixup the mbuf. */ 4070 m->m_data += max_linkhdr; 4071 m->m_len = m->m_pkthdr.len = tlen; 4072 m->m_pkthdr.rcvif = NULL; 4073 memset(mtod(m, u_char *), 0, tlen); 4074 4075 switch (sc->sc_src.sa.sa_family) { 4076 case AF_INET: 4077 ip = mtod(m, struct ip *); 4078 ip->ip_dst = sc->sc_src.sin.sin_addr; 4079 ip->ip_src = sc->sc_dst.sin.sin_addr; 4080 ip->ip_p = IPPROTO_TCP; 4081 th = (struct tcphdr *)(ip + 1); 4082 th->th_dport = sc->sc_src.sin.sin_port; 4083 th->th_sport = sc->sc_dst.sin.sin_port; 4084 break; 4085 #ifdef INET6 4086 case AF_INET6: 4087 ip6 = mtod(m, struct ip6_hdr *); 4088 ip6->ip6_dst = sc->sc_src.sin6.sin6_addr; 4089 ip6->ip6_src = sc->sc_dst.sin6.sin6_addr; 4090 ip6->ip6_nxt = IPPROTO_TCP; 4091 /* ip6_plen will be updated in ip6_output() */ 4092 th = (struct tcphdr *)(ip6 + 1); 4093 th->th_dport = sc->sc_src.sin6.sin6_port; 4094 th->th_sport = sc->sc_dst.sin6.sin6_port; 4095 break; 4096 #endif 4097 default: 4098 th = NULL; 4099 } 4100 4101 th->th_seq = htonl(sc->sc_iss); 4102 th->th_ack = htonl(sc->sc_irs + 1); 4103 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 4104 th->th_flags = TH_SYN|TH_ACK; 4105 #ifdef TCP_ECN 4106 /* Set ECE for SYN-ACK if peer supports ECN. */ 4107 if (tcp_do_ecn && (sc->sc_flags & SCF_ECN_PERMIT)) 4108 th->th_flags |= TH_ECE; 4109 #endif 4110 th->th_win = htons(sc->sc_win); 4111 /* th_sum already 0 */ 4112 /* th_urp already 0 */ 4113 4114 /* Tack on the TCP options. */ 4115 optp = (u_int8_t *)(th + 1); 4116 *optp++ = TCPOPT_MAXSEG; 4117 *optp++ = 4; 4118 *optp++ = (sc->sc_ourmaxseg >> 8) & 0xff; 4119 *optp++ = sc->sc_ourmaxseg & 0xff; 4120 4121 #ifdef TCP_SACK 4122 /* Include SACK_PERMIT_HDR option if peer has already done so. */ 4123 if (sc->sc_flags & SCF_SACK_PERMIT) { 4124 *((u_int32_t *)optp) = htonl(TCPOPT_SACK_PERMIT_HDR); 4125 optp += 4; 4126 } 4127 #endif 4128 4129 if (sc->sc_request_r_scale != 15) { 4130 *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 | 4131 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 | 4132 sc->sc_request_r_scale); 4133 optp += 4; 4134 } 4135 4136 if (sc->sc_flags & SCF_TIMESTAMP) { 4137 u_int32_t *lp = (u_int32_t *)(optp); 4138 /* Form timestamp option as shown in appendix A of RFC 1323. */ 4139 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 4140 sc->sc_modulate = arc4random(); 4141 *lp++ = htonl(SYN_CACHE_TIMESTAMP(sc)); 4142 *lp = htonl(sc->sc_timestamp); 4143 optp += TCPOLEN_TSTAMP_APPA; 4144 } 4145 4146 #ifdef TCP_SIGNATURE 4147 if (sc->sc_flags & SCF_SIGNATURE) { 4148 union sockaddr_union src, dst; 4149 struct tdb *tdb; 4150 4151 bzero(&src, sizeof(union sockaddr_union)); 4152 bzero(&dst, sizeof(union sockaddr_union)); 4153 src.sa.sa_len = sc->sc_src.sa.sa_len; 4154 src.sa.sa_family = sc->sc_src.sa.sa_family; 4155 dst.sa.sa_len = sc->sc_dst.sa.sa_len; 4156 dst.sa.sa_family = sc->sc_dst.sa.sa_family; 4157 4158 switch (sc->sc_src.sa.sa_family) { 4159 case 0: /*default to PF_INET*/ 4160 #ifdef INET 4161 case AF_INET: 4162 src.sin.sin_addr = mtod(m, struct ip *)->ip_src; 4163 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst; 4164 break; 4165 #endif /* INET */ 4166 #ifdef INET6 4167 case AF_INET6: 4168 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src; 4169 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst; 4170 break; 4171 #endif /* INET6 */ 4172 } 4173 4174 tdb = gettdbbysrcdst(0, &src, &dst, IPPROTO_TCP); 4175 if (tdb == NULL) { 4176 if (m) 4177 m_freem(m); 4178 return (EPERM); 4179 } 4180 4181 /* Send signature option */ 4182 *(optp++) = TCPOPT_SIGNATURE; 4183 *(optp++) = TCPOLEN_SIGNATURE; 4184 4185 if (tcp_signature(tdb, sc->sc_src.sa.sa_family, m, th, 4186 hlen, 0, optp) < 0) { 4187 if (m) 4188 m_freem(m); 4189 return (EINVAL); 4190 } 4191 optp += 16; 4192 4193 /* Pad options list to the next 32 bit boundary and 4194 * terminate it. 4195 */ 4196 *optp++ = TCPOPT_NOP; 4197 *optp++ = TCPOPT_EOL; 4198 } 4199 #endif /* TCP_SIGNATURE */ 4200 4201 /* Compute the packet's checksum. */ 4202 switch (sc->sc_src.sa.sa_family) { 4203 case AF_INET: 4204 ip->ip_len = htons(tlen - hlen); 4205 th->th_sum = 0; 4206 th->th_sum = in_cksum(m, tlen); 4207 break; 4208 #ifdef INET6 4209 case AF_INET6: 4210 ip6->ip6_plen = htons(tlen - hlen); 4211 th->th_sum = 0; 4212 th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen); 4213 break; 4214 #endif 4215 } 4216 4217 /* 4218 * Fill in some straggling IP bits. Note the stack expects 4219 * ip_len to be in host order, for convenience. 4220 */ 4221 switch (sc->sc_src.sa.sa_family) { 4222 #ifdef INET 4223 case AF_INET: 4224 ip->ip_len = htons(tlen); 4225 ip->ip_ttl = ip_defttl; 4226 /* XXX tos? */ 4227 break; 4228 #endif 4229 #ifdef INET6 4230 case AF_INET6: 4231 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 4232 ip6->ip6_vfc |= IPV6_VERSION; 4233 ip6->ip6_plen = htons(tlen - hlen); 4234 /* ip6_hlim will be initialized afterwards */ 4235 /* leave flowlabel = 0, it is legal and require no state mgmt */ 4236 break; 4237 #endif 4238 } 4239 4240 /* use IPsec policy from listening socket, on SYN ACK */ 4241 inp = sc->sc_tp ? sc->sc_tp->t_inpcb : NULL; 4242 4243 switch (sc->sc_src.sa.sa_family) { 4244 #ifdef INET 4245 case AF_INET: 4246 error = ip_output(m, sc->sc_ipopts, ro, 4247 (ip_mtudisc ? IP_MTUDISC : 0), 4248 (struct ip_moptions *)NULL, inp); 4249 break; 4250 #endif 4251 #ifdef INET6 4252 case AF_INET6: 4253 ip6->ip6_hlim = in6_selecthlim(NULL, 4254 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL); 4255 4256 error = ip6_output(m, NULL /*XXX*/, (struct route_in6 *)ro, 0, 4257 (struct ip6_moptions *)0, NULL); 4258 break; 4259 #endif 4260 default: 4261 error = EAFNOSUPPORT; 4262 break; 4263 } 4264 return (error); 4265 } 4266