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