1 /* tcp_input.c 1.89 83/02/10 */ 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 "../h/errno.h" 10 11 #include "../net/if.h" 12 #include "../net/route.h" 13 14 #include "../netinet/in.h" 15 #include "../netinet/in_pcb.h" 16 #include "../netinet/in_systm.h" 17 #include "../netinet/ip.h" 18 #include "../netinet/ip_var.h" 19 #include "../netinet/tcp.h" 20 #include "../netinet/tcp_fsm.h" 21 #include "../netinet/tcp_seq.h" 22 #include "../netinet/tcp_timer.h" 23 #include "../netinet/tcp_var.h" 24 #include "../netinet/tcpip.h" 25 #include "../netinet/tcp_debug.h" 26 27 int tcpprintfs = 0; 28 int tcpcksum = 1; 29 struct tcpiphdr tcp_saveti; 30 extern tcpnodelack; 31 32 struct tcpcb *tcp_newtcpcb(); 33 /* 34 * TCP input routine, follows pages 65-76 of the 35 * protocol specification dated September, 1981 very closely. 36 */ 37 tcp_input(m0) 38 struct mbuf *m0; 39 { 40 register struct tcpiphdr *ti; 41 struct inpcb *inp; 42 register struct mbuf *m; 43 struct mbuf *om = 0; 44 int len, tlen, off; 45 register struct tcpcb *tp = 0; 46 register int tiflags; 47 struct socket *so; 48 int todrop, acked; 49 short ostate; 50 struct in_addr laddr; 51 int dropsocket = 0; 52 53 /* 54 * Get IP and TCP header together in first mbuf. 55 * Note: IP leaves IP header in first mbuf. 56 */ 57 m = m0; 58 ti = mtod(m, struct tcpiphdr *); 59 if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 60 ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 61 if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 62 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 63 tcpstat.tcps_hdrops++; 64 return; 65 } 66 ti = mtod(m, struct tcpiphdr *); 67 } 68 69 /* 70 * Checksum extended TCP header and data. 71 */ 72 tlen = ((struct ip *)ti)->ip_len; 73 len = sizeof (struct ip) + tlen; 74 if (tcpcksum) { 75 ti->ti_next = ti->ti_prev = 0; 76 ti->ti_x1 = 0; 77 ti->ti_len = (u_short)tlen; 78 ti->ti_len = htons((u_short)ti->ti_len); 79 if (ti->ti_sum = in_cksum(m, len)) { 80 tcpstat.tcps_badsum++; 81 if (tcpprintfs) 82 printf("tcp cksum %x\n", ti->ti_sum); 83 goto drop; 84 } 85 } 86 87 /* 88 * Check that TCP offset makes sense, 89 * pull out TCP options and adjust length. 90 */ 91 off = ti->ti_off << 2; 92 if (off < sizeof (struct tcphdr) || off > tlen) { 93 tcpstat.tcps_badoff++; 94 goto drop; 95 } 96 tlen -= off; 97 ti->ti_len = tlen; 98 if (off > sizeof (struct tcphdr)) { 99 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 100 tcpstat.tcps_hdrops++; 101 goto drop; 102 } 103 ti = mtod(m, struct tcpiphdr *); 104 om = m_get(M_DONTWAIT, MT_DATA); 105 if (om == 0) 106 goto drop; 107 om->m_len = off - sizeof (struct tcphdr); 108 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 109 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 110 m->m_len -= om->m_len; 111 bcopy(op+om->m_len, op, 112 (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 113 } 114 } 115 tiflags = ti->ti_flags; 116 117 /* 118 * Drop TCP and IP headers. 119 */ 120 off += sizeof (struct ip); 121 m->m_off += off; 122 m->m_len -= off; 123 124 /* 125 * Convert TCP protocol specific fields to host format. 126 */ 127 ti->ti_seq = ntohl(ti->ti_seq); 128 ti->ti_ack = ntohl(ti->ti_ack); 129 ti->ti_win = ntohs(ti->ti_win); 130 ti->ti_urp = ntohs(ti->ti_urp); 131 132 /* 133 * Locate pcb for segment. 134 */ 135 inp = in_pcblookup 136 (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 137 INPLOOKUP_WILDCARD); 138 139 /* 140 * If the state is CLOSED (i.e., TCB does not exist) then 141 * all data in the incoming segment is discarded. 142 */ 143 if (inp == 0) 144 goto dropwithreset; 145 tp = intotcpcb(inp); 146 if (tp == 0) 147 goto dropwithreset; 148 so = inp->inp_socket; 149 if (so->so_options & SO_DEBUG) { 150 ostate = tp->t_state; 151 tcp_saveti = *ti; 152 } 153 if (so->so_options & SO_ACCEPTCONN) { 154 so = sonewconn(so); 155 if (so == 0) 156 goto drop; 157 /* 158 * This is ugly, but .... 159 * 160 * Mark socket as temporary until we're 161 * committed to keeping it. The code at 162 * ``drop'' and ``dropwithreset'' check the 163 * flag dropsocket to see if the temporary 164 * socket created here should be discarded. 165 * We mark the socket as discardable until 166 * we're committed to it below in TCPS_LISTEN. 167 */ 168 dropsocket++; 169 inp = (struct inpcb *)so->so_pcb; 170 inp->inp_laddr = ti->ti_dst; 171 inp->inp_lport = ti->ti_dport; 172 tp = intotcpcb(inp); 173 tp->t_state = TCPS_LISTEN; 174 } 175 176 /* 177 * Segment received on connection. 178 * Reset idle time and keep-alive timer. 179 */ 180 tp->t_idle = 0; 181 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 182 183 /* 184 * Process options. 185 */ 186 if (om) { 187 tcp_dooptions(tp, om); 188 om = 0; 189 } 190 191 /* 192 * Calculate amount of space in receive window, 193 * and then do TCP input processing. 194 */ 195 tp->rcv_wnd = sbspace(&so->so_rcv); 196 if (tp->rcv_wnd < 0) 197 tp->rcv_wnd = 0; 198 199 switch (tp->t_state) { 200 201 /* 202 * If the state is LISTEN then ignore segment if it contains an RST. 203 * If the segment contains an ACK then it is bad and send a RST. 204 * If it does not contain a SYN then it is not interesting; drop it. 205 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 206 * tp->iss, and send a segment: 207 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 208 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 209 * Fill in remote peer address fields if not previously specified. 210 * Enter SYN_RECEIVED state, and process any other fields of this 211 * segment in this state. 212 */ 213 case TCPS_LISTEN: { 214 struct mbuf *am; 215 register struct sockaddr_in *sin; 216 217 if (tiflags & TH_RST) 218 goto drop; 219 if (tiflags & TH_ACK) 220 goto dropwithreset; 221 if ((tiflags & TH_SYN) == 0) 222 goto drop; 223 am = m_get(M_DONTWAIT, MT_SONAME); 224 if (am == NULL) 225 goto drop; 226 am->m_len = sizeof (struct sockaddr_in); 227 sin = mtod(am, struct sockaddr_in *); 228 sin->sin_family = AF_INET; 229 sin->sin_addr = ti->ti_src; 230 sin->sin_port = ti->ti_sport; 231 laddr = inp->inp_laddr; 232 if (inp->inp_laddr.s_addr == INADDR_ANY) 233 inp->inp_laddr = ti->ti_dst; 234 if (in_pcbconnect(inp, am)) { 235 inp->inp_laddr = laddr; 236 (void) m_free(am); 237 goto drop; 238 } 239 (void) m_free(am); 240 tp->t_template = tcp_template(tp); 241 if (tp->t_template == 0) { 242 in_pcbdisconnect(inp); 243 inp->inp_laddr = laddr; 244 tp = 0; 245 goto drop; 246 } 247 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 248 tp->irs = ti->ti_seq; 249 tcp_sendseqinit(tp); 250 tcp_rcvseqinit(tp); 251 tp->t_state = TCPS_SYN_RECEIVED; 252 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 253 dropsocket = 0; /* committed to socket */ 254 goto trimthenstep6; 255 } 256 257 /* 258 * If the state is SYN_SENT: 259 * if seg contains an ACK, but not for our SYN, drop the input. 260 * if seg contains a RST, then drop the connection. 261 * if seg does not contain SYN, then drop it. 262 * Otherwise this is an acceptable SYN segment 263 * initialize tp->rcv_nxt and tp->irs 264 * if seg contains ack then advance tp->snd_una 265 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 266 * arrange for segment to be acked (eventually) 267 * continue processing rest of data/controls, beginning with URG 268 */ 269 case TCPS_SYN_SENT: 270 if ((tiflags & TH_ACK) && 271 /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 272 (SEQ_LT(ti->ti_ack, tp->iss) || 273 SEQ_GT(ti->ti_ack, tp->snd_max))) 274 goto dropwithreset; 275 if (tiflags & TH_RST) { 276 if (tiflags & TH_ACK) 277 tp = tcp_drop(tp, ECONNREFUSED); 278 goto drop; 279 } 280 if ((tiflags & TH_SYN) == 0) 281 goto drop; 282 tp->snd_una = ti->ti_ack; 283 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 284 tp->snd_nxt = tp->snd_una; 285 tp->t_timer[TCPT_REXMT] = 0; 286 tp->irs = ti->ti_seq; 287 tcp_rcvseqinit(tp); 288 tp->t_flags |= TF_ACKNOW; 289 if (SEQ_GT(tp->snd_una, tp->iss)) { 290 soisconnected(so); 291 tp->t_state = TCPS_ESTABLISHED; 292 (void) tcp_reass(tp, (struct tcpiphdr *)0); 293 } else 294 tp->t_state = TCPS_SYN_RECEIVED; 295 goto trimthenstep6; 296 297 trimthenstep6: 298 /* 299 * Advance ti->ti_seq to correspond to first data byte. 300 * If data, trim to stay within window, 301 * dropping FIN if necessary. 302 */ 303 ti->ti_seq++; 304 if (ti->ti_len > tp->rcv_wnd) { 305 todrop = ti->ti_len - tp->rcv_wnd; 306 m_adj(m, -todrop); 307 ti->ti_len = tp->rcv_wnd; 308 ti->ti_flags &= ~TH_FIN; 309 } 310 tp->snd_wl1 = ti->ti_seq - 1; 311 goto step6; 312 } 313 314 /* 315 * States other than LISTEN or SYN_SENT. 316 * First check that at least some bytes of segment are within 317 * receive window. 318 */ 319 if (tp->rcv_wnd == 0) { 320 /* 321 * If window is closed can only take segments at 322 * window edge, and have to drop data and PUSH from 323 * incoming segments. 324 */ 325 if (tp->rcv_nxt != ti->ti_seq) 326 goto dropafterack; 327 if (ti->ti_len > 0) { 328 m_adj(m, ti->ti_len); 329 ti->ti_len = 0; 330 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 331 } 332 } else { 333 /* 334 * If segment begins before rcv_nxt, drop leading 335 * data (and SYN); if nothing left, just ack. 336 */ 337 todrop = tp->rcv_nxt - ti->ti_seq; 338 if (todrop > 0) { 339 if (tiflags & TH_SYN) { 340 tiflags &= ~TH_SYN; 341 ti->ti_flags &= ~TH_SYN; 342 ti->ti_seq++; 343 if (ti->ti_urp > 1) 344 ti->ti_urp--; 345 else 346 tiflags &= ~TH_URG; 347 todrop--; 348 } 349 if (todrop > ti->ti_len || 350 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 351 goto dropafterack; 352 m_adj(m, todrop); 353 ti->ti_seq += todrop; 354 ti->ti_len -= todrop; 355 if (ti->ti_urp > todrop) 356 ti->ti_urp -= todrop; 357 else { 358 tiflags &= ~TH_URG; 359 ti->ti_flags &= ~TH_URG; 360 ti->ti_urp = 0; 361 } 362 } 363 /* 364 * If segment ends after window, drop trailing data 365 * (and PUSH and FIN); if nothing left, just ACK. 366 */ 367 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 368 if (todrop > 0) { 369 if (todrop >= ti->ti_len) 370 goto dropafterack; 371 m_adj(m, -todrop); 372 ti->ti_len -= todrop; 373 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 374 } 375 } 376 377 /* 378 * If data is received on a connection after the 379 * user processes are gone, then RST the other end. 380 */ 381 if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT && 382 ti->ti_len) { 383 tp = tcp_close(tp); 384 goto dropwithreset; 385 } 386 387 /* 388 * If the RST bit is set examine the state: 389 * SYN_RECEIVED STATE: 390 * If passive open, return to LISTEN state. 391 * If active open, inform user that connection was refused. 392 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 393 * Inform user that connection was reset, and close tcb. 394 * CLOSING, LAST_ACK, TIME_WAIT STATES 395 * Close the tcb. 396 */ 397 if (tiflags&TH_RST) switch (tp->t_state) { 398 399 case TCPS_SYN_RECEIVED: 400 tp = tcp_drop(tp, ECONNREFUSED); 401 goto drop; 402 403 case TCPS_ESTABLISHED: 404 case TCPS_FIN_WAIT_1: 405 case TCPS_FIN_WAIT_2: 406 case TCPS_CLOSE_WAIT: 407 tp = tcp_drop(tp, ECONNRESET); 408 goto drop; 409 410 case TCPS_CLOSING: 411 case TCPS_LAST_ACK: 412 case TCPS_TIME_WAIT: 413 tp = tcp_close(tp); 414 goto drop; 415 } 416 417 /* 418 * If a SYN is in the window, then this is an 419 * error and we send an RST and drop the connection. 420 */ 421 if (tiflags & TH_SYN) { 422 tp = tcp_drop(tp, ECONNRESET); 423 goto dropwithreset; 424 } 425 426 /* 427 * If the ACK bit is off we drop the segment and return. 428 */ 429 if ((tiflags & TH_ACK) == 0) 430 goto drop; 431 432 /* 433 * Ack processing. 434 */ 435 switch (tp->t_state) { 436 437 /* 438 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 439 * ESTABLISHED state and continue processing, othewise 440 * send an RST. 441 */ 442 case TCPS_SYN_RECEIVED: 443 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 444 SEQ_GT(ti->ti_ack, tp->snd_max)) 445 goto dropwithreset; 446 tp->snd_una++; /* SYN acked */ 447 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 448 tp->snd_nxt = tp->snd_una; 449 tp->t_timer[TCPT_REXMT] = 0; 450 soisconnected(so); 451 tp->t_state = TCPS_ESTABLISHED; 452 (void) tcp_reass(tp, (struct tcpiphdr *)0); 453 tp->snd_wl1 = ti->ti_seq - 1; 454 /* fall into ... */ 455 456 /* 457 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 458 * ACKs. If the ack is in the range 459 * tp->snd_una < ti->ti_ack <= tp->snd_max 460 * then advance tp->snd_una to ti->ti_ack and drop 461 * data from the retransmission queue. If this ACK reflects 462 * more up to date window information we update our window information. 463 */ 464 case TCPS_ESTABLISHED: 465 case TCPS_FIN_WAIT_1: 466 case TCPS_FIN_WAIT_2: 467 case TCPS_CLOSE_WAIT: 468 case TCPS_CLOSING: 469 case TCPS_LAST_ACK: 470 case TCPS_TIME_WAIT: 471 #define ourfinisacked (acked > 0) 472 473 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 474 break; 475 if (SEQ_GT(ti->ti_ack, tp->snd_max)) 476 goto dropafterack; 477 acked = ti->ti_ack - tp->snd_una; 478 479 /* 480 * If transmit timer is running and timed sequence 481 * number was acked, update smoothed round trip time. 482 */ 483 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 484 if (tp->t_srtt == 0) 485 tp->t_srtt = tp->t_rtt; 486 else 487 tp->t_srtt = 488 tcp_alpha * tp->t_srtt + 489 (1 - tcp_alpha) * tp->t_rtt; 490 tp->t_rtt = 0; 491 } 492 493 if (ti->ti_ack == tp->snd_max) 494 tp->t_timer[TCPT_REXMT] = 0; 495 else { 496 TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 497 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 498 tp->t_rtt = 1; 499 tp->t_rxtshift = 0; 500 } 501 if (acked > so->so_snd.sb_cc) { 502 sbdrop(&so->so_snd, so->so_snd.sb_cc); 503 tp->snd_wnd -= so->so_snd.sb_cc; 504 } else { 505 sbdrop(&so->so_snd, acked); 506 tp->snd_wnd -= acked; 507 acked = 0; 508 } 509 if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 510 sowwakeup(so); 511 tp->snd_una = ti->ti_ack; 512 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 513 tp->snd_nxt = tp->snd_una; 514 515 switch (tp->t_state) { 516 517 /* 518 * In FIN_WAIT_1 STATE in addition to the processing 519 * for the ESTABLISHED state if our FIN is now acknowledged 520 * then enter FIN_WAIT_2. 521 */ 522 case TCPS_FIN_WAIT_1: 523 if (ourfinisacked) { 524 /* 525 * If we can't receive any more 526 * data, then closing user can proceed. 527 */ 528 if (so->so_state & SS_CANTRCVMORE) 529 soisdisconnected(so); 530 tp->t_state = TCPS_FIN_WAIT_2; 531 } 532 break; 533 534 /* 535 * In CLOSING STATE in addition to the processing for 536 * the ESTABLISHED state if the ACK acknowledges our FIN 537 * then enter the TIME-WAIT state, otherwise ignore 538 * the segment. 539 */ 540 case TCPS_CLOSING: 541 if (ourfinisacked) { 542 tp->t_state = TCPS_TIME_WAIT; 543 tcp_canceltimers(tp); 544 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 545 soisdisconnected(so); 546 } 547 break; 548 549 /* 550 * The only thing that can arrive in LAST_ACK state 551 * is an acknowledgment of our FIN. If our FIN is now 552 * acknowledged, delete the TCB, enter the closed state 553 * and return. 554 */ 555 case TCPS_LAST_ACK: 556 if (ourfinisacked) 557 tp = tcp_close(tp); 558 goto drop; 559 560 /* 561 * In TIME_WAIT state the only thing that should arrive 562 * is a retransmission of the remote FIN. Acknowledge 563 * it and restart the finack timer. 564 */ 565 case TCPS_TIME_WAIT: 566 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 567 goto dropafterack; 568 } 569 #undef ourfinisacked 570 } 571 572 step6: 573 /* 574 * Update window information. 575 */ 576 if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 577 (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 578 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 579 tp->snd_wnd = ti->ti_win; 580 tp->snd_wl1 = ti->ti_seq; 581 tp->snd_wl2 = ti->ti_ack; 582 if (tp->snd_wnd != 0) 583 tp->t_timer[TCPT_PERSIST] = 0; 584 } 585 586 /* 587 * Process segments with URG. 588 */ 589 if ((tiflags & TH_URG) && ti->ti_urp && 590 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 591 /* 592 * If this segment advances the known urgent pointer, 593 * then mark the data stream. This should not happen 594 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 595 * a FIN has been received from the remote side. 596 * In these states we ignore the URG. 597 */ 598 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 599 tp->rcv_up = ti->ti_seq + ti->ti_urp; 600 so->so_oobmark = so->so_rcv.sb_cc + 601 (tp->rcv_up - tp->rcv_nxt) - 1; 602 if (so->so_oobmark == 0) 603 so->so_state |= SS_RCVATMARK; 604 sohasoutofband(so); 605 tp->t_oobflags &= ~TCPOOB_HAVEDATA; 606 } 607 /* 608 * Remove out of band data so doesn't get presented to user. 609 * This can happen independent of advancing the URG pointer, 610 * but if two URG's are pending at once, some out-of-band 611 * data may creep in... ick. 612 */ 613 if (ti->ti_urp <= ti->ti_len) 614 tcp_pulloutofband(so, ti); 615 } 616 617 /* 618 * Process the segment text, merging it into the TCP sequencing queue, 619 * and arranging for acknowledgment of receipt if necessary. 620 * This process logically involves adjusting tp->rcv_wnd as data 621 * is presented to the user (this happens in tcp_usrreq.c, 622 * case PRU_RCVD). If a FIN has already been received on this 623 * connection then we just ignore the text. 624 */ 625 if ((ti->ti_len || (tiflags&TH_FIN)) && 626 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 627 tiflags = tcp_reass(tp, ti); 628 if (tcpnodelack == 0) 629 tp->t_flags |= TF_DELACK; 630 else 631 tp->t_flags |= TF_ACKNOW; 632 } else { 633 m_freem(m); 634 tiflags &= ~TH_FIN; 635 } 636 637 /* 638 * If FIN is received ACK the FIN and let the user know 639 * that the connection is closing. 640 */ 641 if (tiflags & TH_FIN) { 642 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 643 socantrcvmore(so); 644 tp->t_flags |= TF_ACKNOW; 645 tp->rcv_nxt++; 646 } 647 switch (tp->t_state) { 648 649 /* 650 * In SYN_RECEIVED and ESTABLISHED STATES 651 * enter the CLOSE_WAIT state. 652 */ 653 case TCPS_SYN_RECEIVED: 654 case TCPS_ESTABLISHED: 655 tp->t_state = TCPS_CLOSE_WAIT; 656 break; 657 658 /* 659 * If still in FIN_WAIT_1 STATE FIN has not been acked so 660 * enter the CLOSING state. 661 */ 662 case TCPS_FIN_WAIT_1: 663 tp->t_state = TCPS_CLOSING; 664 break; 665 666 /* 667 * In FIN_WAIT_2 state enter the TIME_WAIT state, 668 * starting the time-wait timer, turning off the other 669 * standard timers. 670 */ 671 case TCPS_FIN_WAIT_2: 672 tp->t_state = TCPS_TIME_WAIT; 673 tcp_canceltimers(tp); 674 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 675 soisdisconnected(so); 676 break; 677 678 /* 679 * In TIME_WAIT state restart the 2 MSL time_wait timer. 680 */ 681 case TCPS_TIME_WAIT: 682 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 683 break; 684 } 685 } 686 if (so->so_options & SO_DEBUG) 687 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 688 689 /* 690 * Return any desired output. 691 */ 692 (void) tcp_output(tp); 693 return; 694 695 dropafterack: 696 /* 697 * Generate an ACK dropping incoming segment if it occupies 698 * sequence space, where the ACK reflects our state. 699 */ 700 if ((tiflags&TH_RST) || 701 tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 702 goto drop; 703 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 704 tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 705 tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 706 return; 707 708 dropwithreset: 709 if (om) 710 (void) m_free(om); 711 /* 712 * Generate a RST, dropping incoming segment. 713 * Make ACK acceptable to originator of segment. 714 */ 715 if (tiflags & TH_RST) 716 goto drop; 717 if (tiflags & TH_ACK) 718 tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 719 else { 720 if (tiflags & TH_SYN) 721 ti->ti_len++; 722 tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 723 TH_RST|TH_ACK); 724 } 725 /* destroy temporarily created socket */ 726 if (dropsocket) 727 (void) soabort(so); 728 return; 729 730 drop: 731 /* 732 * Drop space held by incoming segment and return. 733 */ 734 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 735 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 736 m_freem(m); 737 /* destroy temporarily created socket */ 738 if (dropsocket) 739 (void) soabort(so); 740 return; 741 } 742 743 tcp_dooptions(tp, om) 744 struct tcpcb *tp; 745 struct mbuf *om; 746 { 747 register u_char *cp; 748 int opt, optlen, cnt; 749 750 cp = mtod(om, u_char *); 751 cnt = om->m_len; 752 for (; cnt > 0; cnt -= optlen, cp += optlen) { 753 opt = cp[0]; 754 if (opt == TCPOPT_EOL) 755 break; 756 if (opt == TCPOPT_NOP) 757 optlen = 1; 758 else 759 optlen = cp[1]; 760 switch (opt) { 761 762 default: 763 break; 764 765 case TCPOPT_MAXSEG: 766 if (optlen != 4) 767 continue; 768 tp->t_maxseg = *(u_short *)(cp + 2); 769 tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 770 break; 771 } 772 } 773 (void) m_free(om); 774 } 775 776 /* 777 * Pull out of band byte out of a segment so 778 * it doesn't appear in the user's data queue. 779 * It is still reflected in the segment length for 780 * sequencing purposes. 781 */ 782 tcp_pulloutofband(so, ti) 783 struct socket *so; 784 struct tcpiphdr *ti; 785 { 786 register struct mbuf *m; 787 int cnt = ti->ti_urp - 1; 788 789 m = dtom(ti); 790 while (cnt >= 0) { 791 if (m->m_len > cnt) { 792 char *cp = mtod(m, caddr_t) + cnt; 793 struct tcpcb *tp = sototcpcb(so); 794 795 tp->t_iobc = *cp; 796 tp->t_oobflags |= TCPOOB_HAVEDATA; 797 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 798 m->m_len--; 799 return; 800 } 801 cnt -= m->m_len; 802 m = m->m_next; 803 if (m == 0) 804 break; 805 } 806 panic("tcp_pulloutofband"); 807 } 808 809 /* 810 * Insert segment ti into reassembly queue of tcp with 811 * control block tp. Return TH_FIN if reassembly now includes 812 * a segment with FIN. 813 */ 814 tcp_reass(tp, ti) 815 register struct tcpcb *tp; 816 register struct tcpiphdr *ti; 817 { 818 register struct tcpiphdr *q; 819 struct socket *so = tp->t_inpcb->inp_socket; 820 struct mbuf *m; 821 int flags; 822 823 /* 824 * Call with ti==0 after become established to 825 * force pre-ESTABLISHED data up to user socket. 826 */ 827 if (ti == 0) 828 goto present; 829 830 /* 831 * Find a segment which begins after this one does. 832 */ 833 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 834 q = (struct tcpiphdr *)q->ti_next) 835 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 836 break; 837 838 /* 839 * If there is a preceding segment, it may provide some of 840 * our data already. If so, drop the data from the incoming 841 * segment. If it provides all of our data, drop us. 842 */ 843 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 844 register int i; 845 q = (struct tcpiphdr *)q->ti_prev; 846 /* conversion to int (in i) handles seq wraparound */ 847 i = q->ti_seq + q->ti_len - ti->ti_seq; 848 if (i > 0) { 849 if (i >= ti->ti_len) 850 goto drop; 851 m_adj(dtom(ti), i); 852 ti->ti_len -= i; 853 ti->ti_seq += i; 854 } 855 q = (struct tcpiphdr *)(q->ti_next); 856 } 857 858 /* 859 * While we overlap succeeding segments trim them or, 860 * if they are completely covered, dequeue them. 861 */ 862 while (q != (struct tcpiphdr *)tp) { 863 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 864 if (i <= 0) 865 break; 866 if (i < q->ti_len) { 867 q->ti_seq += i; 868 q->ti_len -= i; 869 m_adj(dtom(q), i); 870 break; 871 } 872 q = (struct tcpiphdr *)q->ti_next; 873 m = dtom(q->ti_prev); 874 remque(q->ti_prev); 875 m_freem(m); 876 } 877 878 /* 879 * Stick new segment in its place. 880 */ 881 insque(ti, q->ti_prev); 882 883 present: 884 /* 885 * Present data to user, advancing rcv_nxt through 886 * completed sequence space. 887 */ 888 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 889 return (0); 890 ti = tp->seg_next; 891 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 892 return (0); 893 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 894 return (0); 895 do { 896 tp->rcv_nxt += ti->ti_len; 897 flags = ti->ti_flags & TH_FIN; 898 remque(ti); 899 m = dtom(ti); 900 ti = (struct tcpiphdr *)ti->ti_next; 901 if (so->so_state & SS_CANTRCVMORE) 902 m_freem(m); 903 else 904 sbappend(&so->so_rcv, m); 905 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 906 sorwakeup(so); 907 return (flags); 908 drop: 909 m_freem(dtom(ti)); 910 return (0); 911 } 912