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