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