1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)tcp_input.c 7.24 (Berkeley) 06/28/90 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "malloc.h" 13 #include "mbuf.h" 14 #include "protosw.h" 15 #include "socket.h" 16 #include "socketvar.h" 17 #include "errno.h" 18 19 #include "../net/if.h" 20 #include "../net/route.h" 21 22 #include "in.h" 23 #include "in_systm.h" 24 #include "ip.h" 25 #include "in_pcb.h" 26 #include "ip_var.h" 27 #include "tcp.h" 28 #include "tcp_fsm.h" 29 #include "tcp_seq.h" 30 #include "tcp_timer.h" 31 #include "tcp_var.h" 32 #include "tcpip.h" 33 #include "tcp_debug.h" 34 35 #define VAN 36 int tcprexmtthresh = 3; 37 int tcppredack; /* XXX debugging: times hdr predict ok for acks */ 38 int tcppreddat; /* XXX # times header prediction ok for data packets */ 39 int tcppcbcachemiss; 40 struct tcpiphdr tcp_saveti; 41 struct inpcb *tcp_last_inpcb = &tcb; 42 43 struct tcpcb *tcp_newtcpcb(); 44 45 /* 46 * Insert segment ti into reassembly queue of tcp with 47 * control block tp. Return TH_FIN if reassembly now includes 48 * a segment with FIN. The macro form does the common case inline 49 * (segment is the next to be received on an established connection, 50 * and the queue is empty), avoiding linkage into and removal 51 * from the queue and repetition of various conversions. 52 * Set DELACK for segments received in order, but ack immediately 53 * when segments are out of order (so fast retransmit can work). 54 */ 55 #define TCP_REASS(tp, ti, m, so, flags) { \ 56 if ((ti)->ti_seq == (tp)->rcv_nxt && \ 57 (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 58 (tp)->t_state == TCPS_ESTABLISHED) { \ 59 tp->t_flags |= TF_DELACK; \ 60 (tp)->rcv_nxt += (ti)->ti_len; \ 61 flags = (ti)->ti_flags & TH_FIN; \ 62 tcpstat.tcps_rcvpack++;\ 63 tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 64 sbappend(&(so)->so_rcv, (m)); \ 65 sorwakeup(so); \ 66 } else { \ 67 (flags) = tcp_reass((tp), (ti), (m)); \ 68 tp->t_flags |= TF_ACKNOW; \ 69 } \ 70 } 71 72 tcp_reass(tp, ti, m) 73 register struct tcpcb *tp; 74 register struct tcpiphdr *ti; 75 struct mbuf *m; 76 { 77 register struct tcpiphdr *q; 78 struct socket *so = tp->t_inpcb->inp_socket; 79 int flags; 80 81 /* 82 * Call with ti==0 after become established to 83 * force pre-ESTABLISHED data up to user socket. 84 */ 85 if (ti == 0) 86 goto present; 87 88 /* 89 * Find a segment which begins after this one does. 90 */ 91 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 92 q = (struct tcpiphdr *)q->ti_next) 93 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 94 break; 95 96 /* 97 * If there is a preceding segment, it may provide some of 98 * our data already. If so, drop the data from the incoming 99 * segment. If it provides all of our data, drop us. 100 */ 101 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 102 register int i; 103 q = (struct tcpiphdr *)q->ti_prev; 104 /* conversion to int (in i) handles seq wraparound */ 105 i = q->ti_seq + q->ti_len - ti->ti_seq; 106 if (i > 0) { 107 if (i >= ti->ti_len) { 108 tcpstat.tcps_rcvduppack++; 109 tcpstat.tcps_rcvdupbyte += ti->ti_len; 110 m_freem(m); 111 return (0); 112 } 113 m_adj(m, i); 114 ti->ti_len -= i; 115 ti->ti_seq += i; 116 } 117 q = (struct tcpiphdr *)(q->ti_next); 118 } 119 tcpstat.tcps_rcvoopack++; 120 tcpstat.tcps_rcvoobyte += ti->ti_len; 121 REASS_MBUF(ti) = m; /* XXX */ 122 123 /* 124 * While we overlap succeeding segments trim them or, 125 * if they are completely covered, dequeue them. 126 */ 127 while (q != (struct tcpiphdr *)tp) { 128 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 129 if (i <= 0) 130 break; 131 if (i < q->ti_len) { 132 q->ti_seq += i; 133 q->ti_len -= i; 134 m_adj(REASS_MBUF(q), i); 135 break; 136 } 137 q = (struct tcpiphdr *)q->ti_next; 138 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev); 139 remque(q->ti_prev); 140 m_freem(m); 141 } 142 143 /* 144 * Stick new segment in its place. 145 */ 146 insque(ti, q->ti_prev); 147 148 present: 149 /* 150 * Present data to user, advancing rcv_nxt through 151 * completed sequence space. 152 */ 153 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 154 return (0); 155 ti = tp->seg_next; 156 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 157 return (0); 158 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 159 return (0); 160 do { 161 tp->rcv_nxt += ti->ti_len; 162 flags = ti->ti_flags & TH_FIN; 163 remque(ti); 164 m = REASS_MBUF(ti); 165 ti = (struct tcpiphdr *)ti->ti_next; 166 if (so->so_state & SS_CANTRCVMORE) 167 m_freem(m); 168 else 169 sbappend(&so->so_rcv, m); 170 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 171 sorwakeup(so); 172 return (flags); 173 } 174 175 /* 176 * TCP input routine, follows pages 65-76 of the 177 * protocol specification dated September, 1981 very closely. 178 */ 179 tcp_input(m, iphlen) 180 register struct mbuf *m; 181 int iphlen; 182 { 183 register struct tcpiphdr *ti; 184 register struct inpcb *inp; 185 struct mbuf *om = 0; 186 int len, tlen, off; 187 register struct tcpcb *tp = 0; 188 register int tiflags; 189 struct socket *so; 190 int todrop, acked, ourfinisacked, needoutput = 0; 191 short ostate; 192 struct in_addr laddr; 193 int dropsocket = 0; 194 int iss = 0; 195 196 tcpstat.tcps_rcvtotal++; 197 /* 198 * Get IP and TCP header together in first mbuf. 199 * Note: IP leaves IP header in first mbuf. 200 */ 201 ti = mtod(m, struct tcpiphdr *); 202 if (iphlen > sizeof (struct ip)) 203 ip_stripoptions(m, (struct mbuf *)0); 204 if (m->m_len < sizeof (struct tcpiphdr)) { 205 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 206 tcpstat.tcps_rcvshort++; 207 return; 208 } 209 ti = mtod(m, struct tcpiphdr *); 210 } 211 212 /* 213 * Checksum extended TCP header and data. 214 */ 215 tlen = ((struct ip *)ti)->ip_len; 216 len = sizeof (struct ip) + tlen; 217 ti->ti_next = ti->ti_prev = 0; 218 ti->ti_x1 = 0; 219 ti->ti_len = (u_short)tlen; 220 HTONS(ti->ti_len); 221 if (ti->ti_sum = in_cksum(m, len)) { 222 tcpstat.tcps_rcvbadsum++; 223 goto drop; 224 } 225 226 /* 227 * Check that TCP offset makes sense, 228 * pull out TCP options and adjust length. XXX 229 */ 230 off = ti->ti_off << 2; 231 if (off < sizeof (struct tcphdr) || off > tlen) { 232 tcpstat.tcps_rcvbadoff++; 233 goto drop; 234 } 235 tlen -= off; 236 ti->ti_len = tlen; 237 if (off > sizeof (struct tcphdr)) { 238 if (m->m_len < sizeof(struct ip) + off) { 239 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 240 tcpstat.tcps_rcvshort++; 241 return; 242 } 243 ti = mtod(m, struct tcpiphdr *); 244 } 245 om = m_get(M_DONTWAIT, MT_DATA); 246 if (om == 0) 247 goto drop; 248 om->m_len = off - sizeof (struct tcphdr); 249 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 250 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 251 m->m_len -= om->m_len; 252 m->m_pkthdr.len -= om->m_len; 253 bcopy(op+om->m_len, op, 254 (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 255 } 256 } 257 tiflags = ti->ti_flags; 258 259 /* 260 * Convert TCP protocol specific fields to host format. 261 */ 262 NTOHL(ti->ti_seq); 263 NTOHL(ti->ti_ack); 264 NTOHS(ti->ti_win); 265 NTOHS(ti->ti_urp); 266 267 /* 268 * Locate pcb for segment. 269 */ 270 findpcb: 271 inp = tcp_last_inpcb; 272 if (inp->inp_lport != ti->ti_dport || 273 inp->inp_fport != ti->ti_sport || 274 inp->inp_faddr.s_addr != ti->ti_src.s_addr || 275 inp->inp_laddr.s_addr != ti->ti_dst.s_addr) { 276 inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport, 277 ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD); 278 if (inp) 279 tcp_last_inpcb = inp; 280 ++tcppcbcachemiss; 281 } 282 283 /* 284 * If the state is CLOSED (i.e., TCB does not exist) then 285 * all data in the incoming segment is discarded. 286 * If the TCB exists but is in CLOSED state, it is embryonic, 287 * but should either do a listen or a connect soon. 288 */ 289 if (inp == 0) 290 goto dropwithreset; 291 tp = intotcpcb(inp); 292 if (tp == 0) 293 goto dropwithreset; 294 if (tp->t_state == TCPS_CLOSED) 295 goto drop; 296 so = inp->inp_socket; 297 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 298 if (so->so_options & SO_DEBUG) { 299 ostate = tp->t_state; 300 tcp_saveti = *ti; 301 } 302 if (so->so_options & SO_ACCEPTCONN) { 303 so = sonewconn(so, 0); 304 if (so == 0) 305 goto drop; 306 /* 307 * This is ugly, but .... 308 * 309 * Mark socket as temporary until we're 310 * committed to keeping it. The code at 311 * ``drop'' and ``dropwithreset'' check the 312 * flag dropsocket to see if the temporary 313 * socket created here should be discarded. 314 * We mark the socket as discardable until 315 * we're committed to it below in TCPS_LISTEN. 316 */ 317 dropsocket++; 318 inp = (struct inpcb *)so->so_pcb; 319 inp->inp_laddr = ti->ti_dst; 320 inp->inp_lport = ti->ti_dport; 321 #if BSD>=43 322 inp->inp_options = ip_srcroute(); 323 #endif 324 tp = intotcpcb(inp); 325 tp->t_state = TCPS_LISTEN; 326 } 327 } 328 329 /* 330 * Segment received on connection. 331 * Reset idle time and keep-alive timer. 332 */ 333 tp->t_idle = 0; 334 tp->t_timer[TCPT_KEEP] = tcp_keepidle; 335 336 #ifndef VAN 337 /* 338 * Process options if not in LISTEN state, 339 * else do it below (after getting remote address). 340 */ 341 if (om && tp->t_state != TCPS_LISTEN) { 342 tcp_dooptions(tp, om, ti); 343 om = 0; 344 } 345 #endif VAN 346 /* 347 * Header prediction: check for the two common cases 348 * of a uni-directional data xfer. If the packet has 349 * no control flags, is in-sequence, the window didn't 350 * change and we're not retransmitting, it's a 351 * candidate. If the length is zero and the ack moved 352 * forward, we're the sender side of the xfer. Just 353 * free the data acked & wake any higher level process 354 * that was blocked waiting for space. If the length 355 * is non-zero and the ack didn't move, we're the 356 * receiver side. If we're getting packets in-order 357 * (the reassembly queue is empty), add the data to 358 * the socket buffer and note that we need a delayed ack. 359 */ 360 if (tp->t_state == TCPS_ESTABLISHED && 361 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 362 ti->ti_seq == tp->rcv_nxt && 363 ti->ti_win && ti->ti_win == tp->snd_wnd && 364 tp->snd_nxt == tp->snd_max) { 365 if (ti->ti_len == 0) { 366 if (SEQ_GT(ti->ti_ack, tp->snd_una) && 367 SEQ_LEQ(ti->ti_ack, tp->snd_max) && 368 tp->snd_cwnd >= tp->snd_wnd) { 369 /* 370 * this is a pure ack for outstanding data. 371 */ 372 ++tcppredack; 373 if (tp->t_rtt && SEQ_GT(ti->ti_ack,tp->t_rtseq)) 374 tcp_xmit_timer(tp); 375 acked = ti->ti_ack - tp->snd_una; 376 tcpstat.tcps_rcvackpack++; 377 tcpstat.tcps_rcvackbyte += acked; 378 sbdrop(&so->so_snd, acked); 379 tp->snd_una = ti->ti_ack; 380 m_freem(m); 381 382 /* 383 * If all outstanding data are acked, stop 384 * retransmit timer, otherwise restart timer 385 * using current (possibly backed-off) value. 386 * If process is waiting for space, 387 * wakeup/selwakeup/signal. If data 388 * are ready to send, let tcp_output 389 * decide between more output or persist. 390 */ 391 if (tp->snd_una == tp->snd_max) 392 tp->t_timer[TCPT_REXMT] = 0; 393 else if (tp->t_timer[TCPT_PERSIST] == 0) 394 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 395 396 if (so->so_snd.sb_flags & SB_NOTIFY) 397 sowwakeup(so); 398 if (so->so_snd.sb_cc) 399 (void) tcp_output(tp); 400 return; 401 } 402 } else if (ti->ti_ack == tp->snd_una && 403 tp->seg_next == (struct tcpiphdr *)tp && 404 ti->ti_len <= sbspace(&so->so_rcv)) { 405 /* 406 * this is a pure, in-sequence data packet 407 * with nothing on the reassembly queue and 408 * we have enough buffer space to take it. 409 */ 410 ++tcppreddat; 411 tp->rcv_nxt += ti->ti_len; 412 tcpstat.tcps_rcvpack++; 413 tcpstat.tcps_rcvbyte += ti->ti_len; 414 /* 415 * Drop TCP and IP headers then add data 416 * to socket buffer 417 */ 418 m->m_data += sizeof(struct tcpiphdr); 419 m->m_len -= sizeof(struct tcpiphdr); 420 sbappend(&so->so_rcv, m); 421 sorwakeup(so); 422 tp->t_flags |= TF_DELACK; 423 return; 424 } 425 } 426 427 /* 428 * Drop TCP and IP headers; TCP options were dropped above. 429 */ 430 m->m_data += sizeof(struct tcpiphdr); 431 m->m_len -= sizeof(struct tcpiphdr); 432 433 /* 434 * Calculate amount of space in receive window, 435 * and then do TCP input processing. 436 * Receive window is amount of space in rcv queue, 437 * but not less than advertised window. 438 */ 439 { int win; 440 441 win = sbspace(&so->so_rcv); 442 if (win < 0) 443 win = 0; 444 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 445 } 446 447 switch (tp->t_state) { 448 449 /* 450 * If the state is LISTEN then ignore segment if it contains an RST. 451 * If the segment contains an ACK then it is bad and send a RST. 452 * If it does not contain a SYN then it is not interesting; drop it. 453 * Don't bother responding if the destination was a broadcast. 454 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 455 * tp->iss, and send a segment: 456 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 457 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 458 * Fill in remote peer address fields if not previously specified. 459 * Enter SYN_RECEIVED state, and process any other fields of this 460 * segment in this state. 461 */ 462 case TCPS_LISTEN: { 463 struct mbuf *am; 464 register struct sockaddr_in *sin; 465 466 if (tiflags & TH_RST) 467 goto drop; 468 if (tiflags & TH_ACK) 469 goto dropwithreset; 470 if ((tiflags & TH_SYN) == 0) 471 goto drop; 472 if (m->m_flags & M_BCAST) 473 goto drop; 474 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 475 if (am == NULL) 476 goto drop; 477 am->m_len = sizeof (struct sockaddr_in); 478 sin = mtod(am, struct sockaddr_in *); 479 sin->sin_family = AF_INET; 480 sin->sin_len = sizeof(*sin); 481 sin->sin_addr = ti->ti_src; 482 sin->sin_port = ti->ti_sport; 483 laddr = inp->inp_laddr; 484 if (inp->inp_laddr.s_addr == INADDR_ANY) 485 inp->inp_laddr = ti->ti_dst; 486 if (in_pcbconnect(inp, am)) { 487 inp->inp_laddr = laddr; 488 (void) m_free(am); 489 goto drop; 490 } 491 (void) m_free(am); 492 tp->t_template = tcp_template(tp); 493 if (tp->t_template == 0) { 494 tp = tcp_drop(tp, ENOBUFS); 495 dropsocket = 0; /* socket is already gone */ 496 goto drop; 497 } 498 if (om) { 499 tcp_dooptions(tp, om, ti); 500 om = 0; 501 } 502 if (iss) 503 tp->iss = iss; 504 else 505 tp->iss = tcp_iss; 506 tcp_iss += TCP_ISSINCR/2; 507 tp->irs = ti->ti_seq; 508 tcp_sendseqinit(tp); 509 tcp_rcvseqinit(tp); 510 tp->t_flags |= TF_ACKNOW; 511 tp->t_state = TCPS_SYN_RECEIVED; 512 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 513 dropsocket = 0; /* committed to socket */ 514 tcpstat.tcps_accepts++; 515 goto trimthenstep6; 516 } 517 518 /* 519 * If the state is SYN_SENT: 520 * if seg contains an ACK, but not for our SYN, drop the input. 521 * if seg contains a RST, then drop the connection. 522 * if seg does not contain SYN, then drop it. 523 * Otherwise this is an acceptable SYN segment 524 * initialize tp->rcv_nxt and tp->irs 525 * if seg contains ack then advance tp->snd_una 526 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 527 * arrange for segment to be acked (eventually) 528 * continue processing rest of data/controls, beginning with URG 529 */ 530 case TCPS_SYN_SENT: 531 #ifdef VAN 532 if (om) { 533 tcp_dooptions(tp, om, ti); 534 om = 0; 535 } 536 #endif VAN 537 if ((tiflags & TH_ACK) && 538 (SEQ_LEQ(ti->ti_ack, tp->iss) || 539 SEQ_GT(ti->ti_ack, tp->snd_max))) 540 goto dropwithreset; 541 if (tiflags & TH_RST) { 542 if (tiflags & TH_ACK) 543 tp = tcp_drop(tp, ECONNREFUSED); 544 goto drop; 545 } 546 if ((tiflags & TH_SYN) == 0) 547 goto drop; 548 if (tiflags & TH_ACK) { 549 tp->snd_una = ti->ti_ack; 550 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 551 tp->snd_nxt = tp->snd_una; 552 } 553 tp->t_timer[TCPT_REXMT] = 0; 554 tp->irs = ti->ti_seq; 555 tcp_rcvseqinit(tp); 556 tp->t_flags |= TF_ACKNOW; 557 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 558 tcpstat.tcps_connects++; 559 soisconnected(so); 560 tp->t_state = TCPS_ESTABLISHED; 561 (void) tcp_reass(tp, (struct tcpiphdr *)0, 562 (struct mbuf *)0); 563 /* 564 * if we didn't have to retransmit the SYN, 565 * use its rtt as our initial srtt & rtt var. 566 */ 567 if (tp->t_rtt) 568 tcp_xmit_timer(tp); 569 } else 570 tp->t_state = TCPS_SYN_RECEIVED; 571 572 trimthenstep6: 573 /* 574 * Advance ti->ti_seq to correspond to first data byte. 575 * If data, trim to stay within window, 576 * dropping FIN if necessary. 577 */ 578 ti->ti_seq++; 579 if (ti->ti_len > tp->rcv_wnd) { 580 todrop = ti->ti_len - tp->rcv_wnd; 581 m_adj(m, -todrop); 582 ti->ti_len = tp->rcv_wnd; 583 tiflags &= ~TH_FIN; 584 tcpstat.tcps_rcvpackafterwin++; 585 tcpstat.tcps_rcvbyteafterwin += todrop; 586 } 587 tp->snd_wl1 = ti->ti_seq - 1; 588 tp->rcv_up = ti->ti_seq; 589 goto step6; 590 #ifdef VAN 591 592 default: 593 if (om) { 594 tcp_dooptions(tp, om, ti); 595 om = 0; 596 } 597 #endif VAN 598 } 599 600 /* 601 * States other than LISTEN or SYN_SENT. 602 * First check that at least some bytes of segment are within 603 * receive window. If segment begins before rcv_nxt, 604 * drop leading data (and SYN); if nothing left, just ack. 605 */ 606 todrop = tp->rcv_nxt - ti->ti_seq; 607 if (todrop > 0) { 608 if (tiflags & TH_SYN) { 609 tiflags &= ~TH_SYN; 610 ti->ti_seq++; 611 if (ti->ti_urp > 1) 612 ti->ti_urp--; 613 else 614 tiflags &= ~TH_URG; 615 todrop--; 616 } 617 if (todrop > ti->ti_len || 618 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 619 tcpstat.tcps_rcvduppack++; 620 tcpstat.tcps_rcvdupbyte += ti->ti_len; 621 /* 622 * If segment is just one to the left of the window, 623 * check two special cases: 624 * 1. Don't toss RST in response to 4.2-style keepalive. 625 * 2. If the only thing to drop is a FIN, we can drop 626 * it, but check the ACK or we will get into FIN 627 * wars if our FINs crossed (both CLOSING). 628 * In either case, send ACK to resynchronize, 629 * but keep on processing for RST or ACK. 630 */ 631 if ((tiflags & TH_FIN && todrop == ti->ti_len + 1) 632 #ifdef TCP_COMPAT_42 633 || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1) 634 #endif 635 ) { 636 todrop = ti->ti_len; 637 tiflags &= ~TH_FIN; 638 tp->t_flags |= TF_ACKNOW; 639 } else 640 goto dropafterack; 641 } else { 642 tcpstat.tcps_rcvpartduppack++; 643 tcpstat.tcps_rcvpartdupbyte += todrop; 644 } 645 m_adj(m, todrop); 646 ti->ti_seq += todrop; 647 ti->ti_len -= todrop; 648 if (ti->ti_urp > todrop) 649 ti->ti_urp -= todrop; 650 else { 651 tiflags &= ~TH_URG; 652 ti->ti_urp = 0; 653 } 654 } 655 656 /* 657 * If new data are received on a connection after the 658 * user processes are gone, then RST the other end. 659 */ 660 if ((so->so_state & SS_NOFDREF) && 661 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 662 tp = tcp_close(tp); 663 tcpstat.tcps_rcvafterclose++; 664 goto dropwithreset; 665 } 666 667 /* 668 * If segment ends after window, drop trailing data 669 * (and PUSH and FIN); if nothing left, just ACK. 670 */ 671 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 672 if (todrop > 0) { 673 tcpstat.tcps_rcvpackafterwin++; 674 if (todrop >= ti->ti_len) { 675 tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 676 /* 677 * If a new connection request is received 678 * while in TIME_WAIT, drop the old connection 679 * and start over if the sequence numbers 680 * are above the previous ones. 681 */ 682 if (tiflags & TH_SYN && 683 tp->t_state == TCPS_TIME_WAIT && 684 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 685 iss = tp->rcv_nxt + TCP_ISSINCR; 686 tp = tcp_close(tp); 687 goto findpcb; 688 } 689 /* 690 * If window is closed can only take segments at 691 * window edge, and have to drop data and PUSH from 692 * incoming segments. Continue processing, but 693 * remember to ack. Otherwise, drop segment 694 * and ack. 695 */ 696 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 697 tp->t_flags |= TF_ACKNOW; 698 tcpstat.tcps_rcvwinprobe++; 699 } else 700 goto dropafterack; 701 } else 702 tcpstat.tcps_rcvbyteafterwin += todrop; 703 m_adj(m, -todrop); 704 ti->ti_len -= todrop; 705 tiflags &= ~(TH_PUSH|TH_FIN); 706 } 707 708 /* 709 * If the RST bit is set examine the state: 710 * SYN_RECEIVED STATE: 711 * If passive open, return to LISTEN state. 712 * If active open, inform user that connection was refused. 713 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 714 * Inform user that connection was reset, and close tcb. 715 * CLOSING, LAST_ACK, TIME_WAIT STATES 716 * Close the tcb. 717 */ 718 if (tiflags&TH_RST) switch (tp->t_state) { 719 720 case TCPS_SYN_RECEIVED: 721 so->so_error = ECONNREFUSED; 722 goto close; 723 724 case TCPS_ESTABLISHED: 725 case TCPS_FIN_WAIT_1: 726 case TCPS_FIN_WAIT_2: 727 case TCPS_CLOSE_WAIT: 728 so->so_error = ECONNRESET; 729 close: 730 tp->t_state = TCPS_CLOSED; 731 tcpstat.tcps_drops++; 732 tp = tcp_close(tp); 733 goto drop; 734 735 case TCPS_CLOSING: 736 case TCPS_LAST_ACK: 737 case TCPS_TIME_WAIT: 738 tp = tcp_close(tp); 739 goto drop; 740 } 741 742 /* 743 * If a SYN is in the window, then this is an 744 * error and we send an RST and drop the connection. 745 */ 746 if (tiflags & TH_SYN) { 747 tp = tcp_drop(tp, ECONNRESET); 748 goto dropwithreset; 749 } 750 751 /* 752 * If the ACK bit is off we drop the segment and return. 753 */ 754 if ((tiflags & TH_ACK) == 0) 755 goto drop; 756 757 /* 758 * Ack processing. 759 */ 760 switch (tp->t_state) { 761 762 /* 763 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 764 * ESTABLISHED state and continue processing, otherwise 765 * send an RST. 766 */ 767 case TCPS_SYN_RECEIVED: 768 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 769 SEQ_GT(ti->ti_ack, tp->snd_max)) 770 goto dropwithreset; 771 tcpstat.tcps_connects++; 772 soisconnected(so); 773 tp->t_state = TCPS_ESTABLISHED; 774 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0); 775 tp->snd_wl1 = ti->ti_seq - 1; 776 /* fall into ... */ 777 778 /* 779 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 780 * ACKs. If the ack is in the range 781 * tp->snd_una < ti->ti_ack <= tp->snd_max 782 * then advance tp->snd_una to ti->ti_ack and drop 783 * data from the retransmission queue. If this ACK reflects 784 * more up to date window information we update our window information. 785 */ 786 case TCPS_ESTABLISHED: 787 case TCPS_FIN_WAIT_1: 788 case TCPS_FIN_WAIT_2: 789 case TCPS_CLOSE_WAIT: 790 case TCPS_CLOSING: 791 case TCPS_LAST_ACK: 792 case TCPS_TIME_WAIT: 793 794 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 795 if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) { 796 tcpstat.tcps_rcvdupack++; 797 /* 798 * If we have outstanding data (other than 799 * a window probe), this is a completely 800 * duplicate ack (ie, window info didn't 801 * change), the ack is the biggest we've 802 * seen and we've seen exactly our rexmt 803 * threshhold of them, assume a packet 804 * has been dropped and retransmit it. 805 * Kludge snd_nxt & the congestion 806 * window so we send only this one 807 * packet. 808 * 809 * We know we're losing at the current 810 * window size so do congestion avoidance 811 * (set ssthresh to half the current window 812 * and pull our congestion window back to 813 * the new ssthresh). 814 * 815 * Dup acks mean that packets have left the 816 * network (they're now cached at the receiver) 817 * so bump cwnd by the amount in the receiver 818 * to keep a constant cwnd packets in the 819 * network. 820 */ 821 if (tp->t_timer[TCPT_REXMT] == 0 || 822 ti->ti_ack != tp->snd_una) 823 tp->t_dupacks = 0; 824 else if (++tp->t_dupacks == tcprexmtthresh) { 825 tcp_seq onxt = tp->snd_nxt; 826 u_int win = 827 min(tp->snd_wnd, tp->snd_cwnd) / 2 / 828 tp->t_maxseg; 829 830 if (win < 2) 831 win = 2; 832 tp->snd_ssthresh = win * tp->t_maxseg; 833 tp->t_timer[TCPT_REXMT] = 0; 834 tp->t_rtt = 0; 835 tp->snd_nxt = ti->ti_ack; 836 tp->snd_cwnd = tp->t_maxseg; 837 (void) tcp_output(tp); 838 tp->snd_cwnd = tp->snd_ssthresh + 839 tp->t_maxseg * tp->t_dupacks; 840 if (SEQ_GT(onxt, tp->snd_nxt)) 841 tp->snd_nxt = onxt; 842 goto drop; 843 } else if (tp->t_dupacks > tcprexmtthresh) { 844 tp->snd_cwnd += tp->t_maxseg; 845 (void) tcp_output(tp); 846 goto drop; 847 } 848 } else 849 tp->t_dupacks = 0; 850 break; 851 } 852 /* 853 * If the congestion window was inflated to account 854 * for the other side's cached packets, retract it. 855 */ 856 if (tp->t_dupacks > tcprexmtthresh && 857 tp->snd_cwnd > tp->snd_ssthresh) 858 tp->snd_cwnd = tp->snd_ssthresh; 859 tp->t_dupacks = 0; 860 if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 861 tcpstat.tcps_rcvacktoomuch++; 862 goto dropafterack; 863 } 864 acked = ti->ti_ack - tp->snd_una; 865 tcpstat.tcps_rcvackpack++; 866 tcpstat.tcps_rcvackbyte += acked; 867 868 /* 869 * If transmit timer is running and timed sequence 870 * number was acked, update smoothed round trip time. 871 * Since we now have an rtt measurement, cancel the 872 * timer backoff (cf., Phil Karn's retransmit alg.). 873 * Recompute the initial retransmit timer. 874 */ 875 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 876 tcp_xmit_timer(tp); 877 878 /* 879 * If all outstanding data is acked, stop retransmit 880 * timer and remember to restart (more output or persist). 881 * If there is more data to be acked, restart retransmit 882 * timer, using current (possibly backed-off) value. 883 */ 884 if (ti->ti_ack == tp->snd_max) { 885 tp->t_timer[TCPT_REXMT] = 0; 886 needoutput = 1; 887 } else if (tp->t_timer[TCPT_PERSIST] == 0) 888 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 889 /* 890 * When new data is acked, open the congestion window. 891 * If the window gives us less than ssthresh packets 892 * in flight, open exponentially (maxseg per packet). 893 * Otherwise open linearly: maxseg per window 894 * (maxseg^2 / cwnd per packet), plus a constant 895 * fraction of a packet (maxseg/8) to help larger windows 896 * open quickly enough. 897 */ 898 { 899 register u_int cw = tp->snd_cwnd; 900 register u_int incr = tp->t_maxseg; 901 902 if (cw > tp->snd_ssthresh) 903 incr = incr * incr / cw + incr / 8; 904 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN); 905 } 906 if (acked > so->so_snd.sb_cc) { 907 tp->snd_wnd -= so->so_snd.sb_cc; 908 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 909 ourfinisacked = 1; 910 } else { 911 sbdrop(&so->so_snd, acked); 912 tp->snd_wnd -= acked; 913 ourfinisacked = 0; 914 } 915 if (so->so_snd.sb_flags & SB_NOTIFY) 916 sowwakeup(so); 917 tp->snd_una = ti->ti_ack; 918 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 919 tp->snd_nxt = tp->snd_una; 920 921 switch (tp->t_state) { 922 923 /* 924 * In FIN_WAIT_1 STATE in addition to the processing 925 * for the ESTABLISHED state if our FIN is now acknowledged 926 * then enter FIN_WAIT_2. 927 */ 928 case TCPS_FIN_WAIT_1: 929 if (ourfinisacked) { 930 /* 931 * If we can't receive any more 932 * data, then closing user can proceed. 933 * Starting the timer is contrary to the 934 * specification, but if we don't get a FIN 935 * we'll hang forever. 936 */ 937 if (so->so_state & SS_CANTRCVMORE) { 938 soisdisconnected(so); 939 tp->t_timer[TCPT_2MSL] = tcp_maxidle; 940 } 941 tp->t_state = TCPS_FIN_WAIT_2; 942 } 943 break; 944 945 /* 946 * In CLOSING STATE in addition to the processing for 947 * the ESTABLISHED state if the ACK acknowledges our FIN 948 * then enter the TIME-WAIT state, otherwise ignore 949 * the segment. 950 */ 951 case TCPS_CLOSING: 952 if (ourfinisacked) { 953 tp->t_state = TCPS_TIME_WAIT; 954 tcp_canceltimers(tp); 955 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 956 soisdisconnected(so); 957 } 958 break; 959 960 /* 961 * In LAST_ACK, we may still be waiting for data to drain 962 * and/or to be acked, as well as for the ack of our FIN. 963 * If our FIN is now acknowledged, delete the TCB, 964 * enter the closed state and return. 965 */ 966 case TCPS_LAST_ACK: 967 if (ourfinisacked) { 968 tp = tcp_close(tp); 969 goto drop; 970 } 971 break; 972 973 /* 974 * In TIME_WAIT state the only thing that should arrive 975 * is a retransmission of the remote FIN. Acknowledge 976 * it and restart the finack timer. 977 */ 978 case TCPS_TIME_WAIT: 979 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 980 goto dropafterack; 981 } 982 } 983 984 step6: 985 /* 986 * Update window information. 987 * Don't look at window if no ACK: TAC's send garbage on first SYN. 988 */ 989 if ((tiflags & TH_ACK) && 990 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 991 (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 992 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 993 /* keep track of pure window updates */ 994 if (ti->ti_len == 0 && 995 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) 996 tcpstat.tcps_rcvwinupd++; 997 tp->snd_wnd = ti->ti_win; 998 tp->snd_wl1 = ti->ti_seq; 999 tp->snd_wl2 = ti->ti_ack; 1000 if (tp->snd_wnd > tp->max_sndwnd) 1001 tp->max_sndwnd = tp->snd_wnd; 1002 needoutput = 1; 1003 } 1004 1005 /* 1006 * Process segments with URG. 1007 */ 1008 if ((tiflags & TH_URG) && ti->ti_urp && 1009 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1010 /* 1011 * This is a kludge, but if we receive and accept 1012 * random urgent pointers, we'll crash in 1013 * soreceive. It's hard to imagine someone 1014 * actually wanting to send this much urgent data. 1015 */ 1016 if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 1017 ti->ti_urp = 0; /* XXX */ 1018 tiflags &= ~TH_URG; /* XXX */ 1019 goto dodata; /* XXX */ 1020 } 1021 /* 1022 * If this segment advances the known urgent pointer, 1023 * then mark the data stream. This should not happen 1024 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1025 * a FIN has been received from the remote side. 1026 * In these states we ignore the URG. 1027 * 1028 * According to RFC961 (Assigned Protocols), 1029 * the urgent pointer points to the last octet 1030 * of urgent data. We continue, however, 1031 * to consider it to indicate the first octet 1032 * of data past the urgent section as the original 1033 * spec states (in one of two places). 1034 */ 1035 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 1036 tp->rcv_up = ti->ti_seq + ti->ti_urp; 1037 so->so_oobmark = so->so_rcv.sb_cc + 1038 (tp->rcv_up - tp->rcv_nxt) - 1; 1039 if (so->so_oobmark == 0) 1040 so->so_state |= SS_RCVATMARK; 1041 sohasoutofband(so); 1042 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1043 } 1044 /* 1045 * Remove out of band data so doesn't get presented to user. 1046 * This can happen independent of advancing the URG pointer, 1047 * but if two URG's are pending at once, some out-of-band 1048 * data may creep in... ick. 1049 */ 1050 if (ti->ti_urp <= ti->ti_len 1051 #ifdef SO_OOBINLINE 1052 && (so->so_options & SO_OOBINLINE) == 0 1053 #endif 1054 ) 1055 tcp_pulloutofband(so, ti, m); 1056 } else 1057 /* 1058 * If no out of band data is expected, 1059 * pull receive urgent pointer along 1060 * with the receive window. 1061 */ 1062 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1063 tp->rcv_up = tp->rcv_nxt; 1064 dodata: /* XXX */ 1065 1066 /* 1067 * Process the segment text, merging it into the TCP sequencing queue, 1068 * and arranging for acknowledgment of receipt if necessary. 1069 * This process logically involves adjusting tp->rcv_wnd as data 1070 * is presented to the user (this happens in tcp_usrreq.c, 1071 * case PRU_RCVD). If a FIN has already been received on this 1072 * connection then we just ignore the text. 1073 */ 1074 if ((ti->ti_len || (tiflags&TH_FIN)) && 1075 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1076 TCP_REASS(tp, ti, m, so, tiflags); 1077 /* 1078 * Note the amount of data that peer has sent into 1079 * our window, in order to estimate the sender's 1080 * buffer size. 1081 */ 1082 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1083 } else { 1084 m_freem(m); 1085 tiflags &= ~TH_FIN; 1086 } 1087 1088 /* 1089 * If FIN is received ACK the FIN and let the user know 1090 * that the connection is closing. 1091 */ 1092 if (tiflags & TH_FIN) { 1093 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1094 socantrcvmore(so); 1095 tp->t_flags |= TF_ACKNOW; 1096 tp->rcv_nxt++; 1097 } 1098 switch (tp->t_state) { 1099 1100 /* 1101 * In SYN_RECEIVED and ESTABLISHED STATES 1102 * enter the CLOSE_WAIT state. 1103 */ 1104 case TCPS_SYN_RECEIVED: 1105 case TCPS_ESTABLISHED: 1106 tp->t_state = TCPS_CLOSE_WAIT; 1107 break; 1108 1109 /* 1110 * If still in FIN_WAIT_1 STATE FIN has not been acked so 1111 * enter the CLOSING state. 1112 */ 1113 case TCPS_FIN_WAIT_1: 1114 tp->t_state = TCPS_CLOSING; 1115 break; 1116 1117 /* 1118 * In FIN_WAIT_2 state enter the TIME_WAIT state, 1119 * starting the time-wait timer, turning off the other 1120 * standard timers. 1121 */ 1122 case TCPS_FIN_WAIT_2: 1123 tp->t_state = TCPS_TIME_WAIT; 1124 tcp_canceltimers(tp); 1125 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1126 soisdisconnected(so); 1127 break; 1128 1129 /* 1130 * In TIME_WAIT state restart the 2 MSL time_wait timer. 1131 */ 1132 case TCPS_TIME_WAIT: 1133 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1134 break; 1135 } 1136 } 1137 if (so->so_options & SO_DEBUG) 1138 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 1139 1140 /* 1141 * Return any desired output. 1142 */ 1143 if (needoutput || (tp->t_flags & TF_ACKNOW)) 1144 (void) tcp_output(tp); 1145 return; 1146 1147 dropafterack: 1148 /* 1149 * Generate an ACK dropping incoming segment if it occupies 1150 * sequence space, where the ACK reflects our state. 1151 */ 1152 if (tiflags & TH_RST) 1153 goto drop; 1154 m_freem(m); 1155 tp->t_flags |= TF_ACKNOW; 1156 (void) tcp_output(tp); 1157 return; 1158 1159 dropwithreset: 1160 if (om) { 1161 (void) m_free(om); 1162 om = 0; 1163 } 1164 /* 1165 * Generate a RST, dropping incoming segment. 1166 * Make ACK acceptable to originator of segment. 1167 * Don't bother to respond if destination was broadcast. 1168 */ 1169 if ((tiflags & TH_RST) || m->m_flags & M_BCAST) 1170 goto drop; 1171 if (tiflags & TH_ACK) 1172 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 1173 else { 1174 if (tiflags & TH_SYN) 1175 ti->ti_len++; 1176 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 1177 TH_RST|TH_ACK); 1178 } 1179 /* destroy temporarily created socket */ 1180 if (dropsocket) 1181 (void) soabort(so); 1182 return; 1183 1184 drop: 1185 if (om) 1186 (void) m_free(om); 1187 /* 1188 * Drop space held by incoming segment and return. 1189 */ 1190 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1191 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1192 m_freem(m); 1193 /* destroy temporarily created socket */ 1194 if (dropsocket) 1195 (void) soabort(so); 1196 return; 1197 } 1198 1199 tcp_dooptions(tp, om, ti) 1200 struct tcpcb *tp; 1201 struct mbuf *om; 1202 struct tcpiphdr *ti; 1203 { 1204 register u_char *cp; 1205 u_short mss; 1206 int opt, optlen, cnt; 1207 1208 cp = mtod(om, u_char *); 1209 cnt = om->m_len; 1210 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1211 opt = cp[0]; 1212 if (opt == TCPOPT_EOL) 1213 break; 1214 if (opt == TCPOPT_NOP) 1215 optlen = 1; 1216 else { 1217 optlen = cp[1]; 1218 if (optlen <= 0) 1219 break; 1220 } 1221 switch (opt) { 1222 1223 default: 1224 continue; 1225 1226 case TCPOPT_MAXSEG: 1227 if (optlen != 4) 1228 continue; 1229 if (!(ti->ti_flags & TH_SYN)) 1230 continue; 1231 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 1232 NTOHS(mss); 1233 (void) tcp_mss(tp, mss); /* sets t_maxseg */ 1234 break; 1235 } 1236 } 1237 (void) m_free(om); 1238 } 1239 1240 /* 1241 * Pull out of band byte out of a segment so 1242 * it doesn't appear in the user's data queue. 1243 * It is still reflected in the segment length for 1244 * sequencing purposes. 1245 */ 1246 tcp_pulloutofband(so, ti, m) 1247 struct socket *so; 1248 struct tcpiphdr *ti; 1249 register struct mbuf *m; 1250 { 1251 int cnt = ti->ti_urp - 1; 1252 1253 while (cnt >= 0) { 1254 if (m->m_len > cnt) { 1255 char *cp = mtod(m, caddr_t) + cnt; 1256 struct tcpcb *tp = sototcpcb(so); 1257 1258 tp->t_iobc = *cp; 1259 tp->t_oobflags |= TCPOOB_HAVEDATA; 1260 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 1261 m->m_len--; 1262 return; 1263 } 1264 cnt -= m->m_len; 1265 m = m->m_next; 1266 if (m == 0) 1267 break; 1268 } 1269 panic("tcp_pulloutofband"); 1270 } 1271 1272 /* 1273 * Collect new round-trip time estimate 1274 * and update averages and current timeout. 1275 */ 1276 tcp_xmit_timer(tp) 1277 register struct tcpcb *tp; 1278 { 1279 register short delta; 1280 1281 tcpstat.tcps_rttupdated++; 1282 if (tp->t_srtt != 0) { 1283 /* 1284 * srtt is stored as fixed point with 3 bits after the 1285 * binary point (i.e., scaled by 8). The following magic 1286 * is equivalent to the smoothing algorithm in rfc793 with 1287 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 1288 * point). Adjust t_rtt to origin 0. 1289 */ 1290 delta = tp->t_rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT); 1291 if ((tp->t_srtt += delta) <= 0) 1292 tp->t_srtt = 1; 1293 /* 1294 * We accumulate a smoothed rtt variance (actually, a 1295 * smoothed mean difference), then set the retransmit 1296 * timer to smoothed rtt + 4 times the smoothed variance. 1297 * rttvar is stored as fixed point with 2 bits after the 1298 * binary point (scaled by 4). The following is 1299 * equivalent to rfc793 smoothing with an alpha of .75 1300 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 1301 * rfc793's wired-in beta. 1302 */ 1303 if (delta < 0) 1304 delta = -delta; 1305 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 1306 if ((tp->t_rttvar += delta) <= 0) 1307 tp->t_rttvar = 1; 1308 } else { 1309 /* 1310 * No rtt measurement yet - use the unsmoothed rtt. 1311 * Set the variance to half the rtt (so our first 1312 * retransmit happens at 2*rtt) 1313 */ 1314 tp->t_srtt = tp->t_rtt << TCP_RTT_SHIFT; 1315 tp->t_rttvar = tp->t_rtt << (TCP_RTTVAR_SHIFT - 1); 1316 } 1317 tp->t_rtt = 0; 1318 tp->t_rxtshift = 0; 1319 1320 /* 1321 * the retransmit should happen at rtt + 4 * rttvar. 1322 * Because of the way we do the smoothing, srtt and rttvar 1323 * will each average +1/2 tick of bias. When we compute 1324 * the retransmit timer, we want 1/2 tick of rounding and 1325 * 1 extra tick because of +-1/2 tick uncertainty in the 1326 * firing of the timer. The bias will give us exactly the 1327 * 1.5 tick we need. But, because the bias is 1328 * statistical, we have to test that we don't drop below 1329 * the minimum feasible timer (which is 2 ticks). 1330 */ 1331 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 1332 tp->t_rttmin, TCPTV_REXMTMAX); 1333 1334 /* 1335 * We received an ack for a packet that wasn't retransmitted; 1336 * it is probably safe to discard any error indications we've 1337 * received recently. This isn't quite right, but close enough 1338 * for now (a route might have failed after we sent a segment, 1339 * and the return path might not be symmetrical). 1340 */ 1341 tp->t_softerror = 0; 1342 } 1343 1344 /* 1345 * Determine a reasonable value for maxseg size. 1346 * If the route is known, check route for mtu. 1347 * If none, use an mss that can be handled on the outgoing 1348 * interface without forcing IP to fragment; if bigger than 1349 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 1350 * to utilize large mbufs. If no route is found, route has no mtu, 1351 * or the destination isn't local, use a default, hopefully conservative 1352 * size (usually 512 or the default IP max size, but no more than the mtu 1353 * of the interface), as we can't discover anything about intervening 1354 * gateways or networks. We also initialize the congestion/slow start 1355 * window to be a single segment if the destination isn't local. 1356 * While looking at the routing entry, we also initialize other path-dependent 1357 * parameters from pre-set or cached values in the routing entry. 1358 */ 1359 1360 tcp_mss(tp, offer) 1361 register struct tcpcb *tp; 1362 u_short offer; 1363 { 1364 struct route *ro; 1365 register struct rtentry *rt; 1366 struct ifnet *ifp; 1367 register int rtt, mss; 1368 u_long bufsize; 1369 struct inpcb *inp; 1370 struct socket *so; 1371 extern int tcp_mssdflt, tcp_rttdflt; 1372 1373 inp = tp->t_inpcb; 1374 ro = &inp->inp_route; 1375 1376 if ((rt = ro->ro_rt) == (struct rtentry *)0) { 1377 /* No route yet, so try to acquire one */ 1378 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1379 ro->ro_dst.sa_family = AF_INET; 1380 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 1381 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 1382 inp->inp_faddr; 1383 rtalloc(ro); 1384 } 1385 if ((rt = ro->ro_rt) == (struct rtentry *)0) 1386 return (tcp_mssdflt); 1387 } 1388 ifp = rt->rt_ifp; 1389 so = inp->inp_socket; 1390 1391 #ifdef RTV_MTU /* if route characteristics exist ... */ 1392 /* 1393 * While we're here, check if there's an initial rtt 1394 * or rttvar. Convert from the route-table units 1395 * to scaled multiples of the slow timeout timer. 1396 */ 1397 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 1398 if (rt->rt_rmx.rmx_locks & RTV_MTU) 1399 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 1400 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 1401 if (rt->rt_rmx.rmx_rttvar) 1402 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 1403 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 1404 else 1405 /* default variation is +- 1 rtt */ 1406 tp->t_rttvar = 1407 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 1408 TCPT_RANGESET(tp->t_rxtcur, 1409 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 1410 tp->t_rttmin, TCPTV_REXMTMAX); 1411 } 1412 /* 1413 * if there's an mtu associated with the route, use it 1414 */ 1415 if (rt->rt_rmx.rmx_mtu) 1416 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 1417 else 1418 #endif /* RTV_MTU */ 1419 { 1420 mss = ifp->if_mtu - sizeof(struct tcpiphdr); 1421 #if (MCLBYTES & (MCLBYTES - 1)) == 0 1422 if (mss > MCLBYTES) 1423 mss &= ~(MCLBYTES-1); 1424 #else 1425 if (mss > MCLBYTES) 1426 mss = mss / MCLBYTES * MCLBYTES; 1427 #endif 1428 if (!in_localaddr(inp->inp_faddr)) 1429 mss = min(mss, tcp_mssdflt); 1430 } 1431 /* 1432 * The current mss, t_maxseg, is initialized to the default value. 1433 * If we compute a smaller value, reduce the current mss. 1434 * If we compute a larger value, return it for use in sending 1435 * a max seg size option, but don't store it for use 1436 * unless we received an offer at least that large from peer. 1437 * However, do not accept offers under 32 bytes. 1438 */ 1439 if (offer) 1440 mss = min(mss, offer); 1441 mss = max(mss, 32); /* sanity */ 1442 if (mss < tp->t_maxseg || offer != 0) { 1443 /* 1444 * If there's a pipesize, change the socket buffer 1445 * to that size. Make the socket buffers an integral 1446 * number of mss units; if the mss is larger than 1447 * the socket buffer, decrease the mss. 1448 */ 1449 #ifdef RTV_SPIPE 1450 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 1451 #endif 1452 bufsize = so->so_snd.sb_hiwat; 1453 if (bufsize < mss) 1454 mss = bufsize; 1455 else { 1456 bufsize = min(bufsize, SB_MAX) / mss * mss; 1457 (void) sbreserve(&so->so_snd, bufsize); 1458 } 1459 tp->t_maxseg = mss; 1460 1461 #ifdef RTV_RPIPE 1462 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 1463 #endif 1464 bufsize = so->so_rcv.sb_hiwat; 1465 if (bufsize > mss) { 1466 bufsize = min(bufsize, SB_MAX) / mss * mss; 1467 (void) sbreserve(&so->so_rcv, bufsize); 1468 } 1469 } 1470 tp->snd_cwnd = mss; 1471 1472 #ifdef RTV_SSTHRESH 1473 if (rt->rt_rmx.rmx_ssthresh) { 1474 /* 1475 * There's some sort of gateway or interface 1476 * buffer limit on the path. Use this to set 1477 * the slow start threshhold, but set the 1478 * threshold to no less than 2*mss. 1479 */ 1480 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 1481 } 1482 #endif /* RTV_MTU */ 1483 return (mss); 1484 } 1485