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