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