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