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