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