1 /* tcp_input.c 1.90 83/03/27 */ 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 goto drop; 82 } 83 } 84 85 /* 86 * Check that TCP offset makes sense, 87 * pull out TCP options and adjust length. 88 */ 89 off = ti->ti_off << 2; 90 if (off < sizeof (struct tcphdr) || off > tlen) { 91 tcpstat.tcps_badoff++; 92 goto drop; 93 } 94 tlen -= off; 95 ti->ti_len = tlen; 96 if (off > sizeof (struct tcphdr)) { 97 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 98 tcpstat.tcps_hdrops++; 99 return; 100 } 101 ti = mtod(m, struct tcpiphdr *); 102 om = m_get(M_DONTWAIT, MT_DATA); 103 if (om == 0) 104 goto drop; 105 om->m_len = off - sizeof (struct tcphdr); 106 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 107 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 108 m->m_len -= om->m_len; 109 bcopy(op+om->m_len, op, 110 (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 111 } 112 } 113 tiflags = ti->ti_flags; 114 115 /* 116 * Drop TCP and IP headers. 117 */ 118 off += sizeof (struct ip); 119 m->m_off += off; 120 m->m_len -= off; 121 122 /* 123 * Convert TCP protocol specific fields to host format. 124 */ 125 ti->ti_seq = ntohl(ti->ti_seq); 126 ti->ti_ack = ntohl(ti->ti_ack); 127 ti->ti_win = ntohs(ti->ti_win); 128 ti->ti_urp = ntohs(ti->ti_urp); 129 130 /* 131 * Locate pcb for segment. 132 */ 133 inp = in_pcblookup 134 (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 135 INPLOOKUP_WILDCARD); 136 137 /* 138 * If the state is CLOSED (i.e., TCB does not exist) then 139 * all data in the incoming segment is discarded. 140 */ 141 if (inp == 0) 142 goto dropwithreset; 143 tp = intotcpcb(inp); 144 if (tp == 0) 145 goto dropwithreset; 146 so = inp->inp_socket; 147 if (so->so_options & SO_DEBUG) { 148 ostate = tp->t_state; 149 tcp_saveti = *ti; 150 } 151 if (so->so_options & SO_ACCEPTCONN) { 152 so = sonewconn(so); 153 if (so == 0) 154 goto drop; 155 /* 156 * This is ugly, but .... 157 * 158 * Mark socket as temporary until we're 159 * committed to keeping it. The code at 160 * ``drop'' and ``dropwithreset'' check the 161 * flag dropsocket to see if the temporary 162 * socket created here should be discarded. 163 * We mark the socket as discardable until 164 * we're committed to it below in TCPS_LISTEN. 165 */ 166 dropsocket++; 167 inp = (struct inpcb *)so->so_pcb; 168 inp->inp_laddr = ti->ti_dst; 169 inp->inp_lport = ti->ti_dport; 170 tp = intotcpcb(inp); 171 tp->t_state = TCPS_LISTEN; 172 } 173 174 /* 175 * Segment received on connection. 176 * Reset idle time and keep-alive timer. 177 */ 178 tp->t_idle = 0; 179 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 180 181 /* 182 * Process options. 183 */ 184 if (om) { 185 tcp_dooptions(tp, om); 186 om = 0; 187 } 188 189 /* 190 * Calculate amount of space in receive window, 191 * and then do TCP input processing. 192 */ 193 tp->rcv_wnd = sbspace(&so->so_rcv); 194 if (tp->rcv_wnd < 0) 195 tp->rcv_wnd = 0; 196 197 switch (tp->t_state) { 198 199 /* 200 * If the state is LISTEN then ignore segment if it contains an RST. 201 * If the segment contains an ACK then it is bad and send a RST. 202 * If it does not contain a SYN then it is not interesting; drop it. 203 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 204 * tp->iss, and send a segment: 205 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 206 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 207 * Fill in remote peer address fields if not previously specified. 208 * Enter SYN_RECEIVED state, and process any other fields of this 209 * segment in this state. 210 */ 211 case TCPS_LISTEN: { 212 struct mbuf *am; 213 register struct sockaddr_in *sin; 214 215 if (tiflags & TH_RST) 216 goto drop; 217 if (tiflags & TH_ACK) 218 goto dropwithreset; 219 if ((tiflags & TH_SYN) == 0) 220 goto drop; 221 am = m_get(M_DONTWAIT, MT_SONAME); 222 if (am == NULL) 223 goto drop; 224 am->m_len = sizeof (struct sockaddr_in); 225 sin = mtod(am, struct sockaddr_in *); 226 sin->sin_family = AF_INET; 227 sin->sin_addr = ti->ti_src; 228 sin->sin_port = ti->ti_sport; 229 laddr = inp->inp_laddr; 230 if (inp->inp_laddr.s_addr == INADDR_ANY) 231 inp->inp_laddr = ti->ti_dst; 232 if (in_pcbconnect(inp, am)) { 233 inp->inp_laddr = laddr; 234 (void) m_free(am); 235 goto drop; 236 } 237 (void) m_free(am); 238 tp->t_template = tcp_template(tp); 239 if (tp->t_template == 0) { 240 in_pcbdisconnect(inp); 241 inp->inp_laddr = laddr; 242 tp = 0; 243 goto drop; 244 } 245 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 246 tp->irs = ti->ti_seq; 247 tcp_sendseqinit(tp); 248 tcp_rcvseqinit(tp); 249 tp->t_state = TCPS_SYN_RECEIVED; 250 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 251 dropsocket = 0; /* committed to socket */ 252 goto trimthenstep6; 253 } 254 255 /* 256 * If the state is SYN_SENT: 257 * if seg contains an ACK, but not for our SYN, drop the input. 258 * if seg contains a RST, then drop the connection. 259 * if seg does not contain SYN, then drop it. 260 * Otherwise this is an acceptable SYN segment 261 * initialize tp->rcv_nxt and tp->irs 262 * if seg contains ack then advance tp->snd_una 263 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 264 * arrange for segment to be acked (eventually) 265 * continue processing rest of data/controls, beginning with URG 266 */ 267 case TCPS_SYN_SENT: 268 if ((tiflags & TH_ACK) && 269 /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 270 (SEQ_LT(ti->ti_ack, tp->iss) || 271 SEQ_GT(ti->ti_ack, tp->snd_max))) 272 goto dropwithreset; 273 if (tiflags & TH_RST) { 274 if (tiflags & TH_ACK) 275 tp = tcp_drop(tp, ECONNREFUSED); 276 goto drop; 277 } 278 if ((tiflags & TH_SYN) == 0) 279 goto drop; 280 tp->snd_una = ti->ti_ack; 281 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 282 tp->snd_nxt = tp->snd_una; 283 tp->t_timer[TCPT_REXMT] = 0; 284 tp->irs = ti->ti_seq; 285 tcp_rcvseqinit(tp); 286 tp->t_flags |= TF_ACKNOW; 287 if (SEQ_GT(tp->snd_una, tp->iss)) { 288 soisconnected(so); 289 tp->t_state = TCPS_ESTABLISHED; 290 (void) tcp_reass(tp, (struct tcpiphdr *)0); 291 } else 292 tp->t_state = TCPS_SYN_RECEIVED; 293 goto trimthenstep6; 294 295 trimthenstep6: 296 /* 297 * Advance ti->ti_seq to correspond to first data byte. 298 * If data, trim to stay within window, 299 * dropping FIN if necessary. 300 */ 301 ti->ti_seq++; 302 if (ti->ti_len > tp->rcv_wnd) { 303 todrop = ti->ti_len - tp->rcv_wnd; 304 m_adj(m, -todrop); 305 ti->ti_len = tp->rcv_wnd; 306 ti->ti_flags &= ~TH_FIN; 307 } 308 tp->snd_wl1 = ti->ti_seq - 1; 309 goto step6; 310 } 311 312 /* 313 * States other than LISTEN or SYN_SENT. 314 * First check that at least some bytes of segment are within 315 * receive window. 316 */ 317 if (tp->rcv_wnd == 0) { 318 /* 319 * If window is closed can only take segments at 320 * window edge, and have to drop data and PUSH from 321 * incoming segments. 322 */ 323 if (tp->rcv_nxt != ti->ti_seq) 324 goto dropafterack; 325 if (ti->ti_len > 0) { 326 m_adj(m, ti->ti_len); 327 ti->ti_len = 0; 328 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 329 } 330 } else { 331 /* 332 * If segment begins before rcv_nxt, drop leading 333 * data (and SYN); if nothing left, just ack. 334 */ 335 todrop = tp->rcv_nxt - ti->ti_seq; 336 if (todrop > 0) { 337 if (tiflags & TH_SYN) { 338 tiflags &= ~TH_SYN; 339 ti->ti_flags &= ~TH_SYN; 340 ti->ti_seq++; 341 if (ti->ti_urp > 1) 342 ti->ti_urp--; 343 else 344 tiflags &= ~TH_URG; 345 todrop--; 346 } 347 if (todrop > ti->ti_len || 348 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 349 goto dropafterack; 350 m_adj(m, todrop); 351 ti->ti_seq += todrop; 352 ti->ti_len -= todrop; 353 if (ti->ti_urp > todrop) 354 ti->ti_urp -= todrop; 355 else { 356 tiflags &= ~TH_URG; 357 ti->ti_flags &= ~TH_URG; 358 ti->ti_urp = 0; 359 } 360 } 361 /* 362 * If segment ends after window, drop trailing data 363 * (and PUSH and FIN); if nothing left, just ACK. 364 */ 365 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 366 if (todrop > 0) { 367 if (todrop >= ti->ti_len) 368 goto dropafterack; 369 m_adj(m, -todrop); 370 ti->ti_len -= todrop; 371 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 372 } 373 } 374 375 /* 376 * If data is received on a connection after the 377 * user processes are gone, then RST the other end. 378 */ 379 if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT && 380 ti->ti_len) { 381 tp = tcp_close(tp); 382 goto dropwithreset; 383 } 384 385 /* 386 * If the RST bit is set examine the state: 387 * SYN_RECEIVED STATE: 388 * If passive open, return to LISTEN state. 389 * If active open, inform user that connection was refused. 390 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 391 * Inform user that connection was reset, and close tcb. 392 * CLOSING, LAST_ACK, TIME_WAIT STATES 393 * Close the tcb. 394 */ 395 if (tiflags&TH_RST) switch (tp->t_state) { 396 397 case TCPS_SYN_RECEIVED: 398 tp = tcp_drop(tp, ECONNREFUSED); 399 goto drop; 400 401 case TCPS_ESTABLISHED: 402 case TCPS_FIN_WAIT_1: 403 case TCPS_FIN_WAIT_2: 404 case TCPS_CLOSE_WAIT: 405 tp = tcp_drop(tp, ECONNRESET); 406 goto drop; 407 408 case TCPS_CLOSING: 409 case TCPS_LAST_ACK: 410 case TCPS_TIME_WAIT: 411 tp = tcp_close(tp); 412 goto drop; 413 } 414 415 /* 416 * If a SYN is in the window, then this is an 417 * error and we send an RST and drop the connection. 418 */ 419 if (tiflags & TH_SYN) { 420 tp = tcp_drop(tp, ECONNRESET); 421 goto dropwithreset; 422 } 423 424 /* 425 * If the ACK bit is off we drop the segment and return. 426 */ 427 if ((tiflags & TH_ACK) == 0) 428 goto drop; 429 430 /* 431 * Ack processing. 432 */ 433 switch (tp->t_state) { 434 435 /* 436 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 437 * ESTABLISHED state and continue processing, othewise 438 * send an RST. 439 */ 440 case TCPS_SYN_RECEIVED: 441 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 442 SEQ_GT(ti->ti_ack, tp->snd_max)) 443 goto dropwithreset; 444 tp->snd_una++; /* SYN acked */ 445 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 446 tp->snd_nxt = tp->snd_una; 447 tp->t_timer[TCPT_REXMT] = 0; 448 soisconnected(so); 449 tp->t_state = TCPS_ESTABLISHED; 450 (void) tcp_reass(tp, (struct tcpiphdr *)0); 451 tp->snd_wl1 = ti->ti_seq - 1; 452 /* fall into ... */ 453 454 /* 455 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 456 * ACKs. If the ack is in the range 457 * tp->snd_una < ti->ti_ack <= tp->snd_max 458 * then advance tp->snd_una to ti->ti_ack and drop 459 * data from the retransmission queue. If this ACK reflects 460 * more up to date window information we update our window information. 461 */ 462 case TCPS_ESTABLISHED: 463 case TCPS_FIN_WAIT_1: 464 case TCPS_FIN_WAIT_2: 465 case TCPS_CLOSE_WAIT: 466 case TCPS_CLOSING: 467 case TCPS_LAST_ACK: 468 case TCPS_TIME_WAIT: 469 #define ourfinisacked (acked > 0) 470 471 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 472 break; 473 if (SEQ_GT(ti->ti_ack, tp->snd_max)) 474 goto dropafterack; 475 acked = ti->ti_ack - tp->snd_una; 476 477 /* 478 * If transmit timer is running and timed sequence 479 * number was acked, update smoothed round trip time. 480 */ 481 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 482 if (tp->t_srtt == 0) 483 tp->t_srtt = tp->t_rtt; 484 else 485 tp->t_srtt = 486 tcp_alpha * tp->t_srtt + 487 (1 - tcp_alpha) * tp->t_rtt; 488 tp->t_rtt = 0; 489 } 490 491 if (ti->ti_ack == tp->snd_max) 492 tp->t_timer[TCPT_REXMT] = 0; 493 else { 494 TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 495 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 496 tp->t_rtt = 1; 497 tp->t_rxtshift = 0; 498 } 499 if (acked > so->so_snd.sb_cc) { 500 sbdrop(&so->so_snd, so->so_snd.sb_cc); 501 tp->snd_wnd -= so->so_snd.sb_cc; 502 } else { 503 sbdrop(&so->so_snd, acked); 504 tp->snd_wnd -= acked; 505 acked = 0; 506 } 507 if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 508 sowwakeup(so); 509 tp->snd_una = ti->ti_ack; 510 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 511 tp->snd_nxt = tp->snd_una; 512 513 switch (tp->t_state) { 514 515 /* 516 * In FIN_WAIT_1 STATE in addition to the processing 517 * for the ESTABLISHED state if our FIN is now acknowledged 518 * then enter FIN_WAIT_2. 519 */ 520 case TCPS_FIN_WAIT_1: 521 if (ourfinisacked) { 522 /* 523 * If we can't receive any more 524 * data, then closing user can proceed. 525 */ 526 if (so->so_state & SS_CANTRCVMORE) 527 soisdisconnected(so); 528 tp->t_state = TCPS_FIN_WAIT_2; 529 } 530 break; 531 532 /* 533 * In CLOSING STATE in addition to the processing for 534 * the ESTABLISHED state if the ACK acknowledges our FIN 535 * then enter the TIME-WAIT state, otherwise ignore 536 * the segment. 537 */ 538 case TCPS_CLOSING: 539 if (ourfinisacked) { 540 tp->t_state = TCPS_TIME_WAIT; 541 tcp_canceltimers(tp); 542 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 543 soisdisconnected(so); 544 } 545 break; 546 547 /* 548 * The only thing that can arrive in LAST_ACK state 549 * is an acknowledgment of our FIN. If our FIN is now 550 * acknowledged, delete the TCB, enter the closed state 551 * and return. 552 */ 553 case TCPS_LAST_ACK: 554 if (ourfinisacked) 555 tp = tcp_close(tp); 556 goto drop; 557 558 /* 559 * In TIME_WAIT state the only thing that should arrive 560 * is a retransmission of the remote FIN. Acknowledge 561 * it and restart the finack timer. 562 */ 563 case TCPS_TIME_WAIT: 564 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 565 goto dropafterack; 566 } 567 #undef ourfinisacked 568 } 569 570 step6: 571 /* 572 * Update window information. 573 */ 574 if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 575 (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 576 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 577 tp->snd_wnd = ti->ti_win; 578 tp->snd_wl1 = ti->ti_seq; 579 tp->snd_wl2 = ti->ti_ack; 580 if (tp->snd_wnd != 0) 581 tp->t_timer[TCPT_PERSIST] = 0; 582 } 583 584 /* 585 * Process segments with URG. 586 */ 587 if ((tiflags & TH_URG) && ti->ti_urp && 588 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 589 /* 590 * If this segment advances the known urgent pointer, 591 * then mark the data stream. This should not happen 592 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 593 * a FIN has been received from the remote side. 594 * In these states we ignore the URG. 595 */ 596 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 597 tp->rcv_up = ti->ti_seq + ti->ti_urp; 598 so->so_oobmark = so->so_rcv.sb_cc + 599 (tp->rcv_up - tp->rcv_nxt) - 1; 600 if (so->so_oobmark == 0) 601 so->so_state |= SS_RCVATMARK; 602 sohasoutofband(so); 603 tp->t_oobflags &= ~TCPOOB_HAVEDATA; 604 } 605 /* 606 * Remove out of band data so doesn't get presented to user. 607 * This can happen independent of advancing the URG pointer, 608 * but if two URG's are pending at once, some out-of-band 609 * data may creep in... ick. 610 */ 611 if (ti->ti_urp <= ti->ti_len) 612 tcp_pulloutofband(so, ti); 613 } 614 615 /* 616 * Process the segment text, merging it into the TCP sequencing queue, 617 * and arranging for acknowledgment of receipt if necessary. 618 * This process logically involves adjusting tp->rcv_wnd as data 619 * is presented to the user (this happens in tcp_usrreq.c, 620 * case PRU_RCVD). If a FIN has already been received on this 621 * connection then we just ignore the text. 622 */ 623 if ((ti->ti_len || (tiflags&TH_FIN)) && 624 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 625 tiflags = tcp_reass(tp, ti); 626 if (tcpnodelack == 0) 627 tp->t_flags |= TF_DELACK; 628 else 629 tp->t_flags |= TF_ACKNOW; 630 } else { 631 m_freem(m); 632 tiflags &= ~TH_FIN; 633 } 634 635 /* 636 * If FIN is received ACK the FIN and let the user know 637 * that the connection is closing. 638 */ 639 if (tiflags & TH_FIN) { 640 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 641 socantrcvmore(so); 642 tp->t_flags |= TF_ACKNOW; 643 tp->rcv_nxt++; 644 } 645 switch (tp->t_state) { 646 647 /* 648 * In SYN_RECEIVED and ESTABLISHED STATES 649 * enter the CLOSE_WAIT state. 650 */ 651 case TCPS_SYN_RECEIVED: 652 case TCPS_ESTABLISHED: 653 tp->t_state = TCPS_CLOSE_WAIT; 654 break; 655 656 /* 657 * If still in FIN_WAIT_1 STATE FIN has not been acked so 658 * enter the CLOSING state. 659 */ 660 case TCPS_FIN_WAIT_1: 661 tp->t_state = TCPS_CLOSING; 662 break; 663 664 /* 665 * In FIN_WAIT_2 state enter the TIME_WAIT state, 666 * starting the time-wait timer, turning off the other 667 * standard timers. 668 */ 669 case TCPS_FIN_WAIT_2: 670 tp->t_state = TCPS_TIME_WAIT; 671 tcp_canceltimers(tp); 672 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 673 soisdisconnected(so); 674 break; 675 676 /* 677 * In TIME_WAIT state restart the 2 MSL time_wait timer. 678 */ 679 case TCPS_TIME_WAIT: 680 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 681 break; 682 } 683 } 684 if (so->so_options & SO_DEBUG) 685 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 686 687 /* 688 * Return any desired output. 689 */ 690 (void) tcp_output(tp); 691 return; 692 693 dropafterack: 694 /* 695 * Generate an ACK dropping incoming segment if it occupies 696 * sequence space, where the ACK reflects our state. 697 */ 698 if ((tiflags&TH_RST) || 699 tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 700 goto drop; 701 if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 702 tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 703 tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 704 return; 705 706 dropwithreset: 707 if (om) 708 (void) m_free(om); 709 /* 710 * Generate a RST, dropping incoming segment. 711 * Make ACK acceptable to originator of segment. 712 */ 713 if (tiflags & TH_RST) 714 goto drop; 715 if (tiflags & TH_ACK) 716 tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 717 else { 718 if (tiflags & TH_SYN) 719 ti->ti_len++; 720 tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 721 TH_RST|TH_ACK); 722 } 723 /* destroy temporarily created socket */ 724 if (dropsocket) 725 (void) soabort(so); 726 return; 727 728 drop: 729 if (om) 730 (void) m_free(om); 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