tcp_input.c revision 12296
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 * $Id: tcp_input.c,v 1.32 1995/11/09 20:23:02 phk Exp $ 35 */ 36 37#ifndef TUBA_INCLUDE 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/kernel.h> 41#include <sys/sysctl.h> 42#include <sys/malloc.h> 43#include <sys/mbuf.h> 44#include <sys/protosw.h> 45#include <sys/socket.h> 46#include <sys/socketvar.h> 47#include <sys/errno.h> 48#include <sys/queue.h> 49 50#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 51 52#include <net/if.h> 53#include <net/route.h> 54 55#include <netinet/in.h> 56#include <netinet/in_systm.h> 57#include <netinet/ip.h> 58#include <netinet/in_pcb.h> 59#include <netinet/ip_var.h> 60#include <netinet/tcp.h> 61#include <netinet/tcp_fsm.h> 62#include <netinet/tcp_seq.h> 63#include <netinet/tcp_timer.h> 64#include <netinet/tcp_var.h> 65#include <netinet/tcpip.h> 66#ifdef TCPDEBUG 67#include <netinet/tcp_debug.h> 68struct tcpiphdr tcp_saveti; 69#endif 70 71static int tcprexmtthresh = 3; 72tcp_seq tcp_iss; 73tcp_cc tcp_ccgen; 74 75struct tcpstat tcpstat; 76SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, 77 CTLFLAG_RD, &tcpstat , tcpstat, ""); 78 79u_long tcp_now; 80struct inpcbhead tcb; 81struct inpcbinfo tcbinfo; 82 83static void tcp_dooptions __P((struct tcpcb *, 84 u_char *, int, struct tcpiphdr *, struct tcpopt *)); 85static void tcp_pulloutofband __P((struct socket *, 86 struct tcpiphdr *, struct mbuf *)); 87static int tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *)); 88static void tcp_xmit_timer __P((struct tcpcb *, int)); 89 90#endif /* TUBA_INCLUDE */ 91 92/* 93 * Insert segment ti into reassembly queue of tcp with 94 * control block tp. Return TH_FIN if reassembly now includes 95 * a segment with FIN. The macro form does the common case inline 96 * (segment is the next to be received on an established connection, 97 * and the queue is empty), avoiding linkage into and removal 98 * from the queue and repetition of various conversions. 99 * Set DELACK for segments received in order, but ack immediately 100 * when segments are out of order (so fast retransmit can work). 101 */ 102#ifdef TCP_ACK_HACK 103#define TCP_REASS(tp, ti, m, so, flags) { \ 104 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 105 (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 106 (tp)->t_state == TCPS_ESTABLISHED) { \ 107 if (ti->ti_flags & TH_PUSH) \ 108 tp->t_flags |= TF_ACKNOW; \ 109 else \ 110 tp->t_flags |= TF_DELACK; \ 111 (tp)->rcv_nxt += (ti)->ti_len; \ 112 flags = (ti)->ti_flags & TH_FIN; \ 113 tcpstat.tcps_rcvpack++;\ 114 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 115 sbappend(&(so)->so_rcv, (m)); \ 116 sorwakeup(so); \ 117 } else { \ 118 (flags) = tcp_reass((tp), (ti), (m)); \ 119 tp->t_flags |= TF_ACKNOW; \ 120 } \ 121} 122#else 123#define TCP_REASS(tp, ti, m, so, flags) { \ 124 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 125 (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 126 (tp)->t_state == TCPS_ESTABLISHED) { \ 127 tp->t_flags |= TF_DELACK; \ 128 (tp)->rcv_nxt += (ti)->ti_len; \ 129 flags = (ti)->ti_flags & TH_FIN; \ 130 tcpstat.tcps_rcvpack++;\ 131 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 132 sbappend(&(so)->so_rcv, (m)); \ 133 sorwakeup(so); \ 134 } else { \ 135 (flags) = tcp_reass((tp), (ti), (m)); \ 136 tp->t_flags |= TF_ACKNOW; \ 137 } \ 138} 139#endif 140#ifndef TUBA_INCLUDE 141 142static int 143tcp_reass(tp, ti, m) 144 register struct tcpcb *tp; 145 register struct tcpiphdr *ti; 146 struct mbuf *m; 147{ 148 register struct tcpiphdr *q; 149 struct socket *so = tp->t_inpcb->inp_socket; 150 int flags; 151 152 /* 153 * Call with ti==0 after become established to 154 * force pre-ESTABLISHED data up to user socket. 155 */ 156 if (ti == 0) 157 goto present; 158 159 /* 160 * Find a segment which begins after this one does. 161 */ 162 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 163 q = (struct tcpiphdr *)q->ti_next) 164 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 165 break; 166 167 /* 168 * If there is a preceding segment, it may provide some of 169 * our data already. If so, drop the data from the incoming 170 * segment. If it provides all of our data, drop us. 171 */ 172 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 173 register int i; 174 q = (struct tcpiphdr *)q->ti_prev; 175 /* conversion to int (in i) handles seq wraparound */ 176 i = q->ti_seq + q->ti_len - ti->ti_seq; 177 if (i > 0) { 178 if (i >= ti->ti_len) { 179 tcpstat.tcps_rcvduppack++; 180 tcpstat.tcps_rcvdupbyte += ti->ti_len; 181 m_freem(m); 182 /* 183 * Try to present any queued data 184 * at the left window edge to the user. 185 * This is needed after the 3-WHS 186 * completes. 187 */ 188 goto present; /* ??? */ 189 } 190 m_adj(m, i); 191 ti->ti_len -= i; 192 ti->ti_seq += i; 193 } 194 q = (struct tcpiphdr *)(q->ti_next); 195 } 196 tcpstat.tcps_rcvoopack++; 197 tcpstat.tcps_rcvoobyte += ti->ti_len; 198 REASS_MBUF(ti) = m; /* XXX */ 199 200 /* 201 * While we overlap succeeding segments trim them or, 202 * if they are completely covered, dequeue them. 203 */ 204 while (q != (struct tcpiphdr *)tp) { 205 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 206 if (i <= 0) 207 break; 208 if (i < q->ti_len) { 209 q->ti_seq += i; 210 q->ti_len -= i; 211 m_adj(REASS_MBUF(q), i); 212 break; 213 } 214 q = (struct tcpiphdr *)q->ti_next; 215 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev); 216 remque(q->ti_prev); 217 m_freem(m); 218 } 219 220 /* 221 * Stick new segment in its place. 222 */ 223 insque(ti, q->ti_prev); 224 225present: 226 /* 227 * Present data to user, advancing rcv_nxt through 228 * completed sequence space. 229 */ 230 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 231 return (0); 232 ti = tp->seg_next; 233 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 234 return (0); 235 do { 236 tp->rcv_nxt += ti->ti_len; 237 flags = ti->ti_flags & TH_FIN; 238 remque(ti); 239 m = REASS_MBUF(ti); 240 ti = (struct tcpiphdr *)ti->ti_next; 241 if (so->so_state & SS_CANTRCVMORE) 242 m_freem(m); 243 else 244 sbappend(&so->so_rcv, m); 245 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 246 sorwakeup(so); 247 return (flags); 248} 249 250/* 251 * TCP input routine, follows pages 65-76 of the 252 * protocol specification dated September, 1981 very closely. 253 */ 254void 255tcp_input(m, iphlen) 256 register struct mbuf *m; 257 int iphlen; 258{ 259 register struct tcpiphdr *ti; 260 register struct inpcb *inp; 261 u_char *optp = NULL; 262 int optlen = 0; 263 int len, tlen, off; 264 register struct tcpcb *tp = 0; 265 register int tiflags; 266 struct socket *so = 0; 267 int todrop, acked, ourfinisacked, needoutput = 0; 268 struct in_addr laddr; 269 int dropsocket = 0; 270 int iss = 0; 271 u_long tiwin; 272 struct tcpopt to; /* options in this segment */ 273 struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 274 struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 275#ifdef TCPDEBUG 276 short ostate = 0; 277#endif 278 279 bzero((char *)&to, sizeof(to)); 280 281 tcpstat.tcps_rcvtotal++; 282 /* 283 * Get IP and TCP header together in first mbuf. 284 * Note: IP leaves IP header in first mbuf. 285 */ 286 ti = mtod(m, struct tcpiphdr *); 287 if (iphlen > sizeof (struct ip)) 288 ip_stripoptions(m, (struct mbuf *)0); 289 if (m->m_len < sizeof (struct tcpiphdr)) { 290 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 291 tcpstat.tcps_rcvshort++; 292 return; 293 } 294 ti = mtod(m, struct tcpiphdr *); 295 } 296 297 /* 298 * Checksum extended TCP header and data. 299 */ 300 tlen = ((struct ip *)ti)->ip_len; 301 len = sizeof (struct ip) + tlen; 302 ti->ti_next = ti->ti_prev = 0; 303 ti->ti_x1 = 0; 304 ti->ti_len = (u_short)tlen; 305 HTONS(ti->ti_len); 306 ti->ti_sum = in_cksum(m, len); 307 if (ti->ti_sum) { 308 tcpstat.tcps_rcvbadsum++; 309 goto drop; 310 } 311#endif /* TUBA_INCLUDE */ 312 313 /* 314 * Check that TCP offset makes sense, 315 * pull out TCP options and adjust length. XXX 316 */ 317 off = ti->ti_off << 2; 318 if (off < sizeof (struct tcphdr) || off > tlen) { 319 tcpstat.tcps_rcvbadoff++; 320 goto drop; 321 } 322 tlen -= off; 323 ti->ti_len = tlen; 324 if (off > sizeof (struct tcphdr)) { 325 if (m->m_len < sizeof(struct ip) + off) { 326 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 327 tcpstat.tcps_rcvshort++; 328 return; 329 } 330 ti = mtod(m, struct tcpiphdr *); 331 } 332 optlen = off - sizeof (struct tcphdr); 333 optp = mtod(m, u_char *) + sizeof (struct tcpiphdr); 334 /* 335 * Do quick retrieval of timestamp options ("options 336 * prediction?"). If timestamp is the only option and it's 337 * formatted as recommended in RFC 1323 appendix A, we 338 * quickly get the values now and not bother calling 339 * tcp_dooptions(), etc. 340 */ 341 if ((optlen == TCPOLEN_TSTAMP_APPA || 342 (optlen > TCPOLEN_TSTAMP_APPA && 343 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && 344 *(u_long *)optp == htonl(TCPOPT_TSTAMP_HDR) && 345 (ti->ti_flags & TH_SYN) == 0) { 346 to.to_flag |= TOF_TS; 347 to.to_tsval = ntohl(*(u_long *)(optp + 4)); 348 to.to_tsecr = ntohl(*(u_long *)(optp + 8)); 349 optp = NULL; /* we've parsed the options */ 350 } 351 } 352 tiflags = ti->ti_flags; 353 354 /* 355 * Convert TCP protocol specific fields to host format. 356 */ 357 NTOHL(ti->ti_seq); 358 NTOHL(ti->ti_ack); 359 NTOHS(ti->ti_win); 360 NTOHS(ti->ti_urp); 361 362 /* 363 * Drop TCP, IP headers and TCP options. 364 */ 365 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 366 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 367 368 /* 369 * Locate pcb for segment. 370 */ 371findpcb: 372 /* 373 * First look for an exact match. 374 */ 375 inp = in_pcblookuphash(&tcbinfo, ti->ti_src, ti->ti_sport, 376 ti->ti_dst, ti->ti_dport); 377 /* 378 * ...and if that fails, do a wildcard search. 379 */ 380 if (inp == NULL) { 381 inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport, 382 ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD); 383 } 384 385 /* 386 * If the state is CLOSED (i.e., TCB does not exist) then 387 * all data in the incoming segment is discarded. 388 * If the TCB exists but is in CLOSED state, it is embryonic, 389 * but should either do a listen or a connect soon. 390 */ 391 if (inp == NULL) 392 goto dropwithreset; 393 tp = intotcpcb(inp); 394 if (tp == 0) 395 goto dropwithreset; 396 if (tp->t_state == TCPS_CLOSED) 397 goto drop; 398 399 /* Unscale the window into a 32-bit value. */ 400 if ((tiflags & TH_SYN) == 0) 401 tiwin = ti->ti_win << tp->snd_scale; 402 else 403 tiwin = ti->ti_win; 404 405 so = inp->inp_socket; 406 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 407#ifdef TCPDEBUG 408 if (so->so_options & SO_DEBUG) { 409 ostate = tp->t_state; 410 tcp_saveti = *ti; 411 } 412#endif 413 if (so->so_options & SO_ACCEPTCONN) { 414 register struct tcpcb *tp0 = tp; 415 so = sonewconn(so, 0); 416 if (so == 0) 417 goto drop; 418 /* 419 * This is ugly, but .... 420 * 421 * Mark socket as temporary until we're 422 * committed to keeping it. The code at 423 * ``drop'' and ``dropwithreset'' check the 424 * flag dropsocket to see if the temporary 425 * socket created here should be discarded. 426 * We mark the socket as discardable until 427 * we're committed to it below in TCPS_LISTEN. 428 */ 429 dropsocket++; 430 inp = (struct inpcb *)so->so_pcb; 431 inp->inp_laddr = ti->ti_dst; 432 inp->inp_lport = ti->ti_dport; 433 in_pcbrehash(inp); 434#if BSD>=43 435 inp->inp_options = ip_srcroute(); 436#endif 437 tp = intotcpcb(inp); 438 tp->t_state = TCPS_LISTEN; 439 tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 440 441 /* Compute proper scaling value from buffer space */ 442 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 443 TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat) 444 tp->request_r_scale++; 445 } 446 } 447 448 /* 449 * Segment received on connection. 450 * Reset idle time and keep-alive timer. 451 */ 452 tp->t_idle = 0; 453 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 454 455 /* 456 * Process options if not in LISTEN state, 457 * else do it below (after getting remote address). 458 */ 459 if (optp && tp->t_state != TCPS_LISTEN) 460 tcp_dooptions(tp, optp, optlen, ti, 461 &to); 462 463 /* 464 * Header prediction: check for the two common cases 465 * of a uni-directional data xfer. If the packet has 466 * no control flags, is in-sequence, the window didn't 467 * change and we're not retransmitting, it's a 468 * candidate. If the length is zero and the ack moved 469 * forward, we're the sender side of the xfer. Just 470 * free the data acked & wake any higher level process 471 * that was blocked waiting for space. If the length 472 * is non-zero and the ack didn't move, we're the 473 * receiver side. If we're getting packets in-order 474 * (the reassembly queue is empty), add the data to 475 * the socket buffer and note that we need a delayed ack. 476 * Make sure that the hidden state-flags are also off. 477 * Since we check for TCPS_ESTABLISHED above, it can only 478 * be TH_NEEDSYN. 479 */ 480 if (tp->t_state == TCPS_ESTABLISHED && 481 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 482 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 483 ((to.to_flag & TOF_TS) == 0 || 484 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 485 /* 486 * Using the CC option is compulsory if once started: 487 * the segment is OK if no T/TCP was negotiated or 488 * if the segment has a CC option equal to CCrecv 489 */ 490 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 491 (to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv) && 492 ti->ti_seq == tp->rcv_nxt && 493 tiwin && tiwin == tp->snd_wnd && 494 tp->snd_nxt == tp->snd_max) { 495 496 /* 497 * If last ACK falls within this segment's sequence numbers, 498 * record the timestamp. 499 * NOTE that the test is modified according to the latest 500 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 501 */ 502 if ((to.to_flag & TOF_TS) != 0 && 503 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 504 tp->ts_recent_age = tcp_now; 505 tp->ts_recent = to.to_tsval; 506 } 507 508 if (ti->ti_len == 0) { 509 if (SEQ_GT(ti->ti_ack, tp->snd_una) && 510 SEQ_LEQ(ti->ti_ack, tp->snd_max) && 511 tp->snd_cwnd >= tp->snd_wnd) { 512 /* 513 * this is a pure ack for outstanding data. 514 */ 515 ++tcpstat.tcps_predack; 516 if ((to.to_flag & TOF_TS) != 0) 517 tcp_xmit_timer(tp, 518 tcp_now - to.to_tsecr + 1); 519 else if (tp->t_rtt && 520 SEQ_GT(ti->ti_ack, tp->t_rtseq)) 521 tcp_xmit_timer(tp, tp->t_rtt); 522 acked = ti->ti_ack - tp->snd_una; 523 tcpstat.tcps_rcvackpack++; 524 tcpstat.tcps_rcvackbyte += acked; 525 sbdrop(&so->so_snd, acked); 526 tp->snd_una = ti->ti_ack; 527 m_freem(m); 528 529 /* 530 * If all outstanding data are acked, stop 531 * retransmit timer, otherwise restart timer 532 * using current (possibly backed-off) value. 533 * If process is waiting for space, 534 * wakeup/selwakeup/signal. If data 535 * are ready to send, let tcp_output 536 * decide between more output or persist. 537 */ 538 if (tp->snd_una == tp->snd_max) 539 tp->t_timer[TCPT_REXMT] = 0; 540 else if (tp->t_timer[TCPT_PERSIST] == 0) 541 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 542 543 if (so->so_snd.sb_flags & SB_NOTIFY) 544 sowwakeup(so); 545 if (so->so_snd.sb_cc) 546 (void) tcp_output(tp); 547 return; 548 } 549 } else if (ti->ti_ack == tp->snd_una && 550 tp->seg_next == (struct tcpiphdr *)tp && 551 ti->ti_len <= sbspace(&so->so_rcv)) { 552 /* 553 * this is a pure, in-sequence data packet 554 * with nothing on the reassembly queue and 555 * we have enough buffer space to take it. 556 */ 557 ++tcpstat.tcps_preddat; 558 tp->rcv_nxt += ti->ti_len; 559 tcpstat.tcps_rcvpack++; 560 tcpstat.tcps_rcvbyte += ti->ti_len; 561 /* 562 * Add data to socket buffer. 563 */ 564 sbappend(&so->so_rcv, m); 565 sorwakeup(so); 566#ifdef TCP_ACK_HACK 567 /* 568 * If this is a short packet, then ACK now - with Nagel 569 * congestion avoidance sender won't send more until 570 * he gets an ACK. 571 */ 572 if (tiflags & TH_PUSH) { 573 tp->t_flags |= TF_ACKNOW; 574 tcp_output(tp); 575 } else { 576 tp->t_flags |= TF_DELACK; 577 } 578#else 579 tp->t_flags |= TF_DELACK; 580#endif 581 return; 582 } 583 } 584 585 /* 586 * Calculate amount of space in receive window, 587 * and then do TCP input processing. 588 * Receive window is amount of space in rcv queue, 589 * but not less than advertised window. 590 */ 591 { int win; 592 593 win = sbspace(&so->so_rcv); 594 if (win < 0) 595 win = 0; 596 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 597 } 598 599 switch (tp->t_state) { 600 601 /* 602 * If the state is LISTEN then ignore segment if it contains an RST. 603 * If the segment contains an ACK then it is bad and send a RST. 604 * If it does not contain a SYN then it is not interesting; drop it. 605 * Don't bother responding if the destination was a broadcast. 606 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 607 * tp->iss, and send a segment: 608 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 609 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 610 * Fill in remote peer address fields if not previously specified. 611 * Enter SYN_RECEIVED state, and process any other fields of this 612 * segment in this state. 613 */ 614 case TCPS_LISTEN: { 615 struct mbuf *am; 616 register struct sockaddr_in *sin; 617 618 if (tiflags & TH_RST) 619 goto drop; 620 if (tiflags & TH_ACK) 621 goto dropwithreset; 622 if ((tiflags & TH_SYN) == 0) 623 goto drop; 624 /* 625 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 626 * in_broadcast() should never return true on a received 627 * packet with M_BCAST not set. 628 */ 629 if (m->m_flags & (M_BCAST|M_MCAST) || 630 IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 631 goto drop; 632 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 633 if (am == NULL) 634 goto drop; 635 am->m_len = sizeof (struct sockaddr_in); 636 sin = mtod(am, struct sockaddr_in *); 637 sin->sin_family = AF_INET; 638 sin->sin_len = sizeof(*sin); 639 sin->sin_addr = ti->ti_src; 640 sin->sin_port = ti->ti_sport; 641 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 642 laddr = inp->inp_laddr; 643 if (inp->inp_laddr.s_addr == INADDR_ANY) 644 inp->inp_laddr = ti->ti_dst; 645 if (in_pcbconnect(inp, am)) { 646 inp->inp_laddr = laddr; 647 (void) m_free(am); 648 goto drop; 649 } 650 (void) m_free(am); 651 tp->t_template = tcp_template(tp); 652 if (tp->t_template == 0) { 653 tp = tcp_drop(tp, ENOBUFS); 654 dropsocket = 0; /* socket is already gone */ 655 goto drop; 656 } 657 if ((taop = tcp_gettaocache(inp)) == NULL) { 658 taop = &tao_noncached; 659 bzero(taop, sizeof(*taop)); 660 } 661 if (optp) 662 tcp_dooptions(tp, optp, optlen, ti, 663 &to); 664 if (iss) 665 tp->iss = iss; 666 else 667 tp->iss = tcp_iss; 668 tcp_iss += TCP_ISSINCR/4; 669 tp->irs = ti->ti_seq; 670 tcp_sendseqinit(tp); 671 tcp_rcvseqinit(tp); 672 /* 673 * Initialization of the tcpcb for transaction; 674 * set SND.WND = SEG.WND, 675 * initialize CCsend and CCrecv. 676 */ 677 tp->snd_wnd = tiwin; /* initial send-window */ 678 tp->cc_send = CC_INC(tcp_ccgen); 679 tp->cc_recv = to.to_cc; 680 /* 681 * Perform TAO test on incoming CC (SEG.CC) option, if any. 682 * - compare SEG.CC against cached CC from the same host, 683 * if any. 684 * - if SEG.CC > chached value, SYN must be new and is accepted 685 * immediately: save new CC in the cache, mark the socket 686 * connected, enter ESTABLISHED state, turn on flag to 687 * send a SYN in the next segment. 688 * A virtual advertised window is set in rcv_adv to 689 * initialize SWS prevention. Then enter normal segment 690 * processing: drop SYN, process data and FIN. 691 * - otherwise do a normal 3-way handshake. 692 */ 693 if ((to.to_flag & TOF_CC) != 0) { 694 if (taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 695 taop->tao_cc = to.to_cc; 696 tp->t_state = TCPS_ESTABLISHED; 697 698 /* 699 * If there is a FIN, or if there is data and the 700 * connection is local, then delay SYN,ACK(SYN) in 701 * the hope of piggy-backing it on a response 702 * segment. Otherwise must send ACK now in case 703 * the other side is slow starting. 704 */ 705 if ((tiflags & TH_FIN) || (ti->ti_len != 0 && 706 in_localaddr(inp->inp_faddr))) 707 tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 708 else 709 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 710 tp->rcv_adv += tp->rcv_wnd; 711 tcpstat.tcps_connects++; 712 soisconnected(so); 713 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 714 dropsocket = 0; /* committed to socket */ 715 tcpstat.tcps_accepts++; 716 goto trimthenstep6; 717 } 718 /* else do standard 3-way handshake */ 719 } else { 720 /* 721 * No CC option, but maybe CC.NEW: 722 * invalidate cached value. 723 */ 724 taop->tao_cc = 0; 725 } 726 /* 727 * TAO test failed or there was no CC option, 728 * do a standard 3-way handshake. 729 */ 730 tp->t_flags |= TF_ACKNOW; 731 tp->t_state = TCPS_SYN_RECEIVED; 732 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 733 dropsocket = 0; /* committed to socket */ 734 tcpstat.tcps_accepts++; 735 goto trimthenstep6; 736 } 737 738 /* 739 * If the state is SYN_SENT: 740 * if seg contains an ACK, but not for our SYN, drop the input. 741 * if seg contains a RST, then drop the connection. 742 * if seg does not contain SYN, then drop it. 743 * Otherwise this is an acceptable SYN segment 744 * initialize tp->rcv_nxt and tp->irs 745 * if seg contains ack then advance tp->snd_una 746 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 747 * arrange for segment to be acked (eventually) 748 * continue processing rest of data/controls, beginning with URG 749 */ 750 case TCPS_SYN_SENT: 751 if ((taop = tcp_gettaocache(inp)) == NULL) { 752 taop = &tao_noncached; 753 bzero(taop, sizeof(*taop)); 754 } 755 756 if ((tiflags & TH_ACK) && 757 (SEQ_LEQ(ti->ti_ack, tp->iss) || 758 SEQ_GT(ti->ti_ack, tp->snd_max))) { 759 /* 760 * If we have a cached CCsent for the remote host, 761 * hence we haven't just crashed and restarted, 762 * do not send a RST. This may be a retransmission 763 * from the other side after our earlier ACK was lost. 764 * Our new SYN, when it arrives, will serve as the 765 * needed ACK. 766 */ 767 if (taop->tao_ccsent != 0) 768 goto drop; 769 else 770 goto dropwithreset; 771 } 772 if (tiflags & TH_RST) { 773 if (tiflags & TH_ACK) 774 tp = tcp_drop(tp, ECONNREFUSED); 775 goto drop; 776 } 777 if ((tiflags & TH_SYN) == 0) 778 goto drop; 779 tp->snd_wnd = ti->ti_win; /* initial send window */ 780 tp->cc_recv = to.to_cc; /* foreign CC */ 781 782 tp->irs = ti->ti_seq; 783 tcp_rcvseqinit(tp); 784 if (tiflags & TH_ACK) { 785 /* 786 * Our SYN was acked. If segment contains CC.ECHO 787 * option, check it to make sure this segment really 788 * matches our SYN. If not, just drop it as old 789 * duplicate, but send an RST if we're still playing 790 * by the old rules. 791 */ 792 if ((to.to_flag & TOF_CCECHO) && 793 tp->cc_send != to.to_ccecho) { 794 if (taop->tao_ccsent != 0) 795 goto drop; 796 else 797 goto dropwithreset; 798 } 799 tcpstat.tcps_connects++; 800 soisconnected(so); 801 /* Do window scaling on this connection? */ 802 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 803 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 804 tp->snd_scale = tp->requested_s_scale; 805 tp->rcv_scale = tp->request_r_scale; 806 } 807 /* Segment is acceptable, update cache if undefined. */ 808 if (taop->tao_ccsent == 0) 809 taop->tao_ccsent = to.to_ccecho; 810 811 tp->rcv_adv += tp->rcv_wnd; 812 tp->snd_una++; /* SYN is acked */ 813 /* 814 * If there's data, delay ACK; if there's also a FIN 815 * ACKNOW will be turned on later. 816 */ 817 if (ti->ti_len != 0) 818 tp->t_flags |= TF_DELACK; 819 else 820 tp->t_flags |= TF_ACKNOW; 821 /* 822 * Received <SYN,ACK> in SYN_SENT[*] state. 823 * Transitions: 824 * SYN_SENT --> ESTABLISHED 825 * SYN_SENT* --> FIN_WAIT_1 826 */ 827 if (tp->t_flags & TF_NEEDFIN) { 828 tp->t_state = TCPS_FIN_WAIT_1; 829 tp->t_flags &= ~TF_NEEDFIN; 830 tiflags &= ~TH_SYN; 831 } else 832 tp->t_state = TCPS_ESTABLISHED; 833 834 } else { 835 /* 836 * Received initial SYN in SYN-SENT[*] state => simul- 837 * taneous open. If segment contains CC option and there is 838 * a cached CC, apply TAO test; if it succeeds, connection is 839 * half-synchronized. Otherwise, do 3-way handshake: 840 * SYN-SENT -> SYN-RECEIVED 841 * SYN-SENT* -> SYN-RECEIVED* 842 * If there was no CC option, clear cached CC value. 843 */ 844 tp->t_flags |= TF_ACKNOW; 845 tp->t_timer[TCPT_REXMT] = 0; 846 if (to.to_flag & TOF_CC) { 847 if (taop->tao_cc != 0 && 848 CC_GT(to.to_cc, taop->tao_cc)) { 849 /* 850 * update cache and make transition: 851 * SYN-SENT -> ESTABLISHED* 852 * SYN-SENT* -> FIN-WAIT-1* 853 */ 854 taop->tao_cc = to.to_cc; 855 if (tp->t_flags & TF_NEEDFIN) { 856 tp->t_state = TCPS_FIN_WAIT_1; 857 tp->t_flags &= ~TF_NEEDFIN; 858 } else 859 tp->t_state = TCPS_ESTABLISHED; 860 tp->t_flags |= TF_NEEDSYN; 861 } else 862 tp->t_state = TCPS_SYN_RECEIVED; 863 } else { 864 /* CC.NEW or no option => invalidate cache */ 865 taop->tao_cc = 0; 866 tp->t_state = TCPS_SYN_RECEIVED; 867 } 868 } 869 870trimthenstep6: 871 /* 872 * Advance ti->ti_seq to correspond to first data byte. 873 * If data, trim to stay within window, 874 * dropping FIN if necessary. 875 */ 876 ti->ti_seq++; 877 if (ti->ti_len > tp->rcv_wnd) { 878 todrop = ti->ti_len - tp->rcv_wnd; 879 m_adj(m, -todrop); 880 ti->ti_len = tp->rcv_wnd; 881 tiflags &= ~TH_FIN; 882 tcpstat.tcps_rcvpackafterwin++; 883 tcpstat.tcps_rcvbyteafterwin += todrop; 884 } 885 tp->snd_wl1 = ti->ti_seq - 1; 886 tp->rcv_up = ti->ti_seq; 887 /* 888 * Client side of transaction: already sent SYN and data. 889 * If the remote host used T/TCP to validate the SYN, 890 * our data will be ACK'd; if so, enter normal data segment 891 * processing in the middle of step 5, ack processing. 892 * Otherwise, goto step 6. 893 */ 894 if (tiflags & TH_ACK) 895 goto process_ACK; 896 goto step6; 897 /* 898 * If the state is LAST_ACK or CLOSING or TIME_WAIT: 899 * if segment contains a SYN and CC [not CC.NEW] option: 900 * if state == TIME_WAIT and connection duration > MSL, 901 * drop packet and send RST; 902 * 903 * if SEG.CC > CCrecv then is new SYN, and can implicitly 904 * ack the FIN (and data) in retransmission queue. 905 * Complete close and delete TCPCB. Then reprocess 906 * segment, hoping to find new TCPCB in LISTEN state; 907 * 908 * else must be old SYN; drop it. 909 * else do normal processing. 910 */ 911 case TCPS_LAST_ACK: 912 case TCPS_CLOSING: 913 case TCPS_TIME_WAIT: 914 if ((tiflags & TH_SYN) && 915 (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 916 if (tp->t_state == TCPS_TIME_WAIT && 917 tp->t_duration > TCPTV_MSL) 918 goto dropwithreset; 919 if (CC_GT(to.to_cc, tp->cc_recv)) { 920 tp = tcp_close(tp); 921 goto findpcb; 922 } 923 else 924 goto drop; 925 } 926 break; /* continue normal processing */ 927 } 928 929 /* 930 * States other than LISTEN or SYN_SENT. 931 * First check timestamp, if present. 932 * Then check the connection count, if present. 933 * Then check that at least some bytes of segment are within 934 * receive window. If segment begins before rcv_nxt, 935 * drop leading data (and SYN); if nothing left, just ack. 936 * 937 * RFC 1323 PAWS: If we have a timestamp reply on this segment 938 * and it's less than ts_recent, drop it. 939 */ 940 if ((to.to_flag & TOF_TS) != 0 && (tiflags & TH_RST) == 0 && 941 tp->ts_recent && TSTMP_LT(to.to_tsval, tp->ts_recent)) { 942 943 /* Check to see if ts_recent is over 24 days old. */ 944 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 945 /* 946 * Invalidate ts_recent. If this segment updates 947 * ts_recent, the age will be reset later and ts_recent 948 * will get a valid value. If it does not, setting 949 * ts_recent to zero will at least satisfy the 950 * requirement that zero be placed in the timestamp 951 * echo reply when ts_recent isn't valid. The 952 * age isn't reset until we get a valid ts_recent 953 * because we don't want out-of-order segments to be 954 * dropped when ts_recent is old. 955 */ 956 tp->ts_recent = 0; 957 } else { 958 tcpstat.tcps_rcvduppack++; 959 tcpstat.tcps_rcvdupbyte += ti->ti_len; 960 tcpstat.tcps_pawsdrop++; 961 goto dropafterack; 962 } 963 } 964 965 /* 966 * T/TCP mechanism 967 * If T/TCP was negotiated and the segment doesn't have CC, 968 * or if it's CC is wrong then drop the segment. 969 * RST segments do not have to comply with this. 970 */ 971 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 972 ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc) && 973 (tiflags & TH_RST) == 0) 974 goto dropafterack; 975 976 todrop = tp->rcv_nxt - ti->ti_seq; 977 if (todrop > 0) { 978 if (tiflags & TH_SYN) { 979 tiflags &= ~TH_SYN; 980 ti->ti_seq++; 981 if (ti->ti_urp > 1) 982 ti->ti_urp--; 983 else 984 tiflags &= ~TH_URG; 985 todrop--; 986 } 987 /* 988 * Following if statement from Stevens, vol. 2, p. 960. 989 */ 990 if (todrop > ti->ti_len 991 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) { 992 /* 993 * Any valid FIN must be to the left of the window. 994 * At this point the FIN must be a duplicate or out 995 * of sequence; drop it. 996 */ 997 tiflags &= ~TH_FIN; 998 999 /* 1000 * Send an ACK to resynchronize and drop any data. 1001 * But keep on processing for RST or ACK. 1002 */ 1003 tp->t_flags |= TF_ACKNOW; 1004 todrop = ti->ti_len; 1005 tcpstat.tcps_rcvduppack++; 1006 tcpstat.tcps_rcvdupbyte += todrop; 1007 } else { 1008 tcpstat.tcps_rcvpartduppack++; 1009 tcpstat.tcps_rcvpartdupbyte += todrop; 1010 } 1011 m_adj(m, todrop); 1012 ti->ti_seq += todrop; 1013 ti->ti_len -= todrop; 1014 if (ti->ti_urp > todrop) 1015 ti->ti_urp -= todrop; 1016 else { 1017 tiflags &= ~TH_URG; 1018 ti->ti_urp = 0; 1019 } 1020 } 1021 1022 /* 1023 * If new data are received on a connection after the 1024 * user processes are gone, then RST the other end. 1025 */ 1026 if ((so->so_state & SS_NOFDREF) && 1027 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 1028 tp = tcp_close(tp); 1029 tcpstat.tcps_rcvafterclose++; 1030 goto dropwithreset; 1031 } 1032 1033 /* 1034 * If segment ends after window, drop trailing data 1035 * (and PUSH and FIN); if nothing left, just ACK. 1036 */ 1037 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 1038 if (todrop > 0) { 1039 tcpstat.tcps_rcvpackafterwin++; 1040 if (todrop >= ti->ti_len) { 1041 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 1042 /* 1043 * If a new connection request is received 1044 * while in TIME_WAIT, drop the old connection 1045 * and start over if the sequence numbers 1046 * are above the previous ones. 1047 */ 1048 if (tiflags & TH_SYN && 1049 tp->t_state == TCPS_TIME_WAIT && 1050 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 1051 iss = tp->rcv_nxt + TCP_ISSINCR; 1052 tp = tcp_close(tp); 1053 goto findpcb; 1054 } 1055 /* 1056 * If window is closed can only take segments at 1057 * window edge, and have to drop data and PUSH from 1058 * incoming segments. Continue processing, but 1059 * remember to ack. Otherwise, drop segment 1060 * and ack. 1061 */ 1062 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 1063 tp->t_flags |= TF_ACKNOW; 1064 tcpstat.tcps_rcvwinprobe++; 1065 } else 1066 goto dropafterack; 1067 } else 1068 tcpstat.tcps_rcvbyteafterwin += todrop; 1069 m_adj(m, -todrop); 1070 ti->ti_len -= todrop; 1071 tiflags &= ~(TH_PUSH|TH_FIN); 1072 } 1073 1074 /* 1075 * If last ACK falls within this segment's sequence numbers, 1076 * record its timestamp. 1077 * NOTE that the test is modified according to the latest 1078 * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1079 */ 1080 if ((to.to_flag & TOF_TS) != 0 && 1081 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 1082 tp->ts_recent_age = tcp_now; 1083 tp->ts_recent = to.to_tsval; 1084 } 1085 1086 /* 1087 * If the RST bit is set examine the state: 1088 * SYN_RECEIVED STATE: 1089 * If passive open, return to LISTEN state. 1090 * If active open, inform user that connection was refused. 1091 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 1092 * Inform user that connection was reset, and close tcb. 1093 * CLOSING, LAST_ACK, TIME_WAIT STATES 1094 * Close the tcb. 1095 */ 1096 if (tiflags&TH_RST) switch (tp->t_state) { 1097 1098 case TCPS_SYN_RECEIVED: 1099 so->so_error = ECONNREFUSED; 1100 goto close; 1101 1102 case TCPS_ESTABLISHED: 1103 case TCPS_FIN_WAIT_1: 1104 case TCPS_FIN_WAIT_2: 1105 case TCPS_CLOSE_WAIT: 1106 so->so_error = ECONNRESET; 1107 close: 1108 tp->t_state = TCPS_CLOSED; 1109 tcpstat.tcps_drops++; 1110 tp = tcp_close(tp); 1111 goto drop; 1112 1113 case TCPS_CLOSING: 1114 case TCPS_LAST_ACK: 1115 case TCPS_TIME_WAIT: 1116 tp = tcp_close(tp); 1117 goto drop; 1118 } 1119 1120 /* 1121 * If a SYN is in the window, then this is an 1122 * error and we send an RST and drop the connection. 1123 */ 1124 if (tiflags & TH_SYN) { 1125 tp = tcp_drop(tp, ECONNRESET); 1126 goto dropwithreset; 1127 } 1128 1129 /* 1130 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1131 * flag is on (half-synchronized state), then queue data for 1132 * later processing; else drop segment and return. 1133 */ 1134 if ((tiflags & TH_ACK) == 0) { 1135 if (tp->t_state == TCPS_SYN_RECEIVED || 1136 (tp->t_flags & TF_NEEDSYN)) 1137 goto step6; 1138 else 1139 goto drop; 1140 } 1141 1142 /* 1143 * Ack processing. 1144 */ 1145 switch (tp->t_state) { 1146 1147 /* 1148 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 1149 * ESTABLISHED state and continue processing, otherwise 1150 * send an RST. 1151 */ 1152 case TCPS_SYN_RECEIVED: 1153 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 1154 SEQ_GT(ti->ti_ack, tp->snd_max)) 1155 goto dropwithreset; 1156 1157 tcpstat.tcps_connects++; 1158 soisconnected(so); 1159 /* Do window scaling? */ 1160 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1161 (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1162 tp->snd_scale = tp->requested_s_scale; 1163 tp->rcv_scale = tp->request_r_scale; 1164 } 1165 /* 1166 * Upon successful completion of 3-way handshake, 1167 * update cache.CC if it was undefined, pass any queued 1168 * data to the user, and advance state appropriately. 1169 */ 1170 if ((taop = tcp_gettaocache(inp)) != NULL && 1171 taop->tao_cc == 0) 1172 taop->tao_cc = tp->cc_recv; 1173 1174 /* 1175 * Make transitions: 1176 * SYN-RECEIVED -> ESTABLISHED 1177 * SYN-RECEIVED* -> FIN-WAIT-1 1178 */ 1179 if (tp->t_flags & TF_NEEDFIN) { 1180 tp->t_state = TCPS_FIN_WAIT_1; 1181 tp->t_flags &= ~TF_NEEDFIN; 1182 } else 1183 tp->t_state = TCPS_ESTABLISHED; 1184 /* 1185 * If segment contains data or ACK, will call tcp_reass() 1186 * later; if not, do so now to pass queued data to user. 1187 */ 1188 if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0) 1189 (void) tcp_reass(tp, (struct tcpiphdr *)0, 1190 (struct mbuf *)0); 1191 tp->snd_wl1 = ti->ti_seq - 1; 1192 /* fall into ... */ 1193 1194 /* 1195 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1196 * ACKs. If the ack is in the range 1197 * tp->snd_una < ti->ti_ack <= tp->snd_max 1198 * then advance tp->snd_una to ti->ti_ack and drop 1199 * data from the retransmission queue. If this ACK reflects 1200 * more up to date window information we update our window information. 1201 */ 1202 case TCPS_ESTABLISHED: 1203 case TCPS_FIN_WAIT_1: 1204 case TCPS_FIN_WAIT_2: 1205 case TCPS_CLOSE_WAIT: 1206 case TCPS_CLOSING: 1207 case TCPS_LAST_ACK: 1208 case TCPS_TIME_WAIT: 1209 1210 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 1211 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { 1212 tcpstat.tcps_rcvdupack++; 1213 /* 1214 * If we have outstanding data (other than 1215 * a window probe), this is a completely 1216 * duplicate ack (ie, window info didn't 1217 * change), the ack is the biggest we've 1218 * seen and we've seen exactly our rexmt 1219 * threshhold of them, assume a packet 1220 * has been dropped and retransmit it. 1221 * Kludge snd_nxt & the congestion 1222 * window so we send only this one 1223 * packet. 1224 * 1225 * We know we're losing at the current 1226 * window size so do congestion avoidance 1227 * (set ssthresh to half the current window 1228 * and pull our congestion window back to 1229 * the new ssthresh). 1230 * 1231 * Dup acks mean that packets have left the 1232 * network (they're now cached at the receiver) 1233 * so bump cwnd by the amount in the receiver 1234 * to keep a constant cwnd packets in the 1235 * network. 1236 */ 1237 if (tp->t_timer[TCPT_REXMT] == 0 || 1238 ti->ti_ack != tp->snd_una) 1239 tp->t_dupacks = 0; 1240 else if (++tp->t_dupacks == tcprexmtthresh) { 1241 tcp_seq onxt = tp->snd_nxt; 1242 u_int win = 1243 min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1244 tp->t_maxseg; 1245 1246 if (win < 2) 1247 win = 2; 1248 tp->snd_ssthresh = win * tp->t_maxseg; 1249 tp->t_timer[TCPT_REXMT] = 0; 1250 tp->t_rtt = 0; 1251 tp->snd_nxt = ti->ti_ack; 1252 tp->snd_cwnd = tp->t_maxseg; 1253 (void) tcp_output(tp); 1254 tp->snd_cwnd = tp->snd_ssthresh + 1255 tp->t_maxseg * tp->t_dupacks; 1256 if (SEQ_GT(onxt, tp->snd_nxt)) 1257 tp->snd_nxt = onxt; 1258 goto drop; 1259 } else if (tp->t_dupacks > tcprexmtthresh) { 1260 tp->snd_cwnd += tp->t_maxseg; 1261 (void) tcp_output(tp); 1262 goto drop; 1263 } 1264 } else 1265 tp->t_dupacks = 0; 1266 break; 1267 } 1268 /* 1269 * If the congestion window was inflated to account 1270 * for the other side's cached packets, retract it. 1271 */ 1272 if (tp->t_dupacks > tcprexmtthresh && 1273 tp->snd_cwnd > tp->snd_ssthresh) 1274 tp->snd_cwnd = tp->snd_ssthresh; 1275 tp->t_dupacks = 0; 1276 if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 1277 tcpstat.tcps_rcvacktoomuch++; 1278 goto dropafterack; 1279 } 1280 /* 1281 * If we reach this point, ACK is not a duplicate, 1282 * i.e., it ACKs something we sent. 1283 */ 1284 if (tp->t_flags & TF_NEEDSYN) { 1285 /* 1286 * T/TCP: Connection was half-synchronized, and our 1287 * SYN has been ACK'd (so connection is now fully 1288 * synchronized). Go to non-starred state and 1289 * increment snd_una for ACK of SYN. 1290 */ 1291 tp->t_flags &= ~TF_NEEDSYN; 1292 tp->snd_una++; 1293 } 1294 1295process_ACK: 1296 acked = ti->ti_ack - tp->snd_una; 1297 tcpstat.tcps_rcvackpack++; 1298 tcpstat.tcps_rcvackbyte += acked; 1299 1300 /* 1301 * If we have a timestamp reply, update smoothed 1302 * round trip time. If no timestamp is present but 1303 * transmit timer is running and timed sequence 1304 * number was acked, update smoothed round trip time. 1305 * Since we now have an rtt measurement, cancel the 1306 * timer backoff (cf., Phil Karn's retransmit alg.). 1307 * Recompute the initial retransmit timer. 1308 */ 1309 if (to.to_flag & TOF_TS) 1310 tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1); 1311 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 1312 tcp_xmit_timer(tp,tp->t_rtt); 1313 1314 /* 1315 * If all outstanding data is acked, stop retransmit 1316 * timer and remember to restart (more output or persist). 1317 * If there is more data to be acked, restart retransmit 1318 * timer, using current (possibly backed-off) value. 1319 */ 1320 if (ti->ti_ack == tp->snd_max) { 1321 tp->t_timer[TCPT_REXMT] = 0; 1322 needoutput = 1; 1323 } else if (tp->t_timer[TCPT_PERSIST] == 0) 1324 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 1325 1326 /* 1327 * If no data (only SYN) was ACK'd, 1328 * skip rest of ACK processing. 1329 */ 1330 if (acked == 0) 1331 goto step6; 1332 1333 /* 1334 * When new data is acked, open the congestion window. 1335 * If the window gives us less than ssthresh packets 1336 * in flight, open exponentially (maxseg per packet). 1337 * Otherwise open linearly: maxseg per window 1338 * (maxseg^2 / cwnd per packet). 1339 */ 1340 { 1341 register u_int cw = tp->snd_cwnd; 1342 register u_int incr = tp->t_maxseg; 1343 1344 if (cw > tp->snd_ssthresh) 1345 incr = incr * incr / cw; 1346 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); 1347 } 1348 if (acked > so->so_snd.sb_cc) { 1349 tp->snd_wnd -= so->so_snd.sb_cc; 1350 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1351 ourfinisacked = 1; 1352 } else { 1353 sbdrop(&so->so_snd, acked); 1354 tp->snd_wnd -= acked; 1355 ourfinisacked = 0; 1356 } 1357 if (so->so_snd.sb_flags & SB_NOTIFY) 1358 sowwakeup(so); 1359 tp->snd_una = ti->ti_ack; 1360 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1361 tp->snd_nxt = tp->snd_una; 1362 1363 switch (tp->t_state) { 1364 1365 /* 1366 * In FIN_WAIT_1 STATE in addition to the processing 1367 * for the ESTABLISHED state if our FIN is now acknowledged 1368 * then enter FIN_WAIT_2. 1369 */ 1370 case TCPS_FIN_WAIT_1: 1371 if (ourfinisacked) { 1372 /* 1373 * If we can't receive any more 1374 * data, then closing user can proceed. 1375 * Starting the timer is contrary to the 1376 * specification, but if we don't get a FIN 1377 * we'll hang forever. 1378 */ 1379 if (so->so_state & SS_CANTRCVMORE) { 1380 soisdisconnected(so); 1381 tp->t_timer[TCPT_2MSL] = tcp_maxidle; 1382 } 1383 tp->t_state = TCPS_FIN_WAIT_2; 1384 } 1385 break; 1386 1387 /* 1388 * In CLOSING STATE in addition to the processing for 1389 * the ESTABLISHED state if the ACK acknowledges our FIN 1390 * then enter the TIME-WAIT state, otherwise ignore 1391 * the segment. 1392 */ 1393 case TCPS_CLOSING: 1394 if (ourfinisacked) { 1395 tp->t_state = TCPS_TIME_WAIT; 1396 tcp_canceltimers(tp); 1397 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1398 if (tp->cc_recv != 0 && 1399 tp->t_duration < TCPTV_MSL) 1400 tp->t_timer[TCPT_2MSL] = 1401 tp->t_rxtcur * TCPTV_TWTRUNC; 1402 else 1403 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1404 soisdisconnected(so); 1405 } 1406 break; 1407 1408 /* 1409 * In LAST_ACK, we may still be waiting for data to drain 1410 * and/or to be acked, as well as for the ack of our FIN. 1411 * If our FIN is now acknowledged, delete the TCB, 1412 * enter the closed state and return. 1413 */ 1414 case TCPS_LAST_ACK: 1415 if (ourfinisacked) { 1416 tp = tcp_close(tp); 1417 goto drop; 1418 } 1419 break; 1420 1421 /* 1422 * In TIME_WAIT state the only thing that should arrive 1423 * is a retransmission of the remote FIN. Acknowledge 1424 * it and restart the finack timer. 1425 */ 1426 case TCPS_TIME_WAIT: 1427 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1428 goto dropafterack; 1429 } 1430 } 1431 1432step6: 1433 /* 1434 * Update window information. 1435 * Don't look at window if no ACK: TAC's send garbage on first SYN. 1436 */ 1437 if ((tiflags & TH_ACK) && 1438 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 1439 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 1440 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) { 1441 /* keep track of pure window updates */ 1442 if (ti->ti_len == 0 && 1443 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) 1444 tcpstat.tcps_rcvwinupd++; 1445 tp->snd_wnd = tiwin; 1446 tp->snd_wl1 = ti->ti_seq; 1447 tp->snd_wl2 = ti->ti_ack; 1448 if (tp->snd_wnd > tp->max_sndwnd) 1449 tp->max_sndwnd = tp->snd_wnd; 1450 needoutput = 1; 1451 } 1452 1453 /* 1454 * Process segments with URG. 1455 */ 1456 if ((tiflags & TH_URG) && ti->ti_urp && 1457 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1458 /* 1459 * This is a kludge, but if we receive and accept 1460 * random urgent pointers, we'll crash in 1461 * soreceive. It's hard to imagine someone 1462 * actually wanting to send this much urgent data. 1463 */ 1464 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) { 1465 ti->ti_urp = 0; /* XXX */ 1466 tiflags &= ~TH_URG; /* XXX */ 1467 goto dodata; /* XXX */ 1468 } 1469 /* 1470 * If this segment advances the known urgent pointer, 1471 * then mark the data stream. This should not happen 1472 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1473 * a FIN has been received from the remote side. 1474 * In these states we ignore the URG. 1475 * 1476 * According to RFC961 (Assigned Protocols), 1477 * the urgent pointer points to the last octet 1478 * of urgent data. We continue, however, 1479 * to consider it to indicate the first octet 1480 * of data past the urgent section as the original 1481 * spec states (in one of two places). 1482 */ 1483 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 1484 tp->rcv_up = ti->ti_seq + ti->ti_urp; 1485 so->so_oobmark = so->so_rcv.sb_cc + 1486 (tp->rcv_up - tp->rcv_nxt) - 1; 1487 if (so->so_oobmark == 0) 1488 so->so_state |= SS_RCVATMARK; 1489 sohasoutofband(so); 1490 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1491 } 1492 /* 1493 * Remove out of band data so doesn't get presented to user. 1494 * This can happen independent of advancing the URG pointer, 1495 * but if two URG's are pending at once, some out-of-band 1496 * data may creep in... ick. 1497 */ 1498 if (ti->ti_urp <= (u_long)ti->ti_len 1499#ifdef SO_OOBINLINE 1500 && (so->so_options & SO_OOBINLINE) == 0 1501#endif 1502 ) 1503 tcp_pulloutofband(so, ti, m); 1504 } else 1505 /* 1506 * If no out of band data is expected, 1507 * pull receive urgent pointer along 1508 * with the receive window. 1509 */ 1510 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1511 tp->rcv_up = tp->rcv_nxt; 1512dodata: /* XXX */ 1513 1514 /* 1515 * Process the segment text, merging it into the TCP sequencing queue, 1516 * and arranging for acknowledgment of receipt if necessary. 1517 * This process logically involves adjusting tp->rcv_wnd as data 1518 * is presented to the user (this happens in tcp_usrreq.c, 1519 * case PRU_RCVD). If a FIN has already been received on this 1520 * connection then we just ignore the text. 1521 */ 1522 if ((ti->ti_len || (tiflags&TH_FIN)) && 1523 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1524 TCP_REASS(tp, ti, m, so, tiflags); 1525 /* 1526 * Note the amount of data that peer has sent into 1527 * our window, in order to estimate the sender's 1528 * buffer size. 1529 */ 1530 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1531 } else { 1532 m_freem(m); 1533 tiflags &= ~TH_FIN; 1534 } 1535 1536 /* 1537 * If FIN is received ACK the FIN and let the user know 1538 * that the connection is closing. 1539 */ 1540 if (tiflags & TH_FIN) { 1541 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1542 socantrcvmore(so); 1543 /* 1544 * If connection is half-synchronized 1545 * (ie NEEDSYN flag on) then delay ACK, 1546 * so it may be piggybacked when SYN is sent. 1547 * Otherwise, since we received a FIN then no 1548 * more input can be expected, send ACK now. 1549 */ 1550 if (tp->t_flags & TF_NEEDSYN) 1551 tp->t_flags |= TF_DELACK; 1552 else 1553 tp->t_flags |= TF_ACKNOW; 1554 tp->rcv_nxt++; 1555 } 1556 switch (tp->t_state) { 1557 1558 /* 1559 * In SYN_RECEIVED and ESTABLISHED STATES 1560 * enter the CLOSE_WAIT state. 1561 */ 1562 case TCPS_SYN_RECEIVED: 1563 case TCPS_ESTABLISHED: 1564 tp->t_state = TCPS_CLOSE_WAIT; 1565 break; 1566 1567 /* 1568 * If still in FIN_WAIT_1 STATE FIN has not been acked so 1569 * enter the CLOSING state. 1570 */ 1571 case TCPS_FIN_WAIT_1: 1572 tp->t_state = TCPS_CLOSING; 1573 break; 1574 1575 /* 1576 * In FIN_WAIT_2 state enter the TIME_WAIT state, 1577 * starting the time-wait timer, turning off the other 1578 * standard timers. 1579 */ 1580 case TCPS_FIN_WAIT_2: 1581 tp->t_state = TCPS_TIME_WAIT; 1582 tcp_canceltimers(tp); 1583 /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1584 if (tp->cc_recv != 0 && 1585 tp->t_duration < TCPTV_MSL) { 1586 tp->t_timer[TCPT_2MSL] = 1587 tp->t_rxtcur * TCPTV_TWTRUNC; 1588 /* For transaction client, force ACK now. */ 1589 tp->t_flags |= TF_ACKNOW; 1590 } 1591 else 1592 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1593 soisdisconnected(so); 1594 break; 1595 1596 /* 1597 * In TIME_WAIT state restart the 2 MSL time_wait timer. 1598 */ 1599 case TCPS_TIME_WAIT: 1600 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1601 break; 1602 } 1603 } 1604#ifdef TCPDEBUG 1605 if (so->so_options & SO_DEBUG) 1606 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 1607#endif 1608 1609 /* 1610 * Return any desired output. 1611 */ 1612 if (needoutput || (tp->t_flags & TF_ACKNOW)) 1613 (void) tcp_output(tp); 1614 return; 1615 1616dropafterack: 1617 /* 1618 * Generate an ACK dropping incoming segment if it occupies 1619 * sequence space, where the ACK reflects our state. 1620 */ 1621 if (tiflags & TH_RST) 1622 goto drop; 1623#ifdef TCPDEBUG 1624 if (so->so_options & SO_DEBUG) 1625 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1626#endif 1627 m_freem(m); 1628 tp->t_flags |= TF_ACKNOW; 1629 (void) tcp_output(tp); 1630 return; 1631 1632dropwithreset: 1633 /* 1634 * Generate a RST, dropping incoming segment. 1635 * Make ACK acceptable to originator of segment. 1636 * Don't bother to respond if destination was broadcast/multicast. 1637 */ 1638 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) || 1639 IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 1640 goto drop; 1641#ifdef TCPDEBUG 1642 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1643 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1644#endif 1645 if (tiflags & TH_ACK) 1646 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 1647 else { 1648 if (tiflags & TH_SYN) 1649 ti->ti_len++; 1650 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 1651 TH_RST|TH_ACK); 1652 } 1653 /* destroy temporarily created socket */ 1654 if (dropsocket) 1655 (void) soabort(so); 1656 return; 1657 1658drop: 1659 /* 1660 * Drop space held by incoming segment and return. 1661 */ 1662#ifdef TCPDEBUG 1663 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1664 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1665#endif 1666 m_freem(m); 1667 /* destroy temporarily created socket */ 1668 if (dropsocket) 1669 (void) soabort(so); 1670 return; 1671#ifndef TUBA_INCLUDE 1672} 1673 1674static void 1675tcp_dooptions(tp, cp, cnt, ti, to) 1676 struct tcpcb *tp; 1677 u_char *cp; 1678 int cnt; 1679 struct tcpiphdr *ti; 1680 struct tcpopt *to; 1681{ 1682 u_short mss = 0; 1683 int opt, optlen; 1684 1685 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1686 opt = cp[0]; 1687 if (opt == TCPOPT_EOL) 1688 break; 1689 if (opt == TCPOPT_NOP) 1690 optlen = 1; 1691 else { 1692 optlen = cp[1]; 1693 if (optlen <= 0) 1694 break; 1695 } 1696 switch (opt) { 1697 1698 default: 1699 continue; 1700 1701 case TCPOPT_MAXSEG: 1702 if (optlen != TCPOLEN_MAXSEG) 1703 continue; 1704 if (!(ti->ti_flags & TH_SYN)) 1705 continue; 1706 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 1707 NTOHS(mss); 1708 break; 1709 1710 case TCPOPT_WINDOW: 1711 if (optlen != TCPOLEN_WINDOW) 1712 continue; 1713 if (!(ti->ti_flags & TH_SYN)) 1714 continue; 1715 tp->t_flags |= TF_RCVD_SCALE; 1716 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 1717 break; 1718 1719 case TCPOPT_TIMESTAMP: 1720 if (optlen != TCPOLEN_TIMESTAMP) 1721 continue; 1722 to->to_flag |= TOF_TS; 1723 bcopy((char *)cp + 2, 1724 (char *)&to->to_tsval, sizeof(to->to_tsval)); 1725 NTOHL(to->to_tsval); 1726 bcopy((char *)cp + 6, 1727 (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 1728 NTOHL(to->to_tsecr); 1729 1730 /* 1731 * A timestamp received in a SYN makes 1732 * it ok to send timestamp requests and replies. 1733 */ 1734 if (ti->ti_flags & TH_SYN) { 1735 tp->t_flags |= TF_RCVD_TSTMP; 1736 tp->ts_recent = to->to_tsval; 1737 tp->ts_recent_age = tcp_now; 1738 } 1739 break; 1740 case TCPOPT_CC: 1741 if (optlen != TCPOLEN_CC) 1742 continue; 1743 to->to_flag |= TOF_CC; 1744 bcopy((char *)cp + 2, 1745 (char *)&to->to_cc, sizeof(to->to_cc)); 1746 NTOHL(to->to_cc); 1747 /* 1748 * A CC or CC.new option received in a SYN makes 1749 * it ok to send CC in subsequent segments. 1750 */ 1751 if (ti->ti_flags & TH_SYN) 1752 tp->t_flags |= TF_RCVD_CC; 1753 break; 1754 case TCPOPT_CCNEW: 1755 if (optlen != TCPOLEN_CC) 1756 continue; 1757 if (!(ti->ti_flags & TH_SYN)) 1758 continue; 1759 to->to_flag |= TOF_CCNEW; 1760 bcopy((char *)cp + 2, 1761 (char *)&to->to_cc, sizeof(to->to_cc)); 1762 NTOHL(to->to_cc); 1763 /* 1764 * A CC or CC.new option received in a SYN makes 1765 * it ok to send CC in subsequent segments. 1766 */ 1767 tp->t_flags |= TF_RCVD_CC; 1768 break; 1769 case TCPOPT_CCECHO: 1770 if (optlen != TCPOLEN_CC) 1771 continue; 1772 if (!(ti->ti_flags & TH_SYN)) 1773 continue; 1774 to->to_flag |= TOF_CCECHO; 1775 bcopy((char *)cp + 2, 1776 (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 1777 NTOHL(to->to_ccecho); 1778 break; 1779 } 1780 } 1781 if (ti->ti_flags & TH_SYN) 1782 tcp_mss(tp, mss); /* sets t_maxseg */ 1783} 1784 1785/* 1786 * Pull out of band byte out of a segment so 1787 * it doesn't appear in the user's data queue. 1788 * It is still reflected in the segment length for 1789 * sequencing purposes. 1790 */ 1791static void 1792tcp_pulloutofband(so, ti, m) 1793 struct socket *so; 1794 struct tcpiphdr *ti; 1795 register struct mbuf *m; 1796{ 1797 int cnt = ti->ti_urp - 1; 1798 1799 while (cnt >= 0) { 1800 if (m->m_len > cnt) { 1801 char *cp = mtod(m, caddr_t) + cnt; 1802 struct tcpcb *tp = sototcpcb(so); 1803 1804 tp->t_iobc = *cp; 1805 tp->t_oobflags |= TCPOOB_HAVEDATA; 1806 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 1807 m->m_len--; 1808 return; 1809 } 1810 cnt -= m->m_len; 1811 m = m->m_next; 1812 if (m == 0) 1813 break; 1814 } 1815 panic("tcp_pulloutofband"); 1816} 1817 1818/* 1819 * Collect new round-trip time estimate 1820 * and update averages and current timeout. 1821 */ 1822static void 1823tcp_xmit_timer(tp, rtt) 1824 register struct tcpcb *tp; 1825 short rtt; 1826{ 1827 register short delta; 1828 1829 tcpstat.tcps_rttupdated++; 1830 tp->t_rttupdated++; 1831 if (tp->t_srtt != 0) { 1832 /* 1833 * srtt is stored as fixed point with 3 bits after the 1834 * binary point (i.e., scaled by 8). The following magic 1835 * is equivalent to the smoothing algorithm in rfc793 with 1836 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 1837 * point). Adjust rtt to origin 0. 1838 */ 1839 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT); 1840 if ((tp->t_srtt += delta) <= 0) 1841 tp->t_srtt = 1; 1842 /* 1843 * We accumulate a smoothed rtt variance (actually, a 1844 * smoothed mean difference), then set the retransmit 1845 * timer to smoothed rtt + 4 times the smoothed variance. 1846 * rttvar is stored as fixed point with 2 bits after the 1847 * binary point (scaled by 4). The following is 1848 * equivalent to rfc793 smoothing with an alpha of .75 1849 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 1850 * rfc793's wired-in beta. 1851 */ 1852 if (delta < 0) 1853 delta = -delta; 1854 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 1855 if ((tp->t_rttvar += delta) <= 0) 1856 tp->t_rttvar = 1; 1857 } else { 1858 /* 1859 * No rtt measurement yet - use the unsmoothed rtt. 1860 * Set the variance to half the rtt (so our first 1861 * retransmit happens at 3*rtt). 1862 */ 1863 tp->t_srtt = rtt << TCP_RTT_SHIFT; 1864 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 1865 } 1866 tp->t_rtt = 0; 1867 tp->t_rxtshift = 0; 1868 1869 /* 1870 * the retransmit should happen at rtt + 4 * rttvar. 1871 * Because of the way we do the smoothing, srtt and rttvar 1872 * will each average +1/2 tick of bias. When we compute 1873 * the retransmit timer, we want 1/2 tick of rounding and 1874 * 1 extra tick because of +-1/2 tick uncertainty in the 1875 * firing of the timer. The bias will give us exactly the 1876 * 1.5 tick we need. But, because the bias is 1877 * statistical, we have to test that we don't drop below 1878 * the minimum feasible timer (which is 2 ticks). 1879 */ 1880 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 1881 tp->t_rttmin, TCPTV_REXMTMAX); 1882 1883 /* 1884 * We received an ack for a packet that wasn't retransmitted; 1885 * it is probably safe to discard any error indications we've 1886 * received recently. This isn't quite right, but close enough 1887 * for now (a route might have failed after we sent a segment, 1888 * and the return path might not be symmetrical). 1889 */ 1890 tp->t_softerror = 0; 1891} 1892 1893/* 1894 * Determine a reasonable value for maxseg size. 1895 * If the route is known, check route for mtu. 1896 * If none, use an mss that can be handled on the outgoing 1897 * interface without forcing IP to fragment; if bigger than 1898 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 1899 * to utilize large mbufs. If no route is found, route has no mtu, 1900 * or the destination isn't local, use a default, hopefully conservative 1901 * size (usually 512 or the default IP max size, but no more than the mtu 1902 * of the interface), as we can't discover anything about intervening 1903 * gateways or networks. We also initialize the congestion/slow start 1904 * window to be a single segment if the destination isn't local. 1905 * While looking at the routing entry, we also initialize other path-dependent 1906 * parameters from pre-set or cached values in the routing entry. 1907 * 1908 * Also take into account the space needed for options that we 1909 * send regularly. Make maxseg shorter by that amount to assure 1910 * that we can send maxseg amount of data even when the options 1911 * are present. Store the upper limit of the length of options plus 1912 * data in maxopd. 1913 * 1914 * NOTE that this routine is only called when we process an incoming 1915 * segment, for outgoing segments only tcp_mssopt is called. 1916 * 1917 * In case of T/TCP, we call this routine during implicit connection 1918 * setup as well (offer = -1), to initialize maxseg from the cached 1919 * MSS of our peer. 1920 */ 1921void 1922tcp_mss(tp, offer) 1923 struct tcpcb *tp; 1924 int offer; 1925{ 1926 register struct rtentry *rt; 1927 struct ifnet *ifp; 1928 register int rtt, mss; 1929 u_long bufsize; 1930 struct inpcb *inp; 1931 struct socket *so; 1932 struct rmxp_tao *taop; 1933 int origoffer = offer; 1934 1935 inp = tp->t_inpcb; 1936 if ((rt = tcp_rtlookup(inp)) == NULL) { 1937 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 1938 return; 1939 } 1940 ifp = rt->rt_ifp; 1941 so = inp->inp_socket; 1942 1943 taop = rmx_taop(rt->rt_rmx); 1944 /* 1945 * Offer == -1 means that we didn't receive SYN yet, 1946 * use cached value in that case; 1947 */ 1948 if (offer == -1) 1949 offer = taop->tao_mssopt; 1950 /* 1951 * Offer == 0 means that there was no MSS on the SYN segment, 1952 * in this case we use tcp_mssdflt. 1953 */ 1954 if (offer == 0) 1955 offer = tcp_mssdflt; 1956 else 1957 /* 1958 * Sanity check: make sure that maxopd will be large 1959 * enough to allow some data on segments even is the 1960 * all the option space is used (40bytes). Otherwise 1961 * funny things may happen in tcp_output. 1962 */ 1963 offer = max(offer, 64); 1964 taop->tao_mssopt = offer; 1965 1966 /* 1967 * While we're here, check if there's an initial rtt 1968 * or rttvar. Convert from the route-table units 1969 * to scaled multiples of the slow timeout timer. 1970 */ 1971 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 1972 /* 1973 * XXX the lock bit for RTT indicates that the value 1974 * is also a minimum value; this is subject to time. 1975 */ 1976 if (rt->rt_rmx.rmx_locks & RTV_RTT) 1977 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 1978 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 1979 tcpstat.tcps_usedrtt++; 1980 if (rt->rt_rmx.rmx_rttvar) { 1981 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 1982 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 1983 tcpstat.tcps_usedrttvar++; 1984 } else { 1985 /* default variation is +- 1 rtt */ 1986 tp->t_rttvar = 1987 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 1988 } 1989 TCPT_RANGESET(tp->t_rxtcur, 1990 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 1991 tp->t_rttmin, TCPTV_REXMTMAX); 1992 } 1993 /* 1994 * if there's an mtu associated with the route, use it 1995 */ 1996 if (rt->rt_rmx.rmx_mtu) 1997 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 1998 else 1999 { 2000 mss = ifp->if_mtu - sizeof(struct tcpiphdr); 2001 if (!in_localaddr(inp->inp_faddr)) 2002 mss = min(mss, tcp_mssdflt); 2003 } 2004 mss = min(mss, offer); 2005 /* 2006 * maxopd stores the maximum length of data AND options 2007 * in a segment; maxseg is the amount of data in a normal 2008 * segment. We need to store this value (maxopd) apart 2009 * from maxseg, because now every segment carries options 2010 * and thus we normally have somewhat less data in segments. 2011 */ 2012 tp->t_maxopd = mss; 2013 2014 /* 2015 * In case of T/TCP, origoffer==-1 indicates, that no segments 2016 * were received yet. In this case we just guess, otherwise 2017 * we do the same as before T/TCP. 2018 */ 2019 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2020 (origoffer == -1 || 2021 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2022 mss -= TCPOLEN_TSTAMP_APPA; 2023 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2024 (origoffer == -1 || 2025 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2026 mss -= TCPOLEN_CC_APPA; 2027 2028#if (MCLBYTES & (MCLBYTES - 1)) == 0 2029 if (mss > MCLBYTES) 2030 mss &= ~(MCLBYTES-1); 2031#else 2032 if (mss > MCLBYTES) 2033 mss = mss / MCLBYTES * MCLBYTES; 2034#endif 2035 /* 2036 * If there's a pipesize, change the socket buffer 2037 * to that size. Make the socket buffers an integral 2038 * number of mss units; if the mss is larger than 2039 * the socket buffer, decrease the mss. 2040 */ 2041#ifdef RTV_SPIPE 2042 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2043#endif 2044 bufsize = so->so_snd.sb_hiwat; 2045 if (bufsize < mss) 2046 mss = bufsize; 2047 else { 2048 bufsize = roundup(bufsize, mss); 2049 if (bufsize > sb_max) 2050 bufsize = sb_max; 2051 (void)sbreserve(&so->so_snd, bufsize); 2052 } 2053 tp->t_maxseg = mss; 2054 2055#ifdef RTV_RPIPE 2056 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2057#endif 2058 bufsize = so->so_rcv.sb_hiwat; 2059 if (bufsize > mss) { 2060 bufsize = roundup(bufsize, mss); 2061 if (bufsize > sb_max) 2062 bufsize = sb_max; 2063 (void)sbreserve(&so->so_rcv, bufsize); 2064 } 2065 /* 2066 * Don't force slow-start on local network. 2067 */ 2068 if (!in_localaddr(inp->inp_faddr)) 2069 tp->snd_cwnd = mss; 2070 2071 if (rt->rt_rmx.rmx_ssthresh) { 2072 /* 2073 * There's some sort of gateway or interface 2074 * buffer limit on the path. Use this to set 2075 * the slow start threshhold, but set the 2076 * threshold to no less than 2*mss. 2077 */ 2078 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2079 tcpstat.tcps_usedssthresh++; 2080 } 2081} 2082 2083/* 2084 * Determine the MSS option to send on an outgoing SYN. 2085 */ 2086int 2087tcp_mssopt(tp) 2088 struct tcpcb *tp; 2089{ 2090 struct rtentry *rt; 2091 2092 rt = tcp_rtlookup(tp->t_inpcb); 2093 if (rt == NULL) 2094 return tcp_mssdflt; 2095 2096 return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr); 2097} 2098#endif /* TUBA_INCLUDE */ 2099