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