1 /* tcp_input.c 1.35 81/12/03 */ 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 * Segment received on connection. 116 * Reset idle time and keep-alive timer. 117 */ 118 tp->t_idle = 0; 119 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 120 121 /* 122 * Calculate amount of space in receive window, 123 * and then do TCP input processing. 124 */ 125 tp->rcv_wnd = sbspace(&so->so_rcv); 126 127 switch (tp->t_state) { 128 129 /* 130 * If the state is LISTEN then ignore segment if it contains an RST. 131 * If the segment contains an ACK then it is bad and send a RST. 132 * If it does not contain a SYN then it is not interesting; drop it. 133 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 134 * tp->iss, and send a segment: 135 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 136 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 137 * Fill in remote peer address fields if not previously specified. 138 * Enter SYN_RECEIVED state, and process any other fields of this 139 * segment in this state. (p. 65) 140 */ 141 case TCPS_LISTEN: 142 if (tiflags & TH_RST) 143 goto drop; 144 if (tiflags & TH_ACK) 145 goto dropwithreset; 146 if ((tiflags & TH_SYN) == 0) 147 goto drop; 148 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 149 tp->irs = ti->ti_seq; 150 tcp_sendseqinit(tp); 151 tcp_rcvseqinit(tp); 152 tp->t_state = TCPS_SYN_RECEIVED; 153 if (inp->inp_faddr.s_addr == 0) { 154 inp->inp_faddr = ti->ti_src; 155 inp->inp_fport = ti->ti_sport; 156 } 157 goto trimthenstep6; 158 159 /* 160 * If the state is SYN_SENT: 161 * if seg contains an ACK, but not for our SYN, drop the input. 162 * if seg contains a RST, then drop the connection. 163 * if seg does not contain SYN, then drop it. 164 * Otherwise this is an acceptable SYN segment 165 * initialize tp->rcv_nxt and tp->irs 166 * if seg contains ack then advance tp->snd_una 167 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 168 * arrange for segment to be acked (eventually) 169 * continue processing rest of data/controls, beginning with URG 170 */ 171 case TCPS_SYN_SENT: 172 if ((tiflags & TH_ACK) && 173 (SEQ_LEQ(ti->ti_ack, tp->iss) || 174 SEQ_GT(ti->ti_ack, tp->snd_nxt))) 175 goto dropwithreset; 176 if (tiflags & TH_RST) { 177 if (tiflags & TH_ACK) 178 tcp_drop(tp, ECONNRESET); 179 goto drop; 180 } 181 if ((tiflags & TH_SYN) == 0) 182 goto drop; 183 tp->iss = ti->ti_ack; 184 tcp_sendseqinit(tp); 185 tp->irs = ti->ti_seq; 186 tcp_rcvseqinit(tp); 187 tp->t_flags |= TF_ACKNOW; 188 if (SEQ_GT(tp->snd_una, tp->iss)) { 189 tp->t_state = TCPS_ESTABLISHED; 190 (void) tcp_reass(tp, (struct tcpiphdr *)0); 191 } else 192 tp->t_state = TCPS_SYN_RECEIVED; 193 goto trimthenstep6; 194 195 trimthenstep6: 196 /* 197 * If had syn, advance ti->ti_seq to correspond 198 * to first data byte. 199 */ 200 if (tiflags & TH_SYN) 201 ti->ti_seq++; 202 203 /* 204 * If data, trim to stay within window, 205 * dropping FIN if necessary. 206 */ 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 EOL 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_next, 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 inp->inp_faddr.s_addr = 0; 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, ECONNABORTED); 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_nxt)) 341 goto dropwithreset; 342 soisconnected(so); 343 tp->t_state = TCPS_ESTABLISHED; 344 (void) tcp_reass(tp, (struct tcpiphdr *)0); 345 /* fall into ... */ 346 347 /* 348 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 349 * ACKs. If the ack is in the range 350 * tp->snd_una < ti->ti_ack <= tp->snd_nxt 351 * then advance tp->snd_una to ti->ti_ack and drop 352 * data from the retransmission queue. If this ACK reflects 353 * more up to date window information we update our window information. 354 */ 355 case TCPS_ESTABLISHED: 356 case TCPS_FIN_WAIT_1: 357 case TCPS_FIN_WAIT_2: 358 case TCPS_CLOSE_WAIT: 359 case TCPS_CLOSING: 360 #define ourfinisacked (acked > 0) 361 362 if (SEQ_LT(ti->ti_ack, tp->snd_una)) 363 break; 364 if (SEQ_GT(ti->ti_ack, tp->snd_nxt)) 365 goto dropafterack; 366 acked = ti->ti_ack - tp->snd_una; 367 if (acked > so->so_snd.sb_cc) { 368 sbflush(&so->so_snd); 369 acked -= so->so_snd.sb_cc; 370 /* if acked our FIN is acked */ 371 } else { 372 sbdrop(&so->so_snd, acked); 373 acked = 0; 374 } 375 376 /* 377 * If transmit timer is running and timed sequence 378 * number was acked, update smoothed round trip time. 379 */ 380 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 381 tp->t_srtt = 382 tcp_beta * tp->t_srtt + 383 (1 - tcp_beta) * tp->t_rtt; 384 tp->t_rtt = 0; 385 } 386 387 tp->snd_una = ti->ti_ack; 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 goto drop; 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 goto drop; 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 int overage; 583 COUNT(TCP_REASS); 584 585 /* 586 * Call with ti==0 after become established to 587 * force pre-ESTABLISHED data up to user socket. 588 */ 589 if (ti == 0) 590 goto present; 591 592 /* 593 * Find a segment which begins after this one does. 594 */ 595 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 596 q = (struct tcpiphdr *)q->ti_next) 597 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 598 break; 599 600 /* 601 * If there is a preceding segment, it may provide some of 602 * our data already. If so, drop the data from the incoming 603 * segment. If it provides all of our data, drop us. 604 */ 605 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 606 register int i; 607 q = (struct tcpiphdr *)(q->ti_prev); 608 /* conversion to int (in i) handles seq wraparound */ 609 i = q->ti_seq + q->ti_len - ti->ti_seq; 610 if (i > 0) { 611 if (i >= ti->ti_len) 612 goto drop; 613 m_adj(dtom(tp), i); 614 ti->ti_len -= i; 615 ti->ti_seq += i; 616 } 617 q = (struct tcpiphdr *)(q->ti_next); 618 } 619 620 /* 621 * While we overlap succeeding segments trim them or, 622 * if they are completely covered, dequeue them. 623 */ 624 while (q != (struct tcpiphdr *)tp && 625 SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 626 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 627 if (i < q->ti_len) { 628 q->ti_len -= i; 629 m_adj(dtom(q), i); 630 break; 631 } 632 q = (struct tcpiphdr *)q->ti_next; 633 m_freem(dtom(q->ti_prev)); 634 remque(q->ti_prev); 635 } 636 637 /* 638 * Stick new segment in its place. 639 */ 640 insque(ti, q->ti_prev); 641 642 /* 643 * Advance rcv_next through newly completed sequence space. 644 */ 645 while (ti->ti_seq == tp->rcv_nxt) { 646 tp->rcv_nxt += ti->ti_len; 647 flags = ti->ti_flags & TH_FIN; 648 ti = (struct tcpiphdr *)ti->ti_next; 649 if (ti == (struct tcpiphdr *)tp) 650 break; 651 } 652 653 present: 654 /* 655 * Present data to user. 656 */ 657 if (tp->t_state < TCPS_ESTABLISHED) 658 return (flags); 659 ti = tp->seg_next; 660 while (ti != (struct tcpiphdr *)tp && ti->ti_seq < tp->rcv_nxt) { 661 remque(ti); 662 sbappend(&so->so_rcv, dtom(ti)); 663 ti = (struct tcpiphdr *)ti->ti_next; 664 } 665 if (so->so_state & SS_CANTRCVMORE) 666 sbflush(&so->so_rcv); 667 else 668 sorwakeup(so); 669 return (flags); 670 drop: 671 m_freem(dtom(ti)); 672 return (flags); 673 } 674