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