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