1 /* tcp_input.c 1.38 81/12/09 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/mbuf.h" 6 #include "../h/protosw.h" 7 #include "../h/socket.h" 8 #include "../h/socketvar.h" 9 #include "../net/in.h" 10 #include "../net/in_pcb.h" 11 #include "../net/in_systm.h" 12 #include "../net/if.h" 13 #include "../net/ip.h" 14 #include "../net/ip_var.h" 15 #include "../net/tcp.h" 16 #include "../net/tcp_fsm.h" 17 #include "../net/tcp_seq.h" 18 #include "../net/tcp_timer.h" 19 #include "../net/tcp_var.h" 20 #include "../net/tcpip.h" 21 #include "../errno.h" 22 23 int tcpcksum = 1; 24 25 /* 26 * TCP input routine, follows pages 65-76 of the 27 * protocol specification dated September, 1981 very closely. 28 */ 29 tcp_input(m0) 30 struct mbuf *m0; 31 { 32 register struct tcpiphdr *ti; 33 struct inpcb *inp; 34 register struct mbuf *m; 35 int len, tlen, off; 36 register struct tcpcb *tp; 37 register int tiflags; 38 struct socket *so; 39 int todrop, acked; 40 41 COUNT(TCP_INPUT); 42 /* 43 * Get ip and tcp header together in first mbuf. 44 * Note: ip leaves ip header in first mbuf. 45 */ 46 m = m0; 47 ti = mtod(m, struct tcpiphdr *); 48 if (((struct ip *)ti)->ip_len > sizeof (struct ip)) 49 ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 50 if (m->m_len < sizeof (struct tcpiphdr)) { 51 if (m_pullup(m, sizeof (struct tcpiphdr)) == 0) { 52 tcpstat.tcps_hdrops++; 53 goto drop; 54 } 55 ti = mtod(m, struct tcpiphdr *); 56 } 57 58 /* 59 * Checksum extended tcp header and data. 60 */ 61 tlen = ((struct ip *)ti)->ip_len; 62 len = sizeof (struct ip) + tlen; 63 if (tcpcksum) { 64 ti->ti_next = ti->ti_prev = 0; 65 ti->ti_x1 = 0; 66 ti->ti_len = (u_short)tlen; 67 #if vax 68 ti->ti_len = htons(ti->ti_len); 69 #endif 70 if (ti->ti_sum = in_cksum(m, len)) { 71 tcpstat.tcps_badsum++; 72 printf("tcp cksum %x\n", ti->ti_sum); 73 goto drop; 74 } 75 } 76 77 /* 78 * Check that tcp offset makes sense, 79 * process tcp options and adjust length. 80 */ 81 off = ti->ti_off << 2; 82 if (off < sizeof (struct tcphdr) || off > tlen) { 83 tcpstat.tcps_badoff++; 84 goto drop; 85 } 86 ti->ti_len = tlen - off; 87 #if 0 88 if (off > sizeof (struct tcphdr)) 89 tcp_options(ti); 90 #endif 91 tiflags = ti->ti_flags; 92 93 #if vax 94 /* 95 * Convert tcp protocol specific fields to host format. 96 */ 97 ti->ti_seq = ntohl(ti->ti_seq); 98 ti->ti_ack = ntohl(ti->ti_ack); 99 ti->ti_win = ntohs(ti->ti_win); 100 ti->ti_urp = ntohs(ti->ti_urp); 101 #endif 102 103 /* 104 * Locate pcb for segment. 105 */ 106 inp = in_pcblookup 107 (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 108 109 /* 110 * If the state is CLOSED (i.e., TCB does not exist) then 111 * all data in the incoming segment is discarded. (p. 65). 112 */ 113 if (inp == 0) 114 goto dropwithreset; 115 tp = intotcpcb(inp); 116 if (tp == 0) 117 goto dropwithreset; 118 so = inp->inp_socket; 119 120 /* 121 * Segment received on connection. 122 * Reset idle time and keep-alive timer. 123 */ 124 tp->t_idle = 0; 125 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 126 127 /* 128 * Calculate amount of space in receive window, 129 * and then do TCP input processing. 130 */ 131 tp->rcv_wnd = sbspace(&so->so_rcv); 132 if (tp->rcv_wnd < 0) 133 tp->rcv_wnd = 0; 134 135 switch (tp->t_state) { 136 137 /* 138 * If the state is LISTEN then ignore segment if it contains an RST. 139 * If the segment contains an ACK then it is bad and send a RST. 140 * If it does not contain a SYN then it is not interesting; drop it. 141 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 142 * tp->iss, and send a segment: 143 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 144 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 145 * Fill in remote peer address fields if not previously specified. 146 * Enter SYN_RECEIVED state, and process any other fields of this 147 * segment in this state. (p. 65) 148 */ 149 case TCPS_LISTEN: 150 if (tiflags & TH_RST) 151 goto drop; 152 if (tiflags & TH_ACK) 153 goto dropwithreset; 154 if ((tiflags & TH_SYN) == 0) 155 goto drop; 156 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 157 tp->irs = ti->ti_seq; 158 tcp_sendseqinit(tp); 159 tcp_rcvseqinit(tp); 160 tp->t_state = TCPS_SYN_RECEIVED; 161 in_pcbconnect(inp, ti->ti_src, ti->ti_sport); 162 goto trimthenstep6; 163 164 /* 165 * If the state is SYN_SENT: 166 * if seg contains an ACK, but not for our SYN, drop the input. 167 * if seg contains a RST, then drop the connection. 168 * if seg does not contain SYN, then drop it. 169 * Otherwise this is an acceptable SYN segment 170 * initialize tp->rcv_nxt and tp->irs 171 * if seg contains ack then advance tp->snd_una 172 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 173 * arrange for segment to be acked (eventually) 174 * continue processing rest of data/controls, beginning with URG 175 */ 176 case TCPS_SYN_SENT: 177 if ((tiflags & TH_ACK) && 178 (SEQ_LEQ(ti->ti_ack, tp->iss) || 179 SEQ_GT(ti->ti_ack, tp->snd_max))) 180 goto dropwithreset; 181 if (tiflags & TH_RST) { 182 if (tiflags & TH_ACK) 183 tcp_drop(tp, ECONNRESET); 184 goto drop; 185 } 186 if ((tiflags & TH_SYN) == 0) 187 goto drop; 188 tp->snd_una = ti->ti_ack; 189 tp->irs = ti->ti_seq; 190 tcp_rcvseqinit(tp); 191 tp->t_flags |= TF_ACKNOW; 192 if (SEQ_GT(tp->snd_una, tp->iss)) { 193 tp->t_state = TCPS_ESTABLISHED; 194 (void) tcp_reass(tp, (struct tcpiphdr *)0); 195 tp->snd_wl1 = ti->ti_seq; 196 } else 197 tp->t_state = TCPS_SYN_RECEIVED; 198 goto trimthenstep6; 199 200 trimthenstep6: 201 /* 202 * Advance ti->ti_seq to correspond to first data byte. 203 * If data, trim to stay within window, 204 * dropping FIN if necessary. 205 */ 206 ti->ti_seq++; 207 if (ti->ti_len > tp->rcv_wnd) { 208 todrop = ti->ti_len - tp->rcv_wnd; 209 m_adj(m, -todrop); 210 ti->ti_len = tp->rcv_wnd; 211 ti->ti_flags &= ~TH_FIN; 212 } 213 goto step6; 214 } 215 216 /* 217 * States other than LISTEN or SYN_SENT. 218 * First check that at least some bytes of segment are within 219 * receive window. 220 */ 221 if (tp->rcv_wnd == 0) { 222 /* 223 * If window is closed can only take segments at 224 * window edge, and have to drop data and PUSH from 225 * incoming segments. 226 */ 227 if (tp->rcv_nxt != ti->ti_seq) 228 goto dropafterack; 229 if (ti->ti_len > 0) { 230 ti->ti_len = 0; 231 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 232 } 233 } else { 234 /* 235 * If segment begins before rcv_nxt, drop leading 236 * data (and SYN); if nothing left, just ack. 237 */ 238 if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 239 todrop = tp->rcv_nxt - ti->ti_seq; 240 if (tiflags & TH_SYN) { 241 ti->ti_seq++; 242 if (ti->ti_urp > 1) 243 ti->ti_urp--; 244 else 245 tiflags &= ~TH_URG; 246 todrop--; 247 } 248 if (todrop > ti->ti_len) 249 goto dropafterack; 250 m_adj(m, todrop); 251 ti->ti_seq += todrop; 252 ti->ti_len -= todrop; 253 if (ti->ti_urp > todrop) 254 ti->ti_urp -= todrop; 255 else { 256 tiflags &= ~TH_URG; 257 /* ti->ti_flags &= ~TH_URG; */ 258 /* ti->ti_urp = 0; */ 259 } 260 /* tiflags &= ~TH_SYN; */ 261 /* ti->ti_flags &= ~TH_SYN; */ 262 } 263 /* 264 * If segment ends after window, drop trailing data 265 * (and PUSH and FIN); if nothing left, just ACK. 266 */ 267 if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 268 todrop = 269 ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 270 if (todrop > ti->ti_len) 271 goto dropafterack; 272 m_adj(m, -todrop); 273 ti->ti_len -= todrop; 274 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 275 } 276 } 277 278 /* 279 * If the RST bit is set examine the state: 280 * SYN_RECEIVED STATE: 281 * If passive open, return to LISTEN state. 282 * If active open, inform user that connection was refused. 283 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 284 * Inform user that connection was reset, and close tcb. 285 * CLOSING, LAST_ACK, TIME_WAIT STATES 286 * Close the tcb. 287 */ 288 if (tiflags&TH_RST) switch (tp->t_state) { 289 290 case TCPS_SYN_RECEIVED: 291 if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 292 tp->t_state = TCPS_LISTEN; 293 in_pcbdisconnect(inp); 294 goto drop; 295 } 296 tcp_drop(tp, ECONNREFUSED); 297 goto drop; 298 299 case TCPS_ESTABLISHED: 300 case TCPS_FIN_WAIT_1: 301 case TCPS_FIN_WAIT_2: 302 case TCPS_CLOSE_WAIT: 303 tcp_drop(tp, ECONNRESET); 304 goto drop; 305 306 case TCPS_CLOSING: 307 case TCPS_LAST_ACK: 308 case TCPS_TIME_WAIT: 309 tcp_close(tp); 310 goto drop; 311 } 312 313 /* 314 * If a SYN is in the window, then this is an 315 * error and we send an RST and drop the connection. 316 */ 317 if (tiflags & TH_SYN) { 318 tcp_drop(tp, ECONNRESET); 319 goto dropwithreset; 320 } 321 322 /* 323 * If the ACK bit is off we drop the segment and return. 324 */ 325 if ((tiflags & TH_ACK) == 0) 326 goto drop; 327 328 /* 329 * Ack processing. 330 */ 331 switch (tp->t_state) { 332 333 /* 334 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 335 * ESTABLISHED state and continue processing, othewise 336 * send an RST. 337 */ 338 case TCPS_SYN_RECEIVED: 339 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 340 SEQ_GT(ti->ti_ack, tp->snd_max)) 341 goto dropwithreset; 342 soisconnected(so); 343 tp->t_state = TCPS_ESTABLISHED; 344 (void) tcp_reass(tp, (struct tcpiphdr *)0); 345 tp->snd_wl1 = tp->ti_seq - 1; 346 /* fall into ... */ 347 348 /* 349 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 350 * ACKs. If the ack is in the range 351 * tp->snd_una < ti->ti_ack <= tp->snd_max 352 * then advance tp->snd_una to ti->ti_ack and drop 353 * data from the retransmission queue. If this ACK reflects 354 * more up to date window information we update our window information. 355 */ 356 case TCPS_ESTABLISHED: 357 case TCPS_FIN_WAIT_1: 358 case TCPS_FIN_WAIT_2: 359 case TCPS_CLOSE_WAIT: 360 case TCPS_CLOSING: 361 #define ourfinisacked (acked > 0) 362 363 if (SEQ_LT(ti->ti_ack, tp->snd_una)) 364 break; 365 if (SEQ_GT(ti->ti_ack, tp->snd_max)) 366 goto dropafterack; 367 acked = ti->ti_ack - tp->snd_una; 368 if (acked > so->so_snd.sb_cc) { 369 sbflush(&so->so_snd); 370 acked -= so->so_snd.sb_cc; 371 /* if acked our FIN is acked */ 372 } else { 373 sbdrop(&so->so_snd, acked); 374 acked = 0; 375 } 376 tp->snd_una = ti->ti_ack; 377 378 /* 379 * If transmit timer is running and timed sequence 380 * number was acked, update smoothed round trip time. 381 */ 382 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 383 tp->t_srtt = 384 tcp_beta * tp->t_srtt + 385 (1 - tcp_beta) * tp->t_rtt; 386 tp->t_rtt = 0; 387 } 388 389 /* 390 * Update window information. 391 */ 392 if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 393 tp->snd_wl1==ti->ti_seq && SEQ_LEQ(tp->snd_wl2,ti->ti_seq)) { 394 tp->snd_wnd = ti->ti_win; 395 tp->snd_wl1 = ti->ti_seq; 396 tp->snd_wl2 = ti->ti_ack; 397 } 398 399 switch (tp->t_state) { 400 401 /* 402 * In FIN_WAIT_1 STATE in addition to the processing 403 * for the ESTABLISHED state if our FIN is now acknowledged 404 * then enter FIN_WAIT_2. 405 */ 406 case TCPS_FIN_WAIT_1: 407 if (ourfinisacked) 408 tp->t_state = TCPS_FIN_WAIT_2; 409 break; 410 411 /* 412 * In CLOSING STATE in addition to the processing for 413 * the ESTABLISHED state if the ACK acknowledges our FIN 414 * then enter the TIME-WAIT state, otherwise ignore 415 * the segment. 416 */ 417 case TCPS_CLOSING: 418 if (ourfinisacked) 419 tp->t_state = TCPS_TIME_WAIT; 420 goto drop; 421 422 /* 423 * The only thing that can arrive in LAST_ACK state 424 * is an acknowledgment of our FIN. If our FIN is now 425 * acknowledged, delete the TCB, enter the closed state 426 * and return. 427 */ 428 case TCPS_LAST_ACK: 429 if (ourfinisacked) 430 tcp_close(tp); 431 goto drop; 432 433 /* 434 * In TIME_WAIT state the only thing that should arrive 435 * is a retransmission of the remote FIN. Acknowledge 436 * it and restart the finack timer. 437 */ 438 case TCPS_TIME_WAIT: 439 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 440 goto dropafterack; 441 } 442 #undef ourfinisacked 443 } 444 445 step6: 446 /* 447 * If an URG bit is set in the segment and is greater than the 448 * current known urgent pointer, then signal the user that the 449 * remote side has urgent data. This should not happen 450 * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 451 * a FIN has been received from the remote side. In these states 452 * we ignore the URG. 453 */ 454 if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 455 if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 456 tp->rcv_up = ti->ti_urp; 457 #if 0 458 soisurgendata(so); /* XXX */ 459 #endif 460 } 461 462 /* 463 * Process the segment text, merging it into the TCP sequencing queue, 464 * and arranging for acknowledgment of receipt if necessary. 465 * This process logically involves adjusting tp->rcv_wnd as data 466 * is presented to the user (this happens in tcp_usrreq.c, 467 * case PRU_RCVD). If a FIN has already been received on this 468 * connection then we just ignore the text. 469 */ 470 if (ti->ti_len) { 471 if (TCPS_HAVERCVDFIN(tp->t_state)) 472 goto drop; 473 off += sizeof (struct ip); /* drop IP header */ 474 m->m_off += off; 475 m->m_len -= off; 476 tiflags = tcp_reass(tp, ti); 477 tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 478 } else 479 m_freem(m); 480 481 /* 482 * If FIN is received then if we haven't received SYN and 483 * therefore can't validate drop the segment. Otherwise ACK 484 * the FIN and let the user know that the connection is closing. 485 */ 486 if ((tiflags & TH_FIN)) { 487 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 488 goto drop; 489 socantrcvmore(so); 490 tp->t_flags |= TF_ACKNOW; 491 tp->rcv_nxt++; 492 switch (tp->t_state) { 493 494 /* 495 * In SYN_RECEIVED and ESTABLISHED STATES 496 * enter the CLOSE_WAIT state. 497 */ 498 case TCPS_SYN_RECEIVED: 499 case TCPS_ESTABLISHED: 500 tp->t_state = TCPS_CLOSE_WAIT; 501 break; 502 503 /* 504 * If still in FIN_WAIT_1 STATE FIN has not been acked so 505 * enter the CLOSING state. 506 */ 507 case TCPS_FIN_WAIT_1: 508 tp->t_state = TCPS_CLOSING; 509 break; 510 511 /* 512 * In FIN_WAIT_2 state enter the TIME_WAIT state, 513 * starting the time-wait timer, turning off the other 514 * standard timers. 515 */ 516 case TCPS_FIN_WAIT_2: 517 tp->t_state = TCPS_TIME_WAIT;; 518 tcp_canceltimers(tp); 519 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 520 break; 521 522 /* 523 * In TIME_WAIT state restart the 2 MSL time_wait timer. 524 */ 525 case TCPS_TIME_WAIT: 526 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 527 break; 528 } 529 } 530 531 /* 532 * Return any desired output. 533 */ 534 tcp_output(tp); 535 return; 536 537 dropafterack: 538 /* 539 * Generate an ACK, then drop incoming segment. 540 * Make ACK reflect our state. 541 */ 542 if (tiflags & TH_RST) 543 goto drop; 544 tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 545 return; 546 547 dropwithreset: 548 /* 549 * Generate a RST, then drop incoming segment. 550 * Make ACK acceptable to originator of segment. 551 */ 552 if (tiflags & TH_RST) 553 goto drop; 554 if (tiflags & TH_ACK) 555 tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST); 556 else { 557 if (tiflags & TH_SYN) 558 ti->ti_len++; 559 tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 560 } 561 return; 562 563 drop: 564 /* 565 * Drop space held by incoming segment and return. 566 */ 567 m_freem(m); 568 } 569 570 /* 571 * Insert segment ti into reassembly queue of tcp with 572 * control block tp. Return TH_FIN if reassembly now includes 573 * a segment with FIN. 574 */ 575 tcp_reass(tp, ti) 576 register struct tcpcb *tp; 577 register struct tcpiphdr *ti; 578 { 579 register struct tcpiphdr *q; 580 struct socket *so = tp->t_inpcb->inp_socket; 581 int flags = 0; /* no FIN */ 582 COUNT(TCP_REASS); 583 584 /* 585 * Call with ti==0 after become established to 586 * force pre-ESTABLISHED data up to user socket. 587 */ 588 if (ti == 0) 589 goto present; 590 591 /* 592 * Find a segment which begins after this one does. 593 */ 594 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 595 q = (struct tcpiphdr *)q->ti_next) 596 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 597 break; 598 599 /* 600 * If there is a preceding segment, it may provide some of 601 * our data already. If so, drop the data from the incoming 602 * segment. If it provides all of our data, drop us. 603 */ 604 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 605 register int i; 606 q = (struct tcpiphdr *)(q->ti_prev); 607 /* conversion to int (in i) handles seq wraparound */ 608 i = q->ti_seq + q->ti_len - ti->ti_seq; 609 if (i > 0) { 610 if (i >= ti->ti_len) 611 goto drop; 612 m_adj(dtom(tp), i); 613 ti->ti_len -= i; 614 ti->ti_seq += i; 615 } 616 q = (struct tcpiphdr *)(q->ti_next); 617 } 618 619 /* 620 * While we overlap succeeding segments trim them or, 621 * if they are completely covered, dequeue them. 622 */ 623 while (q != (struct tcpiphdr *)tp && 624 SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 625 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 626 if (i < q->ti_len) { 627 q->ti_len -= i; 628 m_adj(dtom(q), i); 629 break; 630 } 631 q = (struct tcpiphdr *)q->ti_next; 632 m_freem(dtom(q->ti_prev)); 633 remque(q->ti_prev); 634 } 635 636 /* 637 * Stick new segment in its place. 638 */ 639 insque(ti, q->ti_prev); 640 641 /* 642 * Advance rcv_next through newly completed sequence space. 643 */ 644 while (ti->ti_seq == tp->rcv_nxt) { 645 tp->rcv_nxt += ti->ti_len; 646 flags = ti->ti_flags & TH_FIN; 647 ti = (struct tcpiphdr *)ti->ti_next; 648 if (ti == (struct tcpiphdr *)tp) 649 break; 650 } 651 652 present: 653 /* 654 * Present data to user. 655 */ 656 if (tp->t_state < TCPS_ESTABLISHED) 657 return (flags); 658 ti = tp->seg_next; 659 while (ti != (struct tcpiphdr *)tp && ti->ti_seq < tp->rcv_nxt) { 660 remque(ti); 661 sbappend(&so->so_rcv, dtom(ti)); 662 ti = (struct tcpiphdr *)ti->ti_next; 663 } 664 if (so->so_state & SS_CANTRCVMORE) 665 sbflush(&so->so_rcv); 666 else 667 sorwakeup(so); 668 return (flags); 669 drop: 670 m_freem(dtom(ti)); 671 return (flags); 672 } 673