tcp_input.c revision 125740
1/* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 34 * $FreeBSD: head/sys/netinet/tcp_input.c 125740 2004-02-12 20:08:28Z bms $ 35 */ 36 37#include "opt_ipfw.h" /* for ipfw_fwd */ 38#include "opt_inet.h" 39#include "opt_inet6.h" 40#include "opt_ipsec.h" 41#include "opt_mac.h" 42#include "opt_tcpdebug.h" 43#include "opt_tcp_input.h" 44 45#include <sys/param.h> 46#include <sys/kernel.h> 47#include <sys/mac.h> 48#include <sys/malloc.h> 49#include <sys/mbuf.h> 50#include <sys/proc.h> /* for proc0 declaration */ 51#include <sys/protosw.h> 52#include <sys/signalvar.h> 53#include <sys/socket.h> 54#include <sys/socketvar.h> 55#include <sys/sysctl.h> 56#include <sys/syslog.h> 57#include <sys/systm.h> 58 59#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 60 61#include <net/if.h> 62#include <net/route.h> 63 64#include <netinet/in.h> 65#include <netinet/in_pcb.h> 66#include <netinet/in_systm.h> 67#include <netinet/in_var.h> 68#include <netinet/ip.h> 69#include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 70#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 71#include <netinet/ip_var.h> 72#include <netinet/ip6.h> 73#include <netinet/icmp6.h> 74#include <netinet6/in6_pcb.h> 75#include <netinet6/ip6_var.h> 76#include <netinet6/nd6.h> 77#include <netinet/tcp.h> 78#include <netinet/tcp_fsm.h> 79#include <netinet/tcp_seq.h> 80#include <netinet/tcp_timer.h> 81#include <netinet/tcp_var.h> 82#include <netinet6/tcp6_var.h> 83#include <netinet/tcpip.h> 84#ifdef TCPDEBUG 85#include <netinet/tcp_debug.h> 86#endif /* TCPDEBUG */ 87 88#ifdef FAST_IPSEC 89#include <netipsec/ipsec.h> 90#include <netipsec/ipsec6.h> 91#endif /*FAST_IPSEC*/ 92 93#ifdef IPSEC 94#include <netinet6/ipsec.h> 95#include <netinet6/ipsec6.h> 96#include <netkey/key.h> 97#endif /*IPSEC*/ 98 99#include <machine/in_cksum.h> 100 101MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry"); 102 103static const int tcprexmtthresh = 3; 104tcp_cc tcp_ccgen; 105 106struct tcpstat tcpstat; 107SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 108 &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 109 110static int log_in_vain = 0; 111SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 112 &log_in_vain, 0, "Log all incoming TCP connections"); 113 114static int blackhole = 0; 115SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 116 &blackhole, 0, "Do not send RST when dropping refused connections"); 117 118int tcp_delack_enabled = 1; 119SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 120 &tcp_delack_enabled, 0, 121 "Delay ACK to try and piggyback it onto a data packet"); 122 123#ifdef TCP_DROP_SYNFIN 124static int drop_synfin = 0; 125SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 126 &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 127#endif 128 129static int tcp_do_rfc3042 = 1; 130SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 131 &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 132 133static int tcp_do_rfc3390 = 1; 134SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 135 &tcp_do_rfc3390, 0, 136 "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 137 138struct inpcbhead tcb; 139#define tcb6 tcb /* for KAME src sync over BSD*'s */ 140struct inpcbinfo tcbinfo; 141struct mtx *tcbinfo_mtx; 142 143static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 144static void tcp_pulloutofband(struct socket *, 145 struct tcphdr *, struct mbuf *, int); 146static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, 147 struct mbuf *); 148static void tcp_xmit_timer(struct tcpcb *, int); 149static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 150static int tcp_timewait(struct tcptw *, struct tcpopt *, 151 struct tcphdr *, struct mbuf *, int); 152 153/* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 154#ifdef INET6 155#define ND6_HINT(tp) \ 156do { \ 157 if ((tp) && (tp)->t_inpcb && \ 158 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 159 nd6_nud_hint(NULL, NULL, 0); \ 160} while (0) 161#else 162#define ND6_HINT(tp) 163#endif 164 165/* 166 * Indicate whether this ack should be delayed. We can delay the ack if 167 * - there is no delayed ack timer in progress and 168 * - our last ack wasn't a 0-sized window. We never want to delay 169 * the ack that opens up a 0-sized window and 170 * - delayed acks are enabled or 171 * - this is a half-synchronized T/TCP connection. 172 */ 173#define DELAY_ACK(tp) \ 174 ((!callout_active(tp->tt_delack) && \ 175 (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 176 (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 177 178static int 179tcp_reass(tp, th, tlenp, m) 180 register struct tcpcb *tp; 181 register struct tcphdr *th; 182 int *tlenp; 183 struct mbuf *m; 184{ 185 struct tseg_qent *q; 186 struct tseg_qent *p = NULL; 187 struct tseg_qent *nq; 188 struct tseg_qent *te; 189 struct socket *so = tp->t_inpcb->inp_socket; 190 int flags; 191 192 /* 193 * Call with th==0 after become established to 194 * force pre-ESTABLISHED data up to user socket. 195 */ 196 if (th == 0) 197 goto present; 198 199 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ 200 MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ, 201 M_NOWAIT); 202 if (te == NULL) { 203 tcpstat.tcps_rcvmemdrop++; 204 m_freem(m); 205 return (0); 206 } 207 208 /* 209 * Find a segment which begins after this one does. 210 */ 211 LIST_FOREACH(q, &tp->t_segq, tqe_q) { 212 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 213 break; 214 p = q; 215 } 216 217 /* 218 * If there is a preceding segment, it may provide some of 219 * our data already. If so, drop the data from the incoming 220 * segment. If it provides all of our data, drop us. 221 */ 222 if (p != NULL) { 223 register int i; 224 /* conversion to int (in i) handles seq wraparound */ 225 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 226 if (i > 0) { 227 if (i >= *tlenp) { 228 tcpstat.tcps_rcvduppack++; 229 tcpstat.tcps_rcvdupbyte += *tlenp; 230 m_freem(m); 231 FREE(te, M_TSEGQ); 232 /* 233 * Try to present any queued data 234 * at the left window edge to the user. 235 * This is needed after the 3-WHS 236 * completes. 237 */ 238 goto present; /* ??? */ 239 } 240 m_adj(m, i); 241 *tlenp -= i; 242 th->th_seq += i; 243 } 244 } 245 tcpstat.tcps_rcvoopack++; 246 tcpstat.tcps_rcvoobyte += *tlenp; 247 248 /* 249 * While we overlap succeeding segments trim them or, 250 * if they are completely covered, dequeue them. 251 */ 252 while (q) { 253 register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 254 if (i <= 0) 255 break; 256 if (i < q->tqe_len) { 257 q->tqe_th->th_seq += i; 258 q->tqe_len -= i; 259 m_adj(q->tqe_m, i); 260 break; 261 } 262 263 nq = LIST_NEXT(q, tqe_q); 264 LIST_REMOVE(q, tqe_q); 265 m_freem(q->tqe_m); 266 FREE(q, M_TSEGQ); 267 q = nq; 268 } 269 270 /* Insert the new segment queue entry into place. */ 271 te->tqe_m = m; 272 te->tqe_th = th; 273 te->tqe_len = *tlenp; 274 275 if (p == NULL) { 276 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 277 } else { 278 LIST_INSERT_AFTER(p, te, tqe_q); 279 } 280 281present: 282 /* 283 * Present data to user, advancing rcv_nxt through 284 * completed sequence space. 285 */ 286 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 287 return (0); 288 q = LIST_FIRST(&tp->t_segq); 289 if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 290 return (0); 291 do { 292 tp->rcv_nxt += q->tqe_len; 293 flags = q->tqe_th->th_flags & TH_FIN; 294 nq = LIST_NEXT(q, tqe_q); 295 LIST_REMOVE(q, tqe_q); 296 if (so->so_state & SS_CANTRCVMORE) 297 m_freem(q->tqe_m); 298 else 299 sbappendstream(&so->so_rcv, q->tqe_m); 300 FREE(q, M_TSEGQ); 301 q = nq; 302 } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 303 ND6_HINT(tp); 304 sorwakeup(so); 305 return (flags); 306} 307 308/* 309 * TCP input routine, follows pages 65-76 of the 310 * protocol specification dated September, 1981 very closely. 311 */ 312#ifdef INET6 313int 314tcp6_input(mp, offp, proto) 315 struct mbuf **mp; 316 int *offp, proto; 317{ 318 register struct mbuf *m = *mp; 319 struct in6_ifaddr *ia6; 320 321 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 322 323 /* 324 * draft-itojun-ipv6-tcp-to-anycast 325 * better place to put this in? 326 */ 327 ia6 = ip6_getdstifaddr(m); 328 if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 329 struct ip6_hdr *ip6; 330 331 ip6 = mtod(m, struct ip6_hdr *); 332 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 333 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 334 return IPPROTO_DONE; 335 } 336 337 tcp_input(m, *offp); 338 return IPPROTO_DONE; 339} 340#endif 341 342void 343tcp_input(m, off0) 344 register struct mbuf *m; 345 int off0; 346{ 347 register struct tcphdr *th; 348 register struct ip *ip = NULL; 349 register struct ipovly *ipov; 350 register struct inpcb *inp = NULL; 351 u_char *optp = NULL; 352 int optlen = 0; 353 int len, tlen, off; 354 int drop_hdrlen; 355 register struct tcpcb *tp = 0; 356 register int thflags; 357 struct socket *so = 0; 358 int todrop, acked, ourfinisacked, needoutput = 0; 359 u_long tiwin; 360 struct tcpopt to; /* options in this segment */ 361 struct rmxp_tao tao; /* our TAO cache entry */ 362 int headlocked = 0; 363 struct sockaddr_in *next_hop = NULL; 364 int rstreason; /* For badport_bandlim accounting purposes */ 365 366 struct ip6_hdr *ip6 = NULL; 367#ifdef INET6 368 int isipv6; 369#else 370 const int isipv6 = 0; 371#endif 372 373#ifdef TCPDEBUG 374 /* 375 * The size of tcp_saveipgen must be the size of the max ip header, 376 * now IPv6. 377 */ 378 u_char tcp_saveipgen[40]; 379 struct tcphdr tcp_savetcp; 380 short ostate = 0; 381#endif 382 383 /* Grab info from MT_TAG mbufs prepended to the chain. */ 384 for (;m && m->m_type == MT_TAG; m = m->m_next) { 385 if (m->_m_tag_id == PACKET_TAG_IPFORWARD) 386 next_hop = (struct sockaddr_in *)m->m_hdr.mh_data; 387 } 388#ifdef INET6 389 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 390#endif 391 bzero(&tao, sizeof(tao)); 392 bzero((char *)&to, sizeof(to)); 393 394 tcpstat.tcps_rcvtotal++; 395 396 if (isipv6) { 397 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 398 ip6 = mtod(m, struct ip6_hdr *); 399 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 400 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 401 tcpstat.tcps_rcvbadsum++; 402 goto drop; 403 } 404 th = (struct tcphdr *)((caddr_t)ip6 + off0); 405 406 /* 407 * Be proactive about unspecified IPv6 address in source. 408 * As we use all-zero to indicate unbounded/unconnected pcb, 409 * unspecified IPv6 address can be used to confuse us. 410 * 411 * Note that packets with unspecified IPv6 destination is 412 * already dropped in ip6_input. 413 */ 414 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 415 /* XXX stat */ 416 goto drop; 417 } 418 } else { 419 /* 420 * Get IP and TCP header together in first mbuf. 421 * Note: IP leaves IP header in first mbuf. 422 */ 423 if (off0 > sizeof (struct ip)) { 424 ip_stripoptions(m, (struct mbuf *)0); 425 off0 = sizeof(struct ip); 426 } 427 if (m->m_len < sizeof (struct tcpiphdr)) { 428 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 429 tcpstat.tcps_rcvshort++; 430 return; 431 } 432 } 433 ip = mtod(m, struct ip *); 434 ipov = (struct ipovly *)ip; 435 th = (struct tcphdr *)((caddr_t)ip + off0); 436 tlen = ip->ip_len; 437 438 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 439 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 440 th->th_sum = m->m_pkthdr.csum_data; 441 else 442 th->th_sum = in_pseudo(ip->ip_src.s_addr, 443 ip->ip_dst.s_addr, 444 htonl(m->m_pkthdr.csum_data + 445 ip->ip_len + 446 IPPROTO_TCP)); 447 th->th_sum ^= 0xffff; 448#ifdef TCPDEBUG 449 ipov->ih_len = (u_short)tlen; 450 ipov->ih_len = htons(ipov->ih_len); 451#endif 452 } else { 453 /* 454 * Checksum extended TCP header and data. 455 */ 456 len = sizeof (struct ip) + tlen; 457 bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 458 ipov->ih_len = (u_short)tlen; 459 ipov->ih_len = htons(ipov->ih_len); 460 th->th_sum = in_cksum(m, len); 461 } 462 if (th->th_sum) { 463 tcpstat.tcps_rcvbadsum++; 464 goto drop; 465 } 466#ifdef INET6 467 /* Re-initialization for later version check */ 468 ip->ip_v = IPVERSION; 469#endif 470 } 471 472 /* 473 * Check that TCP offset makes sense, 474 * pull out TCP options and adjust length. XXX 475 */ 476 off = th->th_off << 2; 477 if (off < sizeof (struct tcphdr) || off > tlen) { 478 tcpstat.tcps_rcvbadoff++; 479 goto drop; 480 } 481 tlen -= off; /* tlen is used instead of ti->ti_len */ 482 if (off > sizeof (struct tcphdr)) { 483 if (isipv6) { 484 IP6_EXTHDR_CHECK(m, off0, off, ); 485 ip6 = mtod(m, struct ip6_hdr *); 486 th = (struct tcphdr *)((caddr_t)ip6 + off0); 487 } else { 488 if (m->m_len < sizeof(struct ip) + off) { 489 if ((m = m_pullup(m, sizeof (struct ip) + off)) 490 == 0) { 491 tcpstat.tcps_rcvshort++; 492 return; 493 } 494 ip = mtod(m, struct ip *); 495 ipov = (struct ipovly *)ip; 496 th = (struct tcphdr *)((caddr_t)ip + off0); 497 } 498 } 499 optlen = off - sizeof (struct tcphdr); 500 optp = (u_char *)(th + 1); 501 } 502 thflags = th->th_flags; 503 504#ifdef TCP_DROP_SYNFIN 505 /* 506 * If the drop_synfin option is enabled, drop all packets with 507 * both the SYN and FIN bits set. This prevents e.g. nmap from 508 * identifying the TCP/IP stack. 509 * 510 * This is a violation of the TCP specification. 511 */ 512 if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 513 goto drop; 514#endif 515 516 /* 517 * Convert TCP protocol specific fields to host format. 518 */ 519 th->th_seq = ntohl(th->th_seq); 520 th->th_ack = ntohl(th->th_ack); 521 th->th_win = ntohs(th->th_win); 522 th->th_urp = ntohs(th->th_urp); 523 524 /* 525 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, 526 * until after ip6_savecontrol() is called and before other functions 527 * which don't want those proto headers. 528 * Because ip6_savecontrol() is going to parse the mbuf to 529 * search for data to be passed up to user-land, it wants mbuf 530 * parameters to be unchanged. 531 * XXX: the call of ip6_savecontrol() has been obsoleted based on 532 * latest version of the advanced API (20020110). 533 */ 534 drop_hdrlen = off0 + off; 535 536 /* 537 * Locate pcb for segment. 538 */ 539 INP_INFO_WLOCK(&tcbinfo); 540 headlocked = 1; 541findpcb: 542 /* IPFIREWALL_FORWARD section */ 543 if (next_hop != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 544 /* 545 * Transparently forwarded. Pretend to be the destination. 546 * already got one like this? 547 */ 548 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 549 ip->ip_dst, th->th_dport, 550 0, m->m_pkthdr.rcvif); 551 if (!inp) { 552 /* It's new. Try find the ambushing socket. */ 553 inp = in_pcblookup_hash(&tcbinfo, 554 ip->ip_src, th->th_sport, 555 next_hop->sin_addr, 556 next_hop->sin_port ? 557 ntohs(next_hop->sin_port) : 558 th->th_dport, 559 1, m->m_pkthdr.rcvif); 560 } 561 } else { 562 if (isipv6) 563 inp = in6_pcblookup_hash(&tcbinfo, 564 &ip6->ip6_src, th->th_sport, 565 &ip6->ip6_dst, th->th_dport, 566 1, m->m_pkthdr.rcvif); 567 else 568 inp = in_pcblookup_hash(&tcbinfo, 569 ip->ip_src, th->th_sport, 570 ip->ip_dst, th->th_dport, 571 1, m->m_pkthdr.rcvif); 572 } 573 574#ifdef IPSEC 575 if (isipv6) { 576 if (inp != NULL && ipsec6_in_reject(m, inp)) { 577 ipsec6stat.in_polvio++; 578 goto drop; 579 } 580 } else { 581 if (inp != NULL && ipsec4_in_reject(m, inp)) { 582 ipsecstat.in_polvio++; 583 goto drop; 584 } 585 } 586#endif 587#ifdef FAST_IPSEC 588 if (isipv6) { 589 if (inp != NULL && ipsec6_in_reject(m, inp)) { 590 goto drop; 591 } 592 } else 593 if (inp != NULL && ipsec4_in_reject(m, inp)) { 594 goto drop; 595 } 596#endif /*FAST_IPSEC*/ 597 598 /* 599 * If the state is CLOSED (i.e., TCB does not exist) then 600 * all data in the incoming segment is discarded. 601 * If the TCB exists but is in CLOSED state, it is embryonic, 602 * but should either do a listen or a connect soon. 603 */ 604 if (inp == NULL) { 605 if (log_in_vain) { 606#ifdef INET6 607 char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 608#else 609 char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 610#endif 611 612 if (isipv6) { 613 strcpy(dbuf, "["); 614 strcpy(sbuf, "["); 615 strcat(dbuf, ip6_sprintf(&ip6->ip6_dst)); 616 strcat(sbuf, ip6_sprintf(&ip6->ip6_src)); 617 strcat(dbuf, "]"); 618 strcat(sbuf, "]"); 619 } else { 620 strcpy(dbuf, inet_ntoa(ip->ip_dst)); 621 strcpy(sbuf, inet_ntoa(ip->ip_src)); 622 } 623 switch (log_in_vain) { 624 case 1: 625 if ((thflags & TH_SYN) == 0) 626 break; 627 /* FALLTHROUGH */ 628 case 2: 629 log(LOG_INFO, 630 "Connection attempt to TCP %s:%d " 631 "from %s:%d flags:0x%02x\n", 632 dbuf, ntohs(th->th_dport), sbuf, 633 ntohs(th->th_sport), thflags); 634 break; 635 default: 636 break; 637 } 638 } 639 if (blackhole) { 640 switch (blackhole) { 641 case 1: 642 if (thflags & TH_SYN) 643 goto drop; 644 break; 645 case 2: 646 goto drop; 647 default: 648 goto drop; 649 } 650 } 651 rstreason = BANDLIM_RST_CLOSEDPORT; 652 goto dropwithreset; 653 } 654 INP_LOCK(inp); 655 if (inp->inp_vflag & INP_TIMEWAIT) { 656 /* 657 * The only option of relevance is TOF_CC, and only if 658 * present in a SYN segment. See tcp_timewait(). 659 */ 660 if (thflags & TH_SYN) 661 tcp_dooptions(&to, optp, optlen, 1); 662 if (tcp_timewait((struct tcptw *)inp->inp_ppcb, 663 &to, th, m, tlen)) 664 goto findpcb; 665 /* 666 * tcp_timewait unlocks inp. 667 */ 668 INP_INFO_WUNLOCK(&tcbinfo); 669 return; 670 } 671 tp = intotcpcb(inp); 672 if (tp == 0) { 673 INP_UNLOCK(inp); 674 rstreason = BANDLIM_RST_CLOSEDPORT; 675 goto dropwithreset; 676 } 677 if (tp->t_state == TCPS_CLOSED) 678 goto drop; 679 680 /* Unscale the window into a 32-bit value. */ 681 if ((thflags & TH_SYN) == 0) 682 tiwin = th->th_win << tp->snd_scale; 683 else 684 tiwin = th->th_win; 685 686#ifdef MAC 687 if (mac_check_inpcb_deliver(inp, m)) 688 goto drop; 689#endif 690 so = inp->inp_socket; 691#ifdef TCPDEBUG 692 if (so->so_options & SO_DEBUG) { 693 ostate = tp->t_state; 694 if (isipv6) 695 bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 696 else 697 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 698 tcp_savetcp = *th; 699 } 700#endif 701 if (so->so_options & SO_ACCEPTCONN) { 702 struct in_conninfo inc; 703 704#ifdef INET6 705 inc.inc_isipv6 = isipv6; 706#endif 707 if (isipv6) { 708 inc.inc6_faddr = ip6->ip6_src; 709 inc.inc6_laddr = ip6->ip6_dst; 710 } else { 711 inc.inc_faddr = ip->ip_src; 712 inc.inc_laddr = ip->ip_dst; 713 } 714 inc.inc_fport = th->th_sport; 715 inc.inc_lport = th->th_dport; 716 717 /* 718 * If the state is LISTEN then ignore segment if it contains 719 * a RST. If the segment contains an ACK then it is bad and 720 * send a RST. If it does not contain a SYN then it is not 721 * interesting; drop it. 722 * 723 * If the state is SYN_RECEIVED (syncache) and seg contains 724 * an ACK, but not for our SYN/ACK, send a RST. If the seg 725 * contains a RST, check the sequence number to see if it 726 * is a valid reset segment. 727 */ 728 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 729 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 730 if (!syncache_expand(&inc, th, &so, m)) { 731 /* 732 * No syncache entry, or ACK was not 733 * for our SYN/ACK. Send a RST. 734 */ 735 tcpstat.tcps_badsyn++; 736 rstreason = BANDLIM_RST_OPENPORT; 737 goto dropwithreset; 738 } 739 if (so == NULL) { 740 /* 741 * Could not complete 3-way handshake, 742 * connection is being closed down, and 743 * syncache will free mbuf. 744 */ 745 INP_UNLOCK(inp); 746 INP_INFO_WUNLOCK(&tcbinfo); 747 return; 748 } 749 /* 750 * Socket is created in state SYN_RECEIVED. 751 * Continue processing segment. 752 */ 753 INP_UNLOCK(inp); 754 inp = sotoinpcb(so); 755 INP_LOCK(inp); 756 tp = intotcpcb(inp); 757 /* 758 * This is what would have happened in 759 * tcp_output() when the SYN,ACK was sent. 760 */ 761 tp->snd_up = tp->snd_una; 762 tp->snd_max = tp->snd_nxt = tp->iss + 1; 763 tp->last_ack_sent = tp->rcv_nxt; 764 /* 765 * RFC1323: The window in SYN & SYN/ACK 766 * segments is never scaled. 767 */ 768 tp->snd_wnd = tiwin; /* unscaled */ 769 goto after_listen; 770 } 771 if (thflags & TH_RST) { 772 syncache_chkrst(&inc, th); 773 goto drop; 774 } 775 if (thflags & TH_ACK) { 776 syncache_badack(&inc); 777 tcpstat.tcps_badsyn++; 778 rstreason = BANDLIM_RST_OPENPORT; 779 goto dropwithreset; 780 } 781 goto drop; 782 } 783 784 /* 785 * Segment's flags are (SYN) or (SYN|FIN). 786 */ 787#ifdef INET6 788 /* 789 * If deprecated address is forbidden, 790 * we do not accept SYN to deprecated interface 791 * address to prevent any new inbound connection from 792 * getting established. 793 * When we do not accept SYN, we send a TCP RST, 794 * with deprecated source address (instead of dropping 795 * it). We compromise it as it is much better for peer 796 * to send a RST, and RST will be the final packet 797 * for the exchange. 798 * 799 * If we do not forbid deprecated addresses, we accept 800 * the SYN packet. RFC2462 does not suggest dropping 801 * SYN in this case. 802 * If we decipher RFC2462 5.5.4, it says like this: 803 * 1. use of deprecated addr with existing 804 * communication is okay - "SHOULD continue to be 805 * used" 806 * 2. use of it with new communication: 807 * (2a) "SHOULD NOT be used if alternate address 808 * with sufficient scope is available" 809 * (2b) nothing mentioned otherwise. 810 * Here we fall into (2b) case as we have no choice in 811 * our source address selection - we must obey the peer. 812 * 813 * The wording in RFC2462 is confusing, and there are 814 * multiple description text for deprecated address 815 * handling - worse, they are not exactly the same. 816 * I believe 5.5.4 is the best one, so we follow 5.5.4. 817 */ 818 if (isipv6 && !ip6_use_deprecated) { 819 struct in6_ifaddr *ia6; 820 821 if ((ia6 = ip6_getdstifaddr(m)) && 822 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 823 INP_UNLOCK(inp); 824 tp = NULL; 825 rstreason = BANDLIM_RST_OPENPORT; 826 goto dropwithreset; 827 } 828 } 829#endif 830 /* 831 * If it is from this socket, drop it, it must be forged. 832 * Don't bother responding if the destination was a broadcast. 833 */ 834 if (th->th_dport == th->th_sport) { 835 if (isipv6) { 836 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 837 &ip6->ip6_src)) 838 goto drop; 839 } else { 840 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 841 goto drop; 842 } 843 } 844 /* 845 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 846 * 847 * Note that it is quite possible to receive unicast 848 * link-layer packets with a broadcast IP address. Use 849 * in_broadcast() to find them. 850 */ 851 if (m->m_flags & (M_BCAST|M_MCAST)) 852 goto drop; 853 if (isipv6) { 854 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 855 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 856 goto drop; 857 } else { 858 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 859 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 860 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 861 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 862 goto drop; 863 } 864 /* 865 * SYN appears to be valid; create compressed TCP state 866 * for syncache, or perform t/tcp connection. 867 */ 868 if (so->so_qlen <= so->so_qlimit) { 869#ifdef TCPDEBUG 870 if (so->so_options & SO_DEBUG) 871 tcp_trace(TA_INPUT, ostate, tp, 872 (void *)tcp_saveipgen, &tcp_savetcp, 0); 873#endif 874 tcp_dooptions(&to, optp, optlen, 1); 875 if (!syncache_add(&inc, &to, th, &so, m)) 876 goto drop; 877 if (so == NULL) { 878 /* 879 * Entry added to syncache, mbuf used to 880 * send SYN,ACK packet. 881 */ 882 KASSERT(headlocked, ("headlocked")); 883 INP_UNLOCK(inp); 884 INP_INFO_WUNLOCK(&tcbinfo); 885 return; 886 } 887 /* 888 * Segment passed TAO tests. 889 */ 890 INP_UNLOCK(inp); 891 inp = sotoinpcb(so); 892 INP_LOCK(inp); 893 tp = intotcpcb(inp); 894 tp->snd_wnd = tiwin; 895 tp->t_starttime = ticks; 896 tp->t_state = TCPS_ESTABLISHED; 897 898 /* 899 * T/TCP logic: 900 * If there is a FIN or if there is data, then 901 * delay SYN,ACK(SYN) in the hope of piggy-backing 902 * it on a response segment. Otherwise must send 903 * ACK now in case the other side is slow starting. 904 */ 905 if (thflags & TH_FIN || tlen != 0) 906 tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 907 else 908 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 909 tcpstat.tcps_connects++; 910 soisconnected(so); 911 goto trimthenstep6; 912 } 913 goto drop; 914 } 915after_listen: 916 917 /* XXX temp debugging */ 918 /* should not happen - syncache should pick up these connections */ 919 if (tp->t_state == TCPS_LISTEN) 920 panic("tcp_input: TCPS_LISTEN"); 921 922 /* 923 * This is the second part of the MSS DoS prevention code (after 924 * minmss on the sending side) and it deals with too many too small 925 * tcp packets in a too short timeframe (1 second). 926 * 927 * For every full second we count the number of received packets 928 * and bytes. If we get a lot of packets per second for this connection 929 * (tcp_minmssoverload) we take a closer look at it and compute the 930 * average packet size for the past second. If that is less than 931 * tcp_minmss we get too many packets with very small payload which 932 * is not good and burdens our system (and every packet generates 933 * a wakeup to the process connected to our socket). We can reasonable 934 * expect this to be small packet DoS attack to exhaust our CPU 935 * cycles. 936 * 937 * Care has to be taken for the minimum packet overload value. This 938 * value defines the minimum number of packets per second before we 939 * start to worry. This must not be too low to avoid killing for 940 * example interactive connections with many small packets like 941 * telnet or SSH. 942 * 943 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables 944 * this check. 945 * 946 * Account for packet if payload packet, skip over ACK, etc. 947 */ 948 if (tcp_minmss && tcp_minmssoverload && 949 tp->t_state == TCPS_ESTABLISHED && tlen > 0) { 950 if (tp->rcv_second > ticks) { 951 tp->rcv_pps++; 952 tp->rcv_byps += tlen + off; 953 if (tp->rcv_pps > tcp_minmssoverload) { 954 if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) { 955 printf("too many small tcp packets from " 956 "%s:%u, av. %lubyte/packet, " 957 "dropping connection\n", 958#ifdef INET6 959 isipv6 ? 960 ip6_sprintf(&inp->inp_inc.inc6_faddr) : 961#endif 962 inet_ntoa(inp->inp_inc.inc_faddr), 963 inp->inp_inc.inc_fport, 964 tp->rcv_byps / tp->rcv_pps); 965 tp = tcp_drop(tp, ECONNRESET); 966 tcpstat.tcps_minmssdrops++; 967 goto drop; 968 } 969 } 970 } else { 971 tp->rcv_second = ticks + hz; 972 tp->rcv_pps = 1; 973 tp->rcv_byps = tlen + off; 974 } 975 } 976 977 /* 978 * Segment received on connection. 979 * Reset idle time and keep-alive timer. 980 */ 981 tp->t_rcvtime = ticks; 982 if (TCPS_HAVEESTABLISHED(tp->t_state)) 983 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 984 985 /* 986 * Process options only when we get SYN/ACK back. The SYN case 987 * for incoming connections is handled in tcp_syncache. 988 * XXX this is traditional behavior, may need to be cleaned up. 989 */ 990 tcp_dooptions(&to, optp, optlen, thflags & TH_SYN); 991 if (thflags & TH_SYN) { 992 if (to.to_flags & TOF_SCALE) { 993 tp->t_flags |= TF_RCVD_SCALE; 994 tp->requested_s_scale = to.to_requested_s_scale; 995 } 996 if (to.to_flags & TOF_TS) { 997 tp->t_flags |= TF_RCVD_TSTMP; 998 tp->ts_recent = to.to_tsval; 999 tp->ts_recent_age = ticks; 1000 } 1001 if (to.to_flags & (TOF_CC|TOF_CCNEW)) 1002 tp->t_flags |= TF_RCVD_CC; 1003 if (to.to_flags & TOF_MSS) 1004 tcp_mss(tp, to.to_mss); 1005 } 1006 1007 /* 1008 * Header prediction: check for the two common cases 1009 * of a uni-directional data xfer. If the packet has 1010 * no control flags, is in-sequence, the window didn't 1011 * change and we're not retransmitting, it's a 1012 * candidate. If the length is zero and the ack moved 1013 * forward, we're the sender side of the xfer. Just 1014 * free the data acked & wake any higher level process 1015 * that was blocked waiting for space. If the length 1016 * is non-zero and the ack didn't move, we're the 1017 * receiver side. If we're getting packets in-order 1018 * (the reassembly queue is empty), add the data to 1019 * the socket buffer and note that we need a delayed ack. 1020 * Make sure that the hidden state-flags are also off. 1021 * Since we check for TCPS_ESTABLISHED above, it can only 1022 * be TH_NEEDSYN. 1023 */ 1024 if (tp->t_state == TCPS_ESTABLISHED && 1025 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1026 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1027 ((to.to_flags & TOF_TS) == 0 || 1028 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 1029 /* 1030 * Using the CC option is compulsory if once started: 1031 * the segment is OK if no T/TCP was negotiated or 1032 * if the segment has a CC option equal to CCrecv 1033 */ 1034 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 1035 ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 1036 th->th_seq == tp->rcv_nxt && 1037 tiwin && tiwin == tp->snd_wnd && 1038 tp->snd_nxt == tp->snd_max) { 1039 1040 /* 1041 * If last ACK falls within this segment's sequence numbers, 1042 * record the timestamp. 1043 * NOTE that the test is modified according to the latest 1044 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1045 */ 1046 if ((to.to_flags & TOF_TS) != 0 && 1047 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1048 tp->ts_recent_age = ticks; 1049 tp->ts_recent = to.to_tsval; 1050 } 1051 1052 if (tlen == 0) { 1053 if (SEQ_GT(th->th_ack, tp->snd_una) && 1054 SEQ_LEQ(th->th_ack, tp->snd_max) && 1055 tp->snd_cwnd >= tp->snd_wnd && 1056 ((!tcp_do_newreno && 1057 tp->t_dupacks < tcprexmtthresh) || 1058 (tcp_do_newreno && !IN_FASTRECOVERY(tp)))) { 1059 KASSERT(headlocked, ("headlocked")); 1060 INP_INFO_WUNLOCK(&tcbinfo); 1061 /* 1062 * this is a pure ack for outstanding data. 1063 */ 1064 ++tcpstat.tcps_predack; 1065 /* 1066 * "bad retransmit" recovery 1067 */ 1068 if (tp->t_rxtshift == 1 && 1069 ticks < tp->t_badrxtwin) { 1070 ++tcpstat.tcps_sndrexmitbad; 1071 tp->snd_cwnd = tp->snd_cwnd_prev; 1072 tp->snd_ssthresh = 1073 tp->snd_ssthresh_prev; 1074 tp->snd_recover = tp->snd_recover_prev; 1075 if (tp->t_flags & TF_WASFRECOVERY) 1076 ENTER_FASTRECOVERY(tp); 1077 tp->snd_nxt = tp->snd_max; 1078 tp->t_badrxtwin = 0; 1079 } 1080 1081 /* 1082 * Recalculate the transmit timer / rtt. 1083 * 1084 * Some boxes send broken timestamp replies 1085 * during the SYN+ACK phase, ignore 1086 * timestamps of 0 or we could calculate a 1087 * huge RTT and blow up the retransmit timer. 1088 */ 1089 if ((to.to_flags & TOF_TS) != 0 && 1090 to.to_tsecr) { 1091 tcp_xmit_timer(tp, 1092 ticks - to.to_tsecr + 1); 1093 } else if (tp->t_rtttime && 1094 SEQ_GT(th->th_ack, tp->t_rtseq)) { 1095 tcp_xmit_timer(tp, 1096 ticks - tp->t_rtttime); 1097 } 1098 tcp_xmit_bandwidth_limit(tp, th->th_ack); 1099 acked = th->th_ack - tp->snd_una; 1100 tcpstat.tcps_rcvackpack++; 1101 tcpstat.tcps_rcvackbyte += acked; 1102 sbdrop(&so->so_snd, acked); 1103 if (SEQ_GT(tp->snd_una, tp->snd_recover) && 1104 SEQ_LEQ(th->th_ack, tp->snd_recover)) 1105 tp->snd_recover = th->th_ack - 1; 1106 tp->snd_una = th->th_ack; 1107 /* 1108 * pull snd_wl2 up to prevent seq wrap relative 1109 * to th_ack. 1110 */ 1111 tp->snd_wl2 = th->th_ack; 1112 tp->t_dupacks = 0; 1113 m_freem(m); 1114 ND6_HINT(tp); /* some progress has been done */ 1115 1116 /* 1117 * If all outstanding data are acked, stop 1118 * retransmit timer, otherwise restart timer 1119 * using current (possibly backed-off) value. 1120 * If process is waiting for space, 1121 * wakeup/selwakeup/signal. If data 1122 * are ready to send, let tcp_output 1123 * decide between more output or persist. 1124 1125#ifdef TCPDEBUG 1126 if (so->so_options & SO_DEBUG) 1127 tcp_trace(TA_INPUT, ostate, tp, 1128 (void *)tcp_saveipgen, 1129 &tcp_savetcp, 0); 1130#endif 1131 */ 1132 if (tp->snd_una == tp->snd_max) 1133 callout_stop(tp->tt_rexmt); 1134 else if (!callout_active(tp->tt_persist)) 1135 callout_reset(tp->tt_rexmt, 1136 tp->t_rxtcur, 1137 tcp_timer_rexmt, tp); 1138 1139 sowwakeup(so); 1140 if (so->so_snd.sb_cc) 1141 (void) tcp_output(tp); 1142 goto check_delack; 1143 } 1144 } else if (th->th_ack == tp->snd_una && 1145 LIST_EMPTY(&tp->t_segq) && 1146 tlen <= sbspace(&so->so_rcv)) { 1147 KASSERT(headlocked, ("headlocked")); 1148 INP_INFO_WUNLOCK(&tcbinfo); 1149 /* 1150 * this is a pure, in-sequence data packet 1151 * with nothing on the reassembly queue and 1152 * we have enough buffer space to take it. 1153 */ 1154 ++tcpstat.tcps_preddat; 1155 tp->rcv_nxt += tlen; 1156 /* 1157 * Pull snd_wl1 up to prevent seq wrap relative to 1158 * th_seq. 1159 */ 1160 tp->snd_wl1 = th->th_seq; 1161 /* 1162 * Pull rcv_up up to prevent seq wrap relative to 1163 * rcv_nxt. 1164 */ 1165 tp->rcv_up = tp->rcv_nxt; 1166 tcpstat.tcps_rcvpack++; 1167 tcpstat.tcps_rcvbyte += tlen; 1168 ND6_HINT(tp); /* some progress has been done */ 1169 /* 1170#ifdef TCPDEBUG 1171 if (so->so_options & SO_DEBUG) 1172 tcp_trace(TA_INPUT, ostate, tp, 1173 (void *)tcp_saveipgen, &tcp_savetcp, 0); 1174#endif 1175 * Add data to socket buffer. 1176 */ 1177 if (so->so_state & SS_CANTRCVMORE) { 1178 m_freem(m); 1179 } else { 1180 m_adj(m, drop_hdrlen); /* delayed header drop */ 1181 sbappendstream(&so->so_rcv, m); 1182 } 1183 sorwakeup(so); 1184 if (DELAY_ACK(tp)) { 1185 tp->t_flags |= TF_DELACK; 1186 } else { 1187 tp->t_flags |= TF_ACKNOW; 1188 tcp_output(tp); 1189 } 1190 goto check_delack; 1191 } 1192 } 1193 1194 /* 1195 * Calculate amount of space in receive window, 1196 * and then do TCP input processing. 1197 * Receive window is amount of space in rcv queue, 1198 * but not less than advertised window. 1199 */ 1200 { int win; 1201 1202 win = sbspace(&so->so_rcv); 1203 if (win < 0) 1204 win = 0; 1205 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1206 } 1207 1208 switch (tp->t_state) { 1209 1210 /* 1211 * If the state is SYN_RECEIVED: 1212 * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1213 */ 1214 case TCPS_SYN_RECEIVED: 1215 if ((thflags & TH_ACK) && 1216 (SEQ_LEQ(th->th_ack, tp->snd_una) || 1217 SEQ_GT(th->th_ack, tp->snd_max))) { 1218 rstreason = BANDLIM_RST_OPENPORT; 1219 goto dropwithreset; 1220 } 1221 break; 1222 1223 /* 1224 * If the state is SYN_SENT: 1225 * if seg contains an ACK, but not for our SYN, drop the input. 1226 * if seg contains a RST, then drop the connection. 1227 * if seg does not contain SYN, then drop it. 1228 * Otherwise this is an acceptable SYN segment 1229 * initialize tp->rcv_nxt and tp->irs 1230 * if seg contains ack then advance tp->snd_una 1231 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1232 * arrange for segment to be acked (eventually) 1233 * continue processing rest of data/controls, beginning with URG 1234 */ 1235 case TCPS_SYN_SENT: 1236 if (tcp_do_rfc1644) 1237 tcp_hc_gettao(&inp->inp_inc, &tao); 1238 1239 if ((thflags & TH_ACK) && 1240 (SEQ_LEQ(th->th_ack, tp->iss) || 1241 SEQ_GT(th->th_ack, tp->snd_max))) { 1242 /* 1243 * If we have a cached CCsent for the remote host, 1244 * hence we haven't just crashed and restarted, 1245 * do not send a RST. This may be a retransmission 1246 * from the other side after our earlier ACK was lost. 1247 * Our new SYN, when it arrives, will serve as the 1248 * needed ACK. 1249 */ 1250 if (tao.tao_ccsent != 0) 1251 goto drop; 1252 else { 1253 rstreason = BANDLIM_UNLIMITED; 1254 goto dropwithreset; 1255 } 1256 } 1257 if (thflags & TH_RST) { 1258 if (thflags & TH_ACK) 1259 tp = tcp_drop(tp, ECONNREFUSED); 1260 goto drop; 1261 } 1262 if ((thflags & TH_SYN) == 0) 1263 goto drop; 1264 tp->snd_wnd = th->th_win; /* initial send window */ 1265 tp->cc_recv = to.to_cc; /* foreign CC */ 1266 1267 tp->irs = th->th_seq; 1268 tcp_rcvseqinit(tp); 1269 if (thflags & TH_ACK) { 1270 /* 1271 * Our SYN was acked. If segment contains CC.ECHO 1272 * option, check it to make sure this segment really 1273 * matches our SYN. If not, just drop it as old 1274 * duplicate, but send an RST if we're still playing 1275 * by the old rules. If no CC.ECHO option, make sure 1276 * we don't get fooled into using T/TCP. 1277 */ 1278 if (to.to_flags & TOF_CCECHO) { 1279 if (tp->cc_send != to.to_ccecho) { 1280 if (tao.tao_ccsent != 0) 1281 goto drop; 1282 else { 1283 rstreason = BANDLIM_UNLIMITED; 1284 goto dropwithreset; 1285 } 1286 } 1287 } else 1288 tp->t_flags &= ~TF_RCVD_CC; 1289 tcpstat.tcps_connects++; 1290 soisconnected(so); 1291#ifdef MAC 1292 mac_set_socket_peer_from_mbuf(m, so); 1293#endif 1294 /* Do window scaling on this connection? */ 1295 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1296 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1297 tp->snd_scale = tp->requested_s_scale; 1298 tp->rcv_scale = tp->request_r_scale; 1299 } 1300 /* Segment is acceptable, update cache if undefined. */ 1301 if (tao.tao_ccsent == 0 && tcp_do_rfc1644) 1302 tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT, to.to_ccecho, 0); 1303 1304 tp->rcv_adv += tp->rcv_wnd; 1305 tp->snd_una++; /* SYN is acked */ 1306 /* 1307 * If there's data, delay ACK; if there's also a FIN 1308 * ACKNOW will be turned on later. 1309 */ 1310 if (DELAY_ACK(tp) && tlen != 0) 1311 callout_reset(tp->tt_delack, tcp_delacktime, 1312 tcp_timer_delack, tp); 1313 else 1314 tp->t_flags |= TF_ACKNOW; 1315 /* 1316 * Received <SYN,ACK> in SYN_SENT[*] state. 1317 * Transitions: 1318 * SYN_SENT --> ESTABLISHED 1319 * SYN_SENT* --> FIN_WAIT_1 1320 */ 1321 tp->t_starttime = ticks; 1322 if (tp->t_flags & TF_NEEDFIN) { 1323 tp->t_state = TCPS_FIN_WAIT_1; 1324 tp->t_flags &= ~TF_NEEDFIN; 1325 thflags &= ~TH_SYN; 1326 } else { 1327 tp->t_state = TCPS_ESTABLISHED; 1328 callout_reset(tp->tt_keep, tcp_keepidle, 1329 tcp_timer_keep, tp); 1330 } 1331 } else { 1332 /* 1333 * Received initial SYN in SYN-SENT[*] state => 1334 * simultaneous open. If segment contains CC option 1335 * and there is a cached CC, apply TAO test. 1336 * If it succeeds, connection is * half-synchronized. 1337 * Otherwise, do 3-way handshake: 1338 * SYN-SENT -> SYN-RECEIVED 1339 * SYN-SENT* -> SYN-RECEIVED* 1340 * If there was no CC option, clear cached CC value. 1341 */ 1342 tp->t_flags |= TF_ACKNOW; 1343 callout_stop(tp->tt_rexmt); 1344 if (to.to_flags & TOF_CC) { 1345 if (tao.tao_cc != 0 && 1346 CC_GT(to.to_cc, tao.tao_cc)) { 1347 /* 1348 * update cache and make transition: 1349 * SYN-SENT -> ESTABLISHED* 1350 * SYN-SENT* -> FIN-WAIT-1* 1351 */ 1352 tao.tao_cc = to.to_cc; 1353 tcp_hc_updatetao(&inp->inp_inc, 1354 TCP_HC_TAO_CC, to.to_cc, 0); 1355 tp->t_starttime = ticks; 1356 if (tp->t_flags & TF_NEEDFIN) { 1357 tp->t_state = TCPS_FIN_WAIT_1; 1358 tp->t_flags &= ~TF_NEEDFIN; 1359 } else { 1360 tp->t_state = TCPS_ESTABLISHED; 1361 callout_reset(tp->tt_keep, 1362 tcp_keepidle, 1363 tcp_timer_keep, 1364 tp); 1365 } 1366 tp->t_flags |= TF_NEEDSYN; 1367 } else 1368 tp->t_state = TCPS_SYN_RECEIVED; 1369 } else { 1370 if (tcp_do_rfc1644) { 1371 /* CC.NEW or no option => invalidate cache */ 1372 tao.tao_cc = 0; 1373 tcp_hc_updatetao(&inp->inp_inc, 1374 TCP_HC_TAO_CC, to.to_cc, 0); 1375 } 1376 tp->t_state = TCPS_SYN_RECEIVED; 1377 } 1378 } 1379 1380trimthenstep6: 1381 /* 1382 * Advance th->th_seq to correspond to first data byte. 1383 * If data, trim to stay within window, 1384 * dropping FIN if necessary. 1385 */ 1386 th->th_seq++; 1387 if (tlen > tp->rcv_wnd) { 1388 todrop = tlen - tp->rcv_wnd; 1389 m_adj(m, -todrop); 1390 tlen = tp->rcv_wnd; 1391 thflags &= ~TH_FIN; 1392 tcpstat.tcps_rcvpackafterwin++; 1393 tcpstat.tcps_rcvbyteafterwin += todrop; 1394 } 1395 tp->snd_wl1 = th->th_seq - 1; 1396 tp->rcv_up = th->th_seq; 1397 /* 1398 * Client side of transaction: already sent SYN and data. 1399 * If the remote host used T/TCP to validate the SYN, 1400 * our data will be ACK'd; if so, enter normal data segment 1401 * processing in the middle of step 5, ack processing. 1402 * Otherwise, goto step 6. 1403 */ 1404 if (thflags & TH_ACK) 1405 goto process_ACK; 1406 1407 goto step6; 1408 1409 /* 1410 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1411 * if segment contains a SYN and CC [not CC.NEW] option: 1412 * if state == TIME_WAIT and connection duration > MSL, 1413 * drop packet and send RST; 1414 * 1415 * if SEG.CC > CCrecv then is new SYN, and can implicitly 1416 * ack the FIN (and data) in retransmission queue. 1417 * Complete close and delete TCPCB. Then reprocess 1418 * segment, hoping to find new TCPCB in LISTEN state; 1419 * 1420 * else must be old SYN; drop it. 1421 * else do normal processing. 1422 */ 1423 case TCPS_LAST_ACK: 1424 case TCPS_CLOSING: 1425 case TCPS_TIME_WAIT: 1426 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1427 if ((thflags & TH_SYN) && 1428 (to.to_flags & TOF_CC) && tp->cc_recv != 0) { 1429 if (tp->t_state == TCPS_TIME_WAIT && 1430 (ticks - tp->t_starttime) > tcp_msl) { 1431 rstreason = BANDLIM_UNLIMITED; 1432 goto dropwithreset; 1433 } 1434 if (CC_GT(to.to_cc, tp->cc_recv)) { 1435 tp = tcp_close(tp); 1436 goto findpcb; 1437 } 1438 else 1439 goto drop; 1440 } 1441 break; /* continue normal processing */ 1442 } 1443 1444 /* 1445 * States other than LISTEN or SYN_SENT. 1446 * First check the RST flag and sequence number since reset segments 1447 * are exempt from the timestamp and connection count tests. This 1448 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 1449 * below which allowed reset segments in half the sequence space 1450 * to fall though and be processed (which gives forged reset 1451 * segments with a random sequence number a 50 percent chance of 1452 * killing a connection). 1453 * Then check timestamp, if present. 1454 * Then check the connection count, if present. 1455 * Then check that at least some bytes of segment are within 1456 * receive window. If segment begins before rcv_nxt, 1457 * drop leading data (and SYN); if nothing left, just ack. 1458 * 1459 * 1460 * If the RST bit is set, check the sequence number to see 1461 * if this is a valid reset segment. 1462 * RFC 793 page 37: 1463 * In all states except SYN-SENT, all reset (RST) segments 1464 * are validated by checking their SEQ-fields. A reset is 1465 * valid if its sequence number is in the window. 1466 * Note: this does not take into account delayed ACKs, so 1467 * we should test against last_ack_sent instead of rcv_nxt. 1468 * The sequence number in the reset segment is normally an 1469 * echo of our outgoing acknowlegement numbers, but some hosts 1470 * send a reset with the sequence number at the rightmost edge 1471 * of our receive window, and we have to handle this case. 1472 * If we have multiple segments in flight, the intial reset 1473 * segment sequence numbers will be to the left of last_ack_sent, 1474 * but they will eventually catch up. 1475 * In any case, it never made sense to trim reset segments to 1476 * fit the receive window since RFC 1122 says: 1477 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 1478 * 1479 * A TCP SHOULD allow a received RST segment to include data. 1480 * 1481 * DISCUSSION 1482 * It has been suggested that a RST segment could contain 1483 * ASCII text that encoded and explained the cause of the 1484 * RST. No standard has yet been established for such 1485 * data. 1486 * 1487 * If the reset segment passes the sequence number test examine 1488 * the state: 1489 * SYN_RECEIVED STATE: 1490 * If passive open, return to LISTEN state. 1491 * If active open, inform user that connection was refused. 1492 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 1493 * Inform user that connection was reset, and close tcb. 1494 * CLOSING, LAST_ACK STATES: 1495 * Close the tcb. 1496 * TIME_WAIT STATE: 1497 * Drop the segment - see Stevens, vol. 2, p. 964 and 1498 * RFC 1337. 1499 */ 1500 if (thflags & TH_RST) { 1501 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1502 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 1503 switch (tp->t_state) { 1504 1505 case TCPS_SYN_RECEIVED: 1506 so->so_error = ECONNREFUSED; 1507 goto close; 1508 1509 case TCPS_ESTABLISHED: 1510 case TCPS_FIN_WAIT_1: 1511 case TCPS_FIN_WAIT_2: 1512 case TCPS_CLOSE_WAIT: 1513 so->so_error = ECONNRESET; 1514 close: 1515 tp->t_state = TCPS_CLOSED; 1516 tcpstat.tcps_drops++; 1517 tp = tcp_close(tp); 1518 break; 1519 1520 case TCPS_CLOSING: 1521 case TCPS_LAST_ACK: 1522 tp = tcp_close(tp); 1523 break; 1524 1525 case TCPS_TIME_WAIT: 1526 KASSERT(tp->t_state != TCPS_TIME_WAIT, 1527 ("timewait")); 1528 break; 1529 } 1530 } 1531 goto drop; 1532 } 1533 1534 /* 1535 * RFC 1323 PAWS: If we have a timestamp reply on this segment 1536 * and it's less than ts_recent, drop it. 1537 */ 1538 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 1539 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1540 1541 /* Check to see if ts_recent is over 24 days old. */ 1542 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1543 /* 1544 * Invalidate ts_recent. If this segment updates 1545 * ts_recent, the age will be reset later and ts_recent 1546 * will get a valid value. If it does not, setting 1547 * ts_recent to zero will at least satisfy the 1548 * requirement that zero be placed in the timestamp 1549 * echo reply when ts_recent isn't valid. The 1550 * age isn't reset until we get a valid ts_recent 1551 * because we don't want out-of-order segments to be 1552 * dropped when ts_recent is old. 1553 */ 1554 tp->ts_recent = 0; 1555 } else { 1556 tcpstat.tcps_rcvduppack++; 1557 tcpstat.tcps_rcvdupbyte += tlen; 1558 tcpstat.tcps_pawsdrop++; 1559 if (tlen) 1560 goto dropafterack; 1561 goto drop; 1562 } 1563 } 1564 1565 /* 1566 * T/TCP mechanism 1567 * If T/TCP was negotiated and the segment doesn't have CC, 1568 * or if its CC is wrong then drop the segment. 1569 * RST segments do not have to comply with this. 1570 */ 1571 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1572 ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1573 goto dropafterack; 1574 1575 /* 1576 * In the SYN-RECEIVED state, validate that the packet belongs to 1577 * this connection before trimming the data to fit the receive 1578 * window. Check the sequence number versus IRS since we know 1579 * the sequence numbers haven't wrapped. This is a partial fix 1580 * for the "LAND" DoS attack. 1581 */ 1582 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1583 rstreason = BANDLIM_RST_OPENPORT; 1584 goto dropwithreset; 1585 } 1586 1587 todrop = tp->rcv_nxt - th->th_seq; 1588 if (todrop > 0) { 1589 if (thflags & TH_SYN) { 1590 thflags &= ~TH_SYN; 1591 th->th_seq++; 1592 if (th->th_urp > 1) 1593 th->th_urp--; 1594 else 1595 thflags &= ~TH_URG; 1596 todrop--; 1597 } 1598 /* 1599 * Following if statement from Stevens, vol. 2, p. 960. 1600 */ 1601 if (todrop > tlen 1602 || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1603 /* 1604 * Any valid FIN must be to the left of the window. 1605 * At this point the FIN must be a duplicate or out 1606 * of sequence; drop it. 1607 */ 1608 thflags &= ~TH_FIN; 1609 1610 /* 1611 * Send an ACK to resynchronize and drop any data. 1612 * But keep on processing for RST or ACK. 1613 */ 1614 tp->t_flags |= TF_ACKNOW; 1615 todrop = tlen; 1616 tcpstat.tcps_rcvduppack++; 1617 tcpstat.tcps_rcvdupbyte += todrop; 1618 } else { 1619 tcpstat.tcps_rcvpartduppack++; 1620 tcpstat.tcps_rcvpartdupbyte += todrop; 1621 } 1622 drop_hdrlen += todrop; /* drop from the top afterwards */ 1623 th->th_seq += todrop; 1624 tlen -= todrop; 1625 if (th->th_urp > todrop) 1626 th->th_urp -= todrop; 1627 else { 1628 thflags &= ~TH_URG; 1629 th->th_urp = 0; 1630 } 1631 } 1632 1633 /* 1634 * If new data are received on a connection after the 1635 * user processes are gone, then RST the other end. 1636 */ 1637 if ((so->so_state & SS_NOFDREF) && 1638 tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1639 tp = tcp_close(tp); 1640 tcpstat.tcps_rcvafterclose++; 1641 rstreason = BANDLIM_UNLIMITED; 1642 goto dropwithreset; 1643 } 1644 1645 /* 1646 * If segment ends after window, drop trailing data 1647 * (and PUSH and FIN); if nothing left, just ACK. 1648 */ 1649 todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1650 if (todrop > 0) { 1651 tcpstat.tcps_rcvpackafterwin++; 1652 if (todrop >= tlen) { 1653 tcpstat.tcps_rcvbyteafterwin += tlen; 1654 /* 1655 * If a new connection request is received 1656 * while in TIME_WAIT, drop the old connection 1657 * and start over if the sequence numbers 1658 * are above the previous ones. 1659 */ 1660 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1661 if (thflags & TH_SYN && 1662 tp->t_state == TCPS_TIME_WAIT && 1663 SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1664 tp = tcp_close(tp); 1665 goto findpcb; 1666 } 1667 /* 1668 * If window is closed can only take segments at 1669 * window edge, and have to drop data and PUSH from 1670 * incoming segments. Continue processing, but 1671 * remember to ack. Otherwise, drop segment 1672 * and ack. 1673 */ 1674 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1675 tp->t_flags |= TF_ACKNOW; 1676 tcpstat.tcps_rcvwinprobe++; 1677 } else 1678 goto dropafterack; 1679 } else 1680 tcpstat.tcps_rcvbyteafterwin += todrop; 1681 m_adj(m, -todrop); 1682 tlen -= todrop; 1683 thflags &= ~(TH_PUSH|TH_FIN); 1684 } 1685 1686 /* 1687 * If last ACK falls within this segment's sequence numbers, 1688 * record its timestamp. 1689 * NOTE that the test is modified according to the latest 1690 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1691 */ 1692 if ((to.to_flags & TOF_TS) != 0 && 1693 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1694 tp->ts_recent_age = ticks; 1695 tp->ts_recent = to.to_tsval; 1696 } 1697 1698 /* 1699 * If a SYN is in the window, then this is an 1700 * error and we send an RST and drop the connection. 1701 */ 1702 if (thflags & TH_SYN) { 1703 tp = tcp_drop(tp, ECONNRESET); 1704 rstreason = BANDLIM_UNLIMITED; 1705 goto drop; 1706 } 1707 1708 /* 1709 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1710 * flag is on (half-synchronized state), then queue data for 1711 * later processing; else drop segment and return. 1712 */ 1713 if ((thflags & TH_ACK) == 0) { 1714 if (tp->t_state == TCPS_SYN_RECEIVED || 1715 (tp->t_flags & TF_NEEDSYN)) 1716 goto step6; 1717 else 1718 goto drop; 1719 } 1720 1721 /* 1722 * Ack processing. 1723 */ 1724 switch (tp->t_state) { 1725 1726 /* 1727 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1728 * ESTABLISHED state and continue processing. 1729 * The ACK was checked above. 1730 */ 1731 case TCPS_SYN_RECEIVED: 1732 1733 tcpstat.tcps_connects++; 1734 soisconnected(so); 1735 /* Do window scaling? */ 1736 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1737 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1738 tp->snd_scale = tp->requested_s_scale; 1739 tp->rcv_scale = tp->request_r_scale; 1740 } 1741 /* 1742 * Upon successful completion of 3-way handshake, 1743 * update cache.CC, pass any queued data to the user, 1744 * and advance state appropriately. 1745 */ 1746 if (tcp_do_rfc1644) { 1747 tao.tao_cc = tp->cc_recv; 1748 tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CC, 1749 tp->cc_recv, 0); 1750 } 1751 /* 1752 * Make transitions: 1753 * SYN-RECEIVED -> ESTABLISHED 1754 * SYN-RECEIVED* -> FIN-WAIT-1 1755 */ 1756 tp->t_starttime = ticks; 1757 if (tp->t_flags & TF_NEEDFIN) { 1758 tp->t_state = TCPS_FIN_WAIT_1; 1759 tp->t_flags &= ~TF_NEEDFIN; 1760 } else { 1761 tp->t_state = TCPS_ESTABLISHED; 1762 callout_reset(tp->tt_keep, tcp_keepidle, 1763 tcp_timer_keep, tp); 1764 } 1765 /* 1766 * If segment contains data or ACK, will call tcp_reass() 1767 * later; if not, do so now to pass queued data to user. 1768 */ 1769 if (tlen == 0 && (thflags & TH_FIN) == 0) 1770 (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1771 (struct mbuf *)0); 1772 tp->snd_wl1 = th->th_seq - 1; 1773 /* FALLTHROUGH */ 1774 1775 /* 1776 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1777 * ACKs. If the ack is in the range 1778 * tp->snd_una < th->th_ack <= tp->snd_max 1779 * then advance tp->snd_una to th->th_ack and drop 1780 * data from the retransmission queue. If this ACK reflects 1781 * more up to date window information we update our window information. 1782 */ 1783 case TCPS_ESTABLISHED: 1784 case TCPS_FIN_WAIT_1: 1785 case TCPS_FIN_WAIT_2: 1786 case TCPS_CLOSE_WAIT: 1787 case TCPS_CLOSING: 1788 case TCPS_LAST_ACK: 1789 case TCPS_TIME_WAIT: 1790 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1791 if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1792 if (tlen == 0 && tiwin == tp->snd_wnd) { 1793 tcpstat.tcps_rcvdupack++; 1794 /* 1795 * If we have outstanding data (other than 1796 * a window probe), this is a completely 1797 * duplicate ack (ie, window info didn't 1798 * change), the ack is the biggest we've 1799 * seen and we've seen exactly our rexmt 1800 * threshhold of them, assume a packet 1801 * has been dropped and retransmit it. 1802 * Kludge snd_nxt & the congestion 1803 * window so we send only this one 1804 * packet. 1805 * 1806 * We know we're losing at the current 1807 * window size so do congestion avoidance 1808 * (set ssthresh to half the current window 1809 * and pull our congestion window back to 1810 * the new ssthresh). 1811 * 1812 * Dup acks mean that packets have left the 1813 * network (they're now cached at the receiver) 1814 * so bump cwnd by the amount in the receiver 1815 * to keep a constant cwnd packets in the 1816 * network. 1817 */ 1818 if (!callout_active(tp->tt_rexmt) || 1819 th->th_ack != tp->snd_una) 1820 tp->t_dupacks = 0; 1821 else if (++tp->t_dupacks > tcprexmtthresh || 1822 (tcp_do_newreno && 1823 IN_FASTRECOVERY(tp))) { 1824 tp->snd_cwnd += tp->t_maxseg; 1825 (void) tcp_output(tp); 1826 goto drop; 1827 } else if (tp->t_dupacks == tcprexmtthresh) { 1828 tcp_seq onxt = tp->snd_nxt; 1829 u_int win; 1830 if (tcp_do_newreno && 1831 SEQ_LEQ(th->th_ack, 1832 tp->snd_recover)) { 1833 tp->t_dupacks = 0; 1834 break; 1835 } 1836 win = min(tp->snd_wnd, tp->snd_cwnd) / 1837 2 / tp->t_maxseg; 1838 if (win < 2) 1839 win = 2; 1840 tp->snd_ssthresh = win * tp->t_maxseg; 1841 ENTER_FASTRECOVERY(tp); 1842 tp->snd_recover = tp->snd_max; 1843 callout_stop(tp->tt_rexmt); 1844 tp->t_rtttime = 0; 1845 tp->snd_nxt = th->th_ack; 1846 tp->snd_cwnd = tp->t_maxseg; 1847 (void) tcp_output(tp); 1848 KASSERT(tp->snd_limited <= 2, 1849 ("tp->snd_limited too big")); 1850 tp->snd_cwnd = tp->snd_ssthresh + 1851 tp->t_maxseg * 1852 (tp->t_dupacks - tp->snd_limited); 1853 if (SEQ_GT(onxt, tp->snd_nxt)) 1854 tp->snd_nxt = onxt; 1855 goto drop; 1856 } else if (tcp_do_rfc3042) { 1857 u_long oldcwnd = tp->snd_cwnd; 1858 tcp_seq oldsndmax = tp->snd_max; 1859 u_int sent; 1860 KASSERT(tp->t_dupacks == 1 || 1861 tp->t_dupacks == 2, 1862 ("dupacks not 1 or 2")); 1863 if (tp->t_dupacks == 1) 1864 tp->snd_limited = 0; 1865 tp->snd_cwnd = 1866 (tp->snd_nxt - tp->snd_una) + 1867 (tp->t_dupacks - tp->snd_limited) * 1868 tp->t_maxseg; 1869 (void) tcp_output(tp); 1870 sent = tp->snd_max - oldsndmax; 1871 if (sent > tp->t_maxseg) { 1872 KASSERT(tp->snd_limited == 0 && 1873 tp->t_dupacks == 2, 1874 ("sent too much")); 1875 tp->snd_limited = 2; 1876 } else if (sent > 0) 1877 ++tp->snd_limited; 1878 tp->snd_cwnd = oldcwnd; 1879 goto drop; 1880 } 1881 } else 1882 tp->t_dupacks = 0; 1883 break; 1884 } 1885 1886 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); 1887 1888 /* 1889 * If the congestion window was inflated to account 1890 * for the other side's cached packets, retract it. 1891 */ 1892 if (tcp_do_newreno) { 1893 if (IN_FASTRECOVERY(tp)) { 1894 if (SEQ_LT(th->th_ack, tp->snd_recover)) { 1895 tcp_newreno_partial_ack(tp, th); 1896 } else { 1897 /* 1898 * Window inflation should have left us 1899 * with approximately snd_ssthresh 1900 * outstanding data. 1901 * But in case we would be inclined to 1902 * send a burst, better to do it via 1903 * the slow start mechanism. 1904 */ 1905 if (SEQ_GT(th->th_ack + 1906 tp->snd_ssthresh, 1907 tp->snd_max)) 1908 tp->snd_cwnd = tp->snd_max - 1909 th->th_ack + 1910 tp->t_maxseg; 1911 else 1912 tp->snd_cwnd = tp->snd_ssthresh; 1913 } 1914 } 1915 } else { 1916 if (tp->t_dupacks >= tcprexmtthresh && 1917 tp->snd_cwnd > tp->snd_ssthresh) 1918 tp->snd_cwnd = tp->snd_ssthresh; 1919 } 1920 tp->t_dupacks = 0; 1921 if (SEQ_GT(th->th_ack, tp->snd_max)) { 1922 tcpstat.tcps_rcvacktoomuch++; 1923 goto dropafterack; 1924 } 1925 /* 1926 * If we reach this point, ACK is not a duplicate, 1927 * i.e., it ACKs something we sent. 1928 */ 1929 if (tp->t_flags & TF_NEEDSYN) { 1930 /* 1931 * T/TCP: Connection was half-synchronized, and our 1932 * SYN has been ACK'd (so connection is now fully 1933 * synchronized). Go to non-starred state, 1934 * increment snd_una for ACK of SYN, and check if 1935 * we can do window scaling. 1936 */ 1937 tp->t_flags &= ~TF_NEEDSYN; 1938 tp->snd_una++; 1939 /* Do window scaling? */ 1940 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1941 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1942 tp->snd_scale = tp->requested_s_scale; 1943 tp->rcv_scale = tp->request_r_scale; 1944 } 1945 } 1946 1947process_ACK: 1948 acked = th->th_ack - tp->snd_una; 1949 tcpstat.tcps_rcvackpack++; 1950 tcpstat.tcps_rcvackbyte += acked; 1951 1952 /* 1953 * If we just performed our first retransmit, and the ACK 1954 * arrives within our recovery window, then it was a mistake 1955 * to do the retransmit in the first place. Recover our 1956 * original cwnd and ssthresh, and proceed to transmit where 1957 * we left off. 1958 */ 1959 if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1960 ++tcpstat.tcps_sndrexmitbad; 1961 tp->snd_cwnd = tp->snd_cwnd_prev; 1962 tp->snd_ssthresh = tp->snd_ssthresh_prev; 1963 tp->snd_recover = tp->snd_recover_prev; 1964 if (tp->t_flags & TF_WASFRECOVERY) 1965 ENTER_FASTRECOVERY(tp); 1966 tp->snd_nxt = tp->snd_max; 1967 tp->t_badrxtwin = 0; /* XXX probably not required */ 1968 } 1969 1970 /* 1971 * If we have a timestamp reply, update smoothed 1972 * round trip time. If no timestamp is present but 1973 * transmit timer is running and timed sequence 1974 * number was acked, update smoothed round trip time. 1975 * Since we now have an rtt measurement, cancel the 1976 * timer backoff (cf., Phil Karn's retransmit alg.). 1977 * Recompute the initial retransmit timer. 1978 * 1979 * Some boxes send broken timestamp replies 1980 * during the SYN+ACK phase, ignore 1981 * timestamps of 0 or we could calculate a 1982 * huge RTT and blow up the retransmit timer. 1983 */ 1984 if ((to.to_flags & TOF_TS) != 0 && 1985 to.to_tsecr) { 1986 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1987 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 1988 tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1989 } 1990 tcp_xmit_bandwidth_limit(tp, th->th_ack); 1991 1992 /* 1993 * If all outstanding data is acked, stop retransmit 1994 * timer and remember to restart (more output or persist). 1995 * If there is more data to be acked, restart retransmit 1996 * timer, using current (possibly backed-off) value. 1997 */ 1998 if (th->th_ack == tp->snd_max) { 1999 callout_stop(tp->tt_rexmt); 2000 needoutput = 1; 2001 } else if (!callout_active(tp->tt_persist)) 2002 callout_reset(tp->tt_rexmt, tp->t_rxtcur, 2003 tcp_timer_rexmt, tp); 2004 2005 /* 2006 * If no data (only SYN) was ACK'd, 2007 * skip rest of ACK processing. 2008 */ 2009 if (acked == 0) 2010 goto step6; 2011 2012 /* 2013 * When new data is acked, open the congestion window. 2014 * If the window gives us less than ssthresh packets 2015 * in flight, open exponentially (maxseg per packet). 2016 * Otherwise open linearly: maxseg per window 2017 * (maxseg^2 / cwnd per packet). 2018 */ 2019 if (!tcp_do_newreno || !IN_FASTRECOVERY(tp)) { 2020 register u_int cw = tp->snd_cwnd; 2021 register u_int incr = tp->t_maxseg; 2022 if (cw > tp->snd_ssthresh) 2023 incr = incr * incr / cw; 2024 tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2025 } 2026 if (acked > so->so_snd.sb_cc) { 2027 tp->snd_wnd -= so->so_snd.sb_cc; 2028 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 2029 ourfinisacked = 1; 2030 } else { 2031 sbdrop(&so->so_snd, acked); 2032 tp->snd_wnd -= acked; 2033 ourfinisacked = 0; 2034 } 2035 sowwakeup(so); 2036 /* detect una wraparound */ 2037 if (tcp_do_newreno && !IN_FASTRECOVERY(tp) && 2038 SEQ_GT(tp->snd_una, tp->snd_recover) && 2039 SEQ_LEQ(th->th_ack, tp->snd_recover)) 2040 tp->snd_recover = th->th_ack - 1; 2041 if (tcp_do_newreno && IN_FASTRECOVERY(tp) && 2042 SEQ_GEQ(th->th_ack, tp->snd_recover)) 2043 EXIT_FASTRECOVERY(tp); 2044 tp->snd_una = th->th_ack; 2045 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2046 tp->snd_nxt = tp->snd_una; 2047 2048 switch (tp->t_state) { 2049 2050 /* 2051 * In FIN_WAIT_1 STATE in addition to the processing 2052 * for the ESTABLISHED state if our FIN is now acknowledged 2053 * then enter FIN_WAIT_2. 2054 */ 2055 case TCPS_FIN_WAIT_1: 2056 if (ourfinisacked) { 2057 /* 2058 * If we can't receive any more 2059 * data, then closing user can proceed. 2060 * Starting the timer is contrary to the 2061 * specification, but if we don't get a FIN 2062 * we'll hang forever. 2063 */ 2064 /* XXXjl 2065 * we should release the tp also, and use a 2066 * compressed state. 2067 */ 2068 if (so->so_state & SS_CANTRCVMORE) { 2069 soisdisconnected(so); 2070 callout_reset(tp->tt_2msl, tcp_maxidle, 2071 tcp_timer_2msl, tp); 2072 } 2073 tp->t_state = TCPS_FIN_WAIT_2; 2074 } 2075 break; 2076 2077 /* 2078 * In CLOSING STATE in addition to the processing for 2079 * the ESTABLISHED state if the ACK acknowledges our FIN 2080 * then enter the TIME-WAIT state, otherwise ignore 2081 * the segment. 2082 */ 2083 case TCPS_CLOSING: 2084 if (ourfinisacked) { 2085 KASSERT(headlocked, ("headlocked")); 2086 tcp_twstart(tp); 2087 INP_INFO_WUNLOCK(&tcbinfo); 2088 m_freem(m); 2089 return; 2090 } 2091 break; 2092 2093 /* 2094 * In LAST_ACK, we may still be waiting for data to drain 2095 * and/or to be acked, as well as for the ack of our FIN. 2096 * If our FIN is now acknowledged, delete the TCB, 2097 * enter the closed state and return. 2098 */ 2099 case TCPS_LAST_ACK: 2100 if (ourfinisacked) { 2101 tp = tcp_close(tp); 2102 goto drop; 2103 } 2104 break; 2105 2106 /* 2107 * In TIME_WAIT state the only thing that should arrive 2108 * is a retransmission of the remote FIN. Acknowledge 2109 * it and restart the finack timer. 2110 */ 2111 case TCPS_TIME_WAIT: 2112 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 2113 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2114 tcp_timer_2msl, tp); 2115 goto dropafterack; 2116 } 2117 } 2118 2119step6: 2120 /* 2121 * Update window information. 2122 * Don't look at window if no ACK: TAC's send garbage on first SYN. 2123 */ 2124 if ((thflags & TH_ACK) && 2125 (SEQ_LT(tp->snd_wl1, th->th_seq) || 2126 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2127 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2128 /* keep track of pure window updates */ 2129 if (tlen == 0 && 2130 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2131 tcpstat.tcps_rcvwinupd++; 2132 tp->snd_wnd = tiwin; 2133 tp->snd_wl1 = th->th_seq; 2134 tp->snd_wl2 = th->th_ack; 2135 if (tp->snd_wnd > tp->max_sndwnd) 2136 tp->max_sndwnd = tp->snd_wnd; 2137 needoutput = 1; 2138 } 2139 2140 /* 2141 * Process segments with URG. 2142 */ 2143 if ((thflags & TH_URG) && th->th_urp && 2144 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2145 /* 2146 * This is a kludge, but if we receive and accept 2147 * random urgent pointers, we'll crash in 2148 * soreceive. It's hard to imagine someone 2149 * actually wanting to send this much urgent data. 2150 */ 2151 if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2152 th->th_urp = 0; /* XXX */ 2153 thflags &= ~TH_URG; /* XXX */ 2154 goto dodata; /* XXX */ 2155 } 2156 /* 2157 * If this segment advances the known urgent pointer, 2158 * then mark the data stream. This should not happen 2159 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2160 * a FIN has been received from the remote side. 2161 * In these states we ignore the URG. 2162 * 2163 * According to RFC961 (Assigned Protocols), 2164 * the urgent pointer points to the last octet 2165 * of urgent data. We continue, however, 2166 * to consider it to indicate the first octet 2167 * of data past the urgent section as the original 2168 * spec states (in one of two places). 2169 */ 2170 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2171 tp->rcv_up = th->th_seq + th->th_urp; 2172 so->so_oobmark = so->so_rcv.sb_cc + 2173 (tp->rcv_up - tp->rcv_nxt) - 1; 2174 if (so->so_oobmark == 0) 2175 so->so_state |= SS_RCVATMARK; 2176 sohasoutofband(so); 2177 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2178 } 2179 /* 2180 * Remove out of band data so doesn't get presented to user. 2181 * This can happen independent of advancing the URG pointer, 2182 * but if two URG's are pending at once, some out-of-band 2183 * data may creep in... ick. 2184 */ 2185 if (th->th_urp <= (u_long)tlen && 2186 !(so->so_options & SO_OOBINLINE)) { 2187 /* hdr drop is delayed */ 2188 tcp_pulloutofband(so, th, m, drop_hdrlen); 2189 } 2190 } else { 2191 /* 2192 * If no out of band data is expected, 2193 * pull receive urgent pointer along 2194 * with the receive window. 2195 */ 2196 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2197 tp->rcv_up = tp->rcv_nxt; 2198 } 2199dodata: /* XXX */ 2200 KASSERT(headlocked, ("headlocked")); 2201 /* 2202 * Process the segment text, merging it into the TCP sequencing queue, 2203 * and arranging for acknowledgment of receipt if necessary. 2204 * This process logically involves adjusting tp->rcv_wnd as data 2205 * is presented to the user (this happens in tcp_usrreq.c, 2206 * case PRU_RCVD). If a FIN has already been received on this 2207 * connection then we just ignore the text. 2208 */ 2209 if ((tlen || (thflags & TH_FIN)) && 2210 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2211 m_adj(m, drop_hdrlen); /* delayed header drop */ 2212 /* 2213 * Insert segment which includes th into TCP reassembly queue 2214 * with control block tp. Set thflags to whether reassembly now 2215 * includes a segment with FIN. This handles the common case 2216 * inline (segment is the next to be received on an established 2217 * connection, and the queue is empty), avoiding linkage into 2218 * and removal from the queue and repetition of various 2219 * conversions. 2220 * Set DELACK for segments received in order, but ack 2221 * immediately when segments are out of order (so 2222 * fast retransmit can work). 2223 */ 2224 if (th->th_seq == tp->rcv_nxt && 2225 LIST_EMPTY(&tp->t_segq) && 2226 TCPS_HAVEESTABLISHED(tp->t_state)) { 2227 if (DELAY_ACK(tp)) 2228 tp->t_flags |= TF_DELACK; 2229 else 2230 tp->t_flags |= TF_ACKNOW; 2231 tp->rcv_nxt += tlen; 2232 thflags = th->th_flags & TH_FIN; 2233 tcpstat.tcps_rcvpack++; 2234 tcpstat.tcps_rcvbyte += tlen; 2235 ND6_HINT(tp); 2236 if (so->so_state & SS_CANTRCVMORE) 2237 m_freem(m); 2238 else 2239 sbappendstream(&so->so_rcv, m); 2240 sorwakeup(so); 2241 } else { 2242 thflags = tcp_reass(tp, th, &tlen, m); 2243 tp->t_flags |= TF_ACKNOW; 2244 } 2245 2246 /* 2247 * Note the amount of data that peer has sent into 2248 * our window, in order to estimate the sender's 2249 * buffer size. 2250 */ 2251 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2252 } else { 2253 m_freem(m); 2254 thflags &= ~TH_FIN; 2255 } 2256 2257 /* 2258 * If FIN is received ACK the FIN and let the user know 2259 * that the connection is closing. 2260 */ 2261 if (thflags & TH_FIN) { 2262 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2263 socantrcvmore(so); 2264 /* 2265 * If connection is half-synchronized 2266 * (ie NEEDSYN flag on) then delay ACK, 2267 * so it may be piggybacked when SYN is sent. 2268 * Otherwise, since we received a FIN then no 2269 * more input can be expected, send ACK now. 2270 */ 2271 if (tp->t_flags & TF_NEEDSYN) 2272 tp->t_flags |= TF_DELACK; 2273 else 2274 tp->t_flags |= TF_ACKNOW; 2275 tp->rcv_nxt++; 2276 } 2277 switch (tp->t_state) { 2278 2279 /* 2280 * In SYN_RECEIVED and ESTABLISHED STATES 2281 * enter the CLOSE_WAIT state. 2282 */ 2283 case TCPS_SYN_RECEIVED: 2284 tp->t_starttime = ticks; 2285 /*FALLTHROUGH*/ 2286 case TCPS_ESTABLISHED: 2287 tp->t_state = TCPS_CLOSE_WAIT; 2288 break; 2289 2290 /* 2291 * If still in FIN_WAIT_1 STATE FIN has not been acked so 2292 * enter the CLOSING state. 2293 */ 2294 case TCPS_FIN_WAIT_1: 2295 tp->t_state = TCPS_CLOSING; 2296 break; 2297 2298 /* 2299 * In FIN_WAIT_2 state enter the TIME_WAIT state, 2300 * starting the time-wait timer, turning off the other 2301 * standard timers. 2302 */ 2303 case TCPS_FIN_WAIT_2: 2304 KASSERT(headlocked == 1, ("headlocked should be 1")); 2305 tcp_twstart(tp); 2306 INP_INFO_WUNLOCK(&tcbinfo); 2307 return; 2308 2309 /* 2310 * In TIME_WAIT state restart the 2 MSL time_wait timer. 2311 */ 2312 case TCPS_TIME_WAIT: 2313 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 2314 callout_reset(tp->tt_2msl, 2 * tcp_msl, 2315 tcp_timer_2msl, tp); 2316 break; 2317 } 2318 } 2319 INP_INFO_WUNLOCK(&tcbinfo); 2320#ifdef TCPDEBUG 2321 if (so->so_options & SO_DEBUG) 2322 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2323 &tcp_savetcp, 0); 2324#endif 2325 2326 /* 2327 * Return any desired output. 2328 */ 2329 if (needoutput || (tp->t_flags & TF_ACKNOW)) 2330 (void) tcp_output(tp); 2331 2332check_delack: 2333 if (tp->t_flags & TF_DELACK) { 2334 tp->t_flags &= ~TF_DELACK; 2335 callout_reset(tp->tt_delack, tcp_delacktime, 2336 tcp_timer_delack, tp); 2337 } 2338 INP_UNLOCK(inp); 2339 return; 2340 2341dropafterack: 2342 /* 2343 * Generate an ACK dropping incoming segment if it occupies 2344 * sequence space, where the ACK reflects our state. 2345 * 2346 * We can now skip the test for the RST flag since all 2347 * paths to this code happen after packets containing 2348 * RST have been dropped. 2349 * 2350 * In the SYN-RECEIVED state, don't send an ACK unless the 2351 * segment we received passes the SYN-RECEIVED ACK test. 2352 * If it fails send a RST. This breaks the loop in the 2353 * "LAND" DoS attack, and also prevents an ACK storm 2354 * between two listening ports that have been sent forged 2355 * SYN segments, each with the source address of the other. 2356 */ 2357 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2358 (SEQ_GT(tp->snd_una, th->th_ack) || 2359 SEQ_GT(th->th_ack, tp->snd_max)) ) { 2360 rstreason = BANDLIM_RST_OPENPORT; 2361 goto dropwithreset; 2362 } 2363#ifdef TCPDEBUG 2364 if (so->so_options & SO_DEBUG) 2365 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2366 &tcp_savetcp, 0); 2367#endif 2368 KASSERT(headlocked, ("headlocked should be 1")); 2369 INP_INFO_WUNLOCK(&tcbinfo); 2370 m_freem(m); 2371 tp->t_flags |= TF_ACKNOW; 2372 (void) tcp_output(tp); 2373 INP_UNLOCK(inp); 2374 return; 2375 2376dropwithreset: 2377 /* 2378 * Generate a RST, dropping incoming segment. 2379 * Make ACK acceptable to originator of segment. 2380 * Don't bother to respond if destination was broadcast/multicast. 2381 */ 2382 if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2383 goto drop; 2384 if (isipv6) { 2385 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2386 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2387 goto drop; 2388 } else { 2389 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2390 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2391 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2392 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2393 goto drop; 2394 } 2395 /* IPv6 anycast check is done at tcp6_input() */ 2396 2397 /* 2398 * Perform bandwidth limiting. 2399 */ 2400 if (badport_bandlim(rstreason) < 0) 2401 goto drop; 2402 2403#ifdef TCPDEBUG 2404 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2405 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2406 &tcp_savetcp, 0); 2407#endif 2408 2409 if (thflags & TH_ACK) 2410 /* mtod() below is safe as long as hdr dropping is delayed */ 2411 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2412 TH_RST); 2413 else { 2414 if (thflags & TH_SYN) 2415 tlen++; 2416 /* mtod() below is safe as long as hdr dropping is delayed */ 2417 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2418 (tcp_seq)0, TH_RST|TH_ACK); 2419 } 2420 2421 if (tp) 2422 INP_UNLOCK(inp); 2423 if (headlocked) 2424 INP_INFO_WUNLOCK(&tcbinfo); 2425 return; 2426 2427drop: 2428 /* 2429 * Drop space held by incoming segment and return. 2430 */ 2431#ifdef TCPDEBUG 2432 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2433 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2434 &tcp_savetcp, 0); 2435#endif 2436 if (tp) 2437 INP_UNLOCK(inp); 2438 m_freem(m); 2439 if (headlocked) 2440 INP_INFO_WUNLOCK(&tcbinfo); 2441 return; 2442} 2443 2444/* 2445 * Parse TCP options and place in tcpopt. 2446 */ 2447static void 2448tcp_dooptions(to, cp, cnt, is_syn) 2449 struct tcpopt *to; 2450 u_char *cp; 2451 int cnt; 2452 int is_syn; 2453{ 2454 int opt, optlen; 2455 2456 to->to_flags = 0; 2457 for (; cnt > 0; cnt -= optlen, cp += optlen) { 2458 opt = cp[0]; 2459 if (opt == TCPOPT_EOL) 2460 break; 2461 if (opt == TCPOPT_NOP) 2462 optlen = 1; 2463 else { 2464 if (cnt < 2) 2465 break; 2466 optlen = cp[1]; 2467 if (optlen < 2 || optlen > cnt) 2468 break; 2469 } 2470 switch (opt) { 2471 case TCPOPT_MAXSEG: 2472 if (optlen != TCPOLEN_MAXSEG) 2473 continue; 2474 if (!is_syn) 2475 continue; 2476 to->to_flags |= TOF_MSS; 2477 bcopy((char *)cp + 2, 2478 (char *)&to->to_mss, sizeof(to->to_mss)); 2479 to->to_mss = ntohs(to->to_mss); 2480 break; 2481 case TCPOPT_WINDOW: 2482 if (optlen != TCPOLEN_WINDOW) 2483 continue; 2484 if (! is_syn) 2485 continue; 2486 to->to_flags |= TOF_SCALE; 2487 to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2488 break; 2489 case TCPOPT_TIMESTAMP: 2490 if (optlen != TCPOLEN_TIMESTAMP) 2491 continue; 2492 to->to_flags |= TOF_TS; 2493 bcopy((char *)cp + 2, 2494 (char *)&to->to_tsval, sizeof(to->to_tsval)); 2495 to->to_tsval = ntohl(to->to_tsval); 2496 bcopy((char *)cp + 6, 2497 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2498 to->to_tsecr = ntohl(to->to_tsecr); 2499 break; 2500 case TCPOPT_CC: 2501 if (optlen != TCPOLEN_CC) 2502 continue; 2503 to->to_flags |= TOF_CC; 2504 bcopy((char *)cp + 2, 2505 (char *)&to->to_cc, sizeof(to->to_cc)); 2506 to->to_cc = ntohl(to->to_cc); 2507 break; 2508 case TCPOPT_CCNEW: 2509 if (optlen != TCPOLEN_CC) 2510 continue; 2511 if (!is_syn) 2512 continue; 2513 to->to_flags |= TOF_CCNEW; 2514 bcopy((char *)cp + 2, 2515 (char *)&to->to_cc, sizeof(to->to_cc)); 2516 to->to_cc = ntohl(to->to_cc); 2517 break; 2518 case TCPOPT_CCECHO: 2519 if (optlen != TCPOLEN_CC) 2520 continue; 2521 if (!is_syn) 2522 continue; 2523 to->to_flags |= TOF_CCECHO; 2524 bcopy((char *)cp + 2, 2525 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2526 to->to_ccecho = ntohl(to->to_ccecho); 2527 break; 2528#ifdef TCP_SIGNATURE 2529 /* 2530 * XXX In order to reply to a host which has set the 2531 * TCP_SIGNATURE option in its initial SYN, we have to 2532 * record the fact that the option was observed here 2533 * for the syncache code to perform the correct response. 2534 */ 2535 case TCPOPT_SIGNATURE: 2536 if (optlen != TCPOLEN_SIGNATURE) 2537 continue; 2538 to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN); 2539 break; 2540#endif /* TCP_SIGNATURE */ 2541 default: 2542 continue; 2543 } 2544 } 2545} 2546 2547/* 2548 * Pull out of band byte out of a segment so 2549 * it doesn't appear in the user's data queue. 2550 * It is still reflected in the segment length for 2551 * sequencing purposes. 2552 */ 2553static void 2554tcp_pulloutofband(so, th, m, off) 2555 struct socket *so; 2556 struct tcphdr *th; 2557 register struct mbuf *m; 2558 int off; /* delayed to be droped hdrlen */ 2559{ 2560 int cnt = off + th->th_urp - 1; 2561 2562 while (cnt >= 0) { 2563 if (m->m_len > cnt) { 2564 char *cp = mtod(m, caddr_t) + cnt; 2565 struct tcpcb *tp = sototcpcb(so); 2566 2567 tp->t_iobc = *cp; 2568 tp->t_oobflags |= TCPOOB_HAVEDATA; 2569 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2570 m->m_len--; 2571 if (m->m_flags & M_PKTHDR) 2572 m->m_pkthdr.len--; 2573 return; 2574 } 2575 cnt -= m->m_len; 2576 m = m->m_next; 2577 if (m == 0) 2578 break; 2579 } 2580 panic("tcp_pulloutofband"); 2581} 2582 2583/* 2584 * Collect new round-trip time estimate 2585 * and update averages and current timeout. 2586 */ 2587static void 2588tcp_xmit_timer(tp, rtt) 2589 register struct tcpcb *tp; 2590 int rtt; 2591{ 2592 register int delta; 2593 2594 tcpstat.tcps_rttupdated++; 2595 tp->t_rttupdated++; 2596 if (tp->t_srtt != 0) { 2597 /* 2598 * srtt is stored as fixed point with 5 bits after the 2599 * binary point (i.e., scaled by 8). The following magic 2600 * is equivalent to the smoothing algorithm in rfc793 with 2601 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2602 * point). Adjust rtt to origin 0. 2603 */ 2604 delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2605 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2606 2607 if ((tp->t_srtt += delta) <= 0) 2608 tp->t_srtt = 1; 2609 2610 /* 2611 * We accumulate a smoothed rtt variance (actually, a 2612 * smoothed mean difference), then set the retransmit 2613 * timer to smoothed rtt + 4 times the smoothed variance. 2614 * rttvar is stored as fixed point with 4 bits after the 2615 * binary point (scaled by 16). The following is 2616 * equivalent to rfc793 smoothing with an alpha of .75 2617 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2618 * rfc793's wired-in beta. 2619 */ 2620 if (delta < 0) 2621 delta = -delta; 2622 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2623 if ((tp->t_rttvar += delta) <= 0) 2624 tp->t_rttvar = 1; 2625 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 2626 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2627 } else { 2628 /* 2629 * No rtt measurement yet - use the unsmoothed rtt. 2630 * Set the variance to half the rtt (so our first 2631 * retransmit happens at 3*rtt). 2632 */ 2633 tp->t_srtt = rtt << TCP_RTT_SHIFT; 2634 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2635 tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2636 } 2637 tp->t_rtttime = 0; 2638 tp->t_rxtshift = 0; 2639 2640 /* 2641 * the retransmit should happen at rtt + 4 * rttvar. 2642 * Because of the way we do the smoothing, srtt and rttvar 2643 * will each average +1/2 tick of bias. When we compute 2644 * the retransmit timer, we want 1/2 tick of rounding and 2645 * 1 extra tick because of +-1/2 tick uncertainty in the 2646 * firing of the timer. The bias will give us exactly the 2647 * 1.5 tick we need. But, because the bias is 2648 * statistical, we have to test that we don't drop below 2649 * the minimum feasible timer (which is 2 ticks). 2650 */ 2651 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 2652 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2653 2654 /* 2655 * We received an ack for a packet that wasn't retransmitted; 2656 * it is probably safe to discard any error indications we've 2657 * received recently. This isn't quite right, but close enough 2658 * for now (a route might have failed after we sent a segment, 2659 * and the return path might not be symmetrical). 2660 */ 2661 tp->t_softerror = 0; 2662} 2663 2664/* 2665 * Determine a reasonable value for maxseg size. 2666 * If the route is known, check route for mtu. 2667 * If none, use an mss that can be handled on the outgoing 2668 * interface without forcing IP to fragment; if bigger than 2669 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2670 * to utilize large mbufs. If no route is found, route has no mtu, 2671 * or the destination isn't local, use a default, hopefully conservative 2672 * size (usually 512 or the default IP max size, but no more than the mtu 2673 * of the interface), as we can't discover anything about intervening 2674 * gateways or networks. We also initialize the congestion/slow start 2675 * window to be a single segment if the destination isn't local. 2676 * While looking at the routing entry, we also initialize other path-dependent 2677 * parameters from pre-set or cached values in the routing entry. 2678 * 2679 * Also take into account the space needed for options that we 2680 * send regularly. Make maxseg shorter by that amount to assure 2681 * that we can send maxseg amount of data even when the options 2682 * are present. Store the upper limit of the length of options plus 2683 * data in maxopd. 2684 * 2685 * 2686 * In case of T/TCP, we call this routine during implicit connection 2687 * setup as well (offer = -1), to initialize maxseg from the cached 2688 * MSS of our peer. 2689 * 2690 * NOTE that this routine is only called when we process an incoming 2691 * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 2692 */ 2693void 2694tcp_mss(tp, offer) 2695 struct tcpcb *tp; 2696 int offer; 2697{ 2698 int rtt, mss; 2699 u_long bufsize; 2700 u_long maxmtu; 2701 struct inpcb *inp = tp->t_inpcb; 2702 struct socket *so; 2703 struct hc_metrics_lite metrics; 2704 struct rmxp_tao tao; 2705 int origoffer = offer; 2706#ifdef INET6 2707 int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2708 size_t min_protoh = isipv6 ? 2709 sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2710 sizeof (struct tcpiphdr); 2711#else 2712 const size_t min_protoh = sizeof(struct tcpiphdr); 2713#endif 2714 bzero(&tao, sizeof(tao)); 2715 2716 /* initialize */ 2717#ifdef INET6 2718 if (isipv6) { 2719 maxmtu = tcp_maxmtu6(&inp->inp_inc); 2720 tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt; 2721 } else 2722#endif 2723 { 2724 maxmtu = tcp_maxmtu(&inp->inp_inc); 2725 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2726 } 2727 so = inp->inp_socket; 2728 2729 /* 2730 * no route to sender, take default mss and return 2731 */ 2732 if (maxmtu == 0) 2733 return; 2734 2735 /* what have we got? */ 2736 switch (offer) { 2737 case 0: 2738 /* 2739 * Offer == 0 means that there was no MSS on the SYN 2740 * segment, in this case we use tcp_mssdflt. 2741 */ 2742 offer = 2743#ifdef INET6 2744 isipv6 ? tcp_v6mssdflt : 2745#endif 2746 tcp_mssdflt; 2747 break; 2748 2749 case -1: 2750 /* 2751 * Offer == -1 means that we didn't receive SYN yet, 2752 * use cached value in that case; 2753 */ 2754 if (tcp_do_rfc1644) 2755 tcp_hc_gettao(&inp->inp_inc, &tao); 2756 if (tao.tao_mssopt != 0) 2757 offer = tao.tao_mssopt; 2758 /* FALLTHROUGH */ 2759 2760 default: 2761 /* 2762 * Prevent DoS attack with too small MSS. Round up 2763 * to at least minmss. 2764 */ 2765 offer = max(offer, tcp_minmss); 2766 /* 2767 * Sanity check: make sure that maxopd will be large 2768 * enough to allow some data on segments even if the 2769 * all the option space is used (40bytes). Otherwise 2770 * funny things may happen in tcp_output. 2771 */ 2772 offer = max(offer, 64); 2773 if (tcp_do_rfc1644) 2774 tcp_hc_updatetao(&inp->inp_inc, 2775 TCP_HC_TAO_MSSOPT, 0, offer); 2776 } 2777 2778 /* 2779 * rmx information is now retrieved from tcp_hostcache 2780 */ 2781 tcp_hc_get(&inp->inp_inc, &metrics); 2782 2783 /* 2784 * if there's a discovered mtu int tcp hostcache, use it 2785 * else, use the link mtu. 2786 */ 2787 if (metrics.rmx_mtu) 2788 mss = metrics.rmx_mtu - min_protoh; 2789 else { 2790#ifdef INET6 2791 if (isipv6) { 2792 mss = maxmtu - min_protoh; 2793 if (!path_mtu_discovery && 2794 !in6_localaddr(&inp->in6p_faddr)) 2795 mss = min(mss, tcp_v6mssdflt); 2796 } else 2797#endif 2798 { 2799 mss = maxmtu - min_protoh; 2800 if (!path_mtu_discovery && 2801 !in_localaddr(inp->inp_faddr)) 2802 mss = min(mss, tcp_mssdflt); 2803 } 2804 } 2805 mss = min(mss, offer); 2806 2807 /* 2808 * maxopd stores the maximum length of data AND options 2809 * in a segment; maxseg is the amount of data in a normal 2810 * segment. We need to store this value (maxopd) apart 2811 * from maxseg, because now every segment carries options 2812 * and thus we normally have somewhat less data in segments. 2813 */ 2814 tp->t_maxopd = mss; 2815 2816 /* 2817 * In case of T/TCP, origoffer==-1 indicates, that no segments 2818 * were received yet. In this case we just guess, otherwise 2819 * we do the same as before T/TCP. 2820 */ 2821 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2822 (origoffer == -1 || 2823 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2824 mss -= TCPOLEN_TSTAMP_APPA; 2825 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2826 (origoffer == -1 || 2827 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2828 mss -= TCPOLEN_CC_APPA; 2829 tp->t_maxseg = mss; 2830 2831#if (MCLBYTES & (MCLBYTES - 1)) == 0 2832 if (mss > MCLBYTES) 2833 mss &= ~(MCLBYTES-1); 2834#else 2835 if (mss > MCLBYTES) 2836 mss = mss / MCLBYTES * MCLBYTES; 2837#endif 2838 tp->t_maxseg = mss; 2839 2840 /* 2841 * If there's a pipesize, change the socket buffer to that size, 2842 * don't change if sb_hiwat is different than default (then it 2843 * has been changed on purpose with setsockopt). 2844 * Make the socket buffers an integral number of mss units; 2845 * if the mss is larger than the socket buffer, decrease the mss. 2846 */ 2847 if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 2848 bufsize = metrics.rmx_sendpipe; 2849 else 2850 bufsize = so->so_snd.sb_hiwat; 2851 if (bufsize < mss) 2852 mss = bufsize; 2853 else { 2854 bufsize = roundup(bufsize, mss); 2855 if (bufsize > sb_max) 2856 bufsize = sb_max; 2857 if (bufsize > so->so_snd.sb_hiwat) 2858 (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2859 } 2860 tp->t_maxseg = mss; 2861 2862 if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 2863 bufsize = metrics.rmx_recvpipe; 2864 else 2865 bufsize = so->so_rcv.sb_hiwat; 2866 if (bufsize > mss) { 2867 bufsize = roundup(bufsize, mss); 2868 if (bufsize > sb_max) 2869 bufsize = sb_max; 2870 if (bufsize > so->so_rcv.sb_hiwat) 2871 (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2872 } 2873 /* 2874 * While we're here, check the others too 2875 */ 2876 if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 2877 tp->t_srtt = rtt; 2878 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 2879 tcpstat.tcps_usedrtt++; 2880 if (metrics.rmx_rttvar) { 2881 tp->t_rttvar = metrics.rmx_rttvar; 2882 tcpstat.tcps_usedrttvar++; 2883 } else { 2884 /* default variation is +- 1 rtt */ 2885 tp->t_rttvar = 2886 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2887 } 2888 TCPT_RANGESET(tp->t_rxtcur, 2889 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2890 tp->t_rttmin, TCPTV_REXMTMAX); 2891 } 2892 if (metrics.rmx_ssthresh) { 2893 /* 2894 * There's some sort of gateway or interface 2895 * buffer limit on the path. Use this to set 2896 * the slow start threshhold, but set the 2897 * threshold to no less than 2*mss. 2898 */ 2899 tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 2900 tcpstat.tcps_usedssthresh++; 2901 } 2902 if (metrics.rmx_bandwidth) 2903 tp->snd_bandwidth = metrics.rmx_bandwidth; 2904 2905 /* 2906 * Set the slow-start flight size depending on whether this 2907 * is a local network or not. 2908 * 2909 * Extend this so we cache the cwnd too and retrieve it here. 2910 * Make cwnd even bigger than RFC3390 suggests but only if we 2911 * have previous experience with the remote host. Be careful 2912 * not make cwnd bigger than remote receive window or our own 2913 * send socket buffer. Maybe put some additional upper bound 2914 * on the retrieved cwnd. Should do incremental updates to 2915 * hostcache when cwnd collapses so next connection doesn't 2916 * overloads the path again. 2917 * 2918 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 2919 * We currently check only in syncache_socket for that. 2920 */ 2921#define TCP_METRICS_CWND 2922#ifdef TCP_METRICS_CWND 2923 if (metrics.rmx_cwnd) 2924 tp->snd_cwnd = max(mss, 2925 min(metrics.rmx_cwnd / 2, 2926 min(tp->snd_wnd, so->so_snd.sb_hiwat))); 2927 else 2928#endif 2929 if (tcp_do_rfc3390) 2930 tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 2931#ifdef INET6 2932 else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2933 (!isipv6 && in_localaddr(inp->inp_faddr))) 2934#else 2935 else if (in_localaddr(inp->inp_faddr)) 2936#endif 2937 tp->snd_cwnd = mss * ss_fltsz_local; 2938 else 2939 tp->snd_cwnd = mss * ss_fltsz; 2940} 2941 2942/* 2943 * Determine the MSS option to send on an outgoing SYN. 2944 */ 2945int 2946tcp_mssopt(inc) 2947 struct in_conninfo *inc; 2948{ 2949 int mss = 0; 2950 u_long maxmtu = 0; 2951 u_long thcmtu = 0; 2952 size_t min_protoh; 2953#ifdef INET6 2954 int isipv6 = inc->inc_isipv6 ? 1 : 0; 2955#endif 2956 2957 KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 2958 2959#ifdef INET6 2960 if (isipv6) { 2961 mss = tcp_v6mssdflt; 2962 maxmtu = tcp_maxmtu6(inc); 2963 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 2964 min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 2965 } else 2966#endif 2967 { 2968 mss = tcp_mssdflt; 2969 maxmtu = tcp_maxmtu(inc); 2970 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 2971 min_protoh = sizeof(struct tcpiphdr); 2972 } 2973 if (maxmtu && thcmtu) 2974 mss = min(maxmtu, thcmtu) - min_protoh; 2975 else if (maxmtu || thcmtu) 2976 mss = max(maxmtu, thcmtu) - min_protoh; 2977 2978 return (mss); 2979} 2980 2981 2982/* 2983 * On a partial ack arrives, force the retransmission of the 2984 * next unacknowledged segment. Do not clear tp->t_dupacks. 2985 * By setting snd_nxt to ti_ack, this forces retransmission timer to 2986 * be started again. 2987 */ 2988static void 2989tcp_newreno_partial_ack(tp, th) 2990 struct tcpcb *tp; 2991 struct tcphdr *th; 2992{ 2993 tcp_seq onxt = tp->snd_nxt; 2994 u_long ocwnd = tp->snd_cwnd; 2995 2996 callout_stop(tp->tt_rexmt); 2997 tp->t_rtttime = 0; 2998 tp->snd_nxt = th->th_ack; 2999 /* 3000 * Set snd_cwnd to one segment beyond acknowledged offset. 3001 * (tp->snd_una has not yet been updated when this function is called.) 3002 */ 3003 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3004 tp->t_flags |= TF_ACKNOW; 3005 (void) tcp_output(tp); 3006 tp->snd_cwnd = ocwnd; 3007 if (SEQ_GT(onxt, tp->snd_nxt)) 3008 tp->snd_nxt = onxt; 3009 /* 3010 * Partial window deflation. Relies on fact that tp->snd_una 3011 * not updated yet. 3012 */ 3013 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 3014} 3015 3016/* 3017 * Returns 1 if the TIME_WAIT state was killed and we should start over, 3018 * looking for a pcb in the listen state. Returns 0 otherwise. 3019 */ 3020static int 3021tcp_timewait(tw, to, th, m, tlen) 3022 struct tcptw *tw; 3023 struct tcpopt *to; 3024 struct tcphdr *th; 3025 struct mbuf *m; 3026 int tlen; 3027{ 3028 int thflags; 3029 tcp_seq seq; 3030#ifdef INET6 3031 int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 3032#else 3033 const int isipv6 = 0; 3034#endif 3035 3036 thflags = th->th_flags; 3037 3038 /* 3039 * NOTE: for FIN_WAIT_2 (to be added later), 3040 * must validate sequence number before accepting RST 3041 */ 3042 3043 /* 3044 * If the segment contains RST: 3045 * Drop the segment - see Stevens, vol. 2, p. 964 and 3046 * RFC 1337. 3047 */ 3048 if (thflags & TH_RST) 3049 goto drop; 3050 3051 /* 3052 * If segment contains a SYN and CC [not CC.NEW] option: 3053 * if connection duration > MSL, drop packet and send RST; 3054 * 3055 * if SEG.CC > CCrecv then is new SYN. 3056 * Complete close and delete TCPCB. Then reprocess 3057 * segment, hoping to find new TCPCB in LISTEN state; 3058 * 3059 * else must be old SYN; drop it. 3060 * else do normal processing. 3061 */ 3062 if ((thflags & TH_SYN) && (to->to_flags & TOF_CC) && tw->cc_recv != 0) { 3063 if ((ticks - tw->t_starttime) > tcp_msl) 3064 goto reset; 3065 if (CC_GT(to->to_cc, tw->cc_recv)) { 3066 (void) tcp_twclose(tw, 0); 3067 return (1); 3068 } 3069 goto drop; 3070 } 3071 3072#if 0 3073/* PAWS not needed at the moment */ 3074 /* 3075 * RFC 1323 PAWS: If we have a timestamp reply on this segment 3076 * and it's less than ts_recent, drop it. 3077 */ 3078 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 3079 TSTMP_LT(to.to_tsval, tp->ts_recent)) { 3080 if ((thflags & TH_ACK) == 0) 3081 goto drop; 3082 goto ack; 3083 } 3084 /* 3085 * ts_recent is never updated because we never accept new segments. 3086 */ 3087#endif 3088 3089 /* 3090 * If a new connection request is received 3091 * while in TIME_WAIT, drop the old connection 3092 * and start over if the sequence numbers 3093 * are above the previous ones. 3094 */ 3095 if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 3096 (void) tcp_twclose(tw, 0); 3097 return (1); 3098 } 3099 3100 /* 3101 * Drop the the segment if it does not contain an ACK. 3102 */ 3103 if ((thflags & TH_ACK) == 0) 3104 goto drop; 3105 3106 /* 3107 * Reset the 2MSL timer if this is a duplicate FIN. 3108 */ 3109 if (thflags & TH_FIN) { 3110 seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 3111 if (seq + 1 == tw->rcv_nxt) 3112 tcp_timer_2msl_reset(tw, 2 * tcp_msl); 3113 } 3114 3115 /* 3116 * Acknowledge the segment if it has data or is not a duplicate ACK. 3117 */ 3118 if (thflags != TH_ACK || tlen != 0 || 3119 th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 3120 tcp_twrespond(tw, NULL, m, TH_ACK); 3121 goto drop; 3122 3123reset: 3124 /* 3125 * Generate a RST, dropping incoming segment. 3126 * Make ACK acceptable to originator of segment. 3127 * Don't bother to respond if destination was broadcast/multicast. 3128 */ 3129 if (m->m_flags & (M_BCAST|M_MCAST)) 3130 goto drop; 3131 if (isipv6) { 3132 struct ip6_hdr *ip6; 3133 3134 /* IPv6 anycast check is done at tcp6_input() */ 3135 ip6 = mtod(m, struct ip6_hdr *); 3136 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3137 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3138 goto drop; 3139 } else { 3140 struct ip *ip; 3141 3142 ip = mtod(m, struct ip *); 3143 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3144 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3145 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3146 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3147 goto drop; 3148 } 3149 if (thflags & TH_ACK) { 3150 tcp_respond(NULL, 3151 mtod(m, void *), th, m, 0, th->th_ack, TH_RST); 3152 } else { 3153 seq = th->th_seq + (thflags & TH_SYN ? 1 : 0); 3154 tcp_respond(NULL, 3155 mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK); 3156 } 3157 INP_UNLOCK(tw->tw_inpcb); 3158 return (0); 3159 3160drop: 3161 INP_UNLOCK(tw->tw_inpcb); 3162 m_freem(m); 3163 return (0); 3164} 3165