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