1 /* tcp_input.c 1.64 82/03/26 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/mbuf.h" 6 #include "../h/protosw.h" 7 #include "../h/socket.h" 8 #include "../h/socketvar.h" 9 #include "../net/in.h" 10 #include "../net/in_pcb.h" 11 #include "../net/in_systm.h" 12 #include "../net/if.h" 13 #include "../net/ip.h" 14 #include "../net/ip_var.h" 15 #include "../net/tcp.h" 16 #include "../net/tcp_fsm.h" 17 #include "../net/tcp_seq.h" 18 #include "../net/tcp_timer.h" 19 #include "../net/tcp_var.h" 20 #include "../net/tcpip.h" 21 #include "../net/tcp_debug.h" 22 #include "../errno.h" 23 24 int tcpprintfs = 0; 25 int tcpcksum = 1; 26 struct sockaddr_in tcp_in = { AF_INET }; 27 struct tcpiphdr tcp_saveti; 28 extern tcpnodelack; 29 30 struct tcpcb *tcp_newtcpcb(); 31 /* 32 * TCP input routine, follows pages 65-76 of the 33 * protocol specification dated September, 1981 very closely. 34 */ 35 tcp_input(m0) 36 struct mbuf *m0; 37 { 38 register struct tcpiphdr *ti; 39 struct inpcb *inp; 40 register struct mbuf *m; 41 struct mbuf *om = 0; 42 int len, tlen, off; 43 register struct tcpcb *tp = 0; 44 register int tiflags; 45 struct socket *so; 46 int todrop, acked; 47 short ostate; 48 struct in_addr laddr; 49 50 COUNT(TCP_INPUT); 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 in_setsockaddr(inp); 219 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 220 tp->irs = ti->ti_seq; 221 tcp_sendseqinit(tp); 222 tcp_rcvseqinit(tp); 223 tp->t_state = TCPS_SYN_RECEIVED; 224 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 225 goto trimthenstep6; 226 227 /* 228 * If the state is SYN_SENT: 229 * if seg contains an ACK, but not for our SYN, drop the input. 230 * if seg contains a RST, then drop the connection. 231 * if seg does not contain SYN, then drop it. 232 * Otherwise this is an acceptable SYN segment 233 * initialize tp->rcv_nxt and tp->irs 234 * if seg contains ack then advance tp->snd_una 235 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 236 * arrange for segment to be acked (eventually) 237 * continue processing rest of data/controls, beginning with URG 238 */ 239 case TCPS_SYN_SENT: 240 if ((tiflags & TH_ACK) && 241 /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 242 (SEQ_LT(ti->ti_ack, tp->iss) || 243 SEQ_GT(ti->ti_ack, tp->snd_max))) 244 goto dropwithreset; 245 if (tiflags & TH_RST) { 246 if (tiflags & TH_ACK) { 247 tcp_drop(tp, ECONNREFUSED); 248 tp = 0; 249 } 250 goto drop; 251 } 252 if ((tiflags & TH_SYN) == 0) 253 goto drop; 254 tp->snd_una = ti->ti_ack; 255 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 256 tp->snd_nxt = tp->snd_una; 257 tp->t_timer[TCPT_REXMT] = 0; 258 tp->irs = ti->ti_seq; 259 tcp_rcvseqinit(tp); 260 tp->t_flags |= TF_ACKNOW; 261 if (SEQ_GT(tp->snd_una, tp->iss)) { 262 if (so->so_options & SO_ACCEPTCONN) 263 so->so_state |= SS_CONNAWAITING; 264 soisconnected(so); 265 tp->t_state = TCPS_ESTABLISHED; 266 (void) tcp_reass(tp, (struct tcpiphdr *)0); 267 } else 268 tp->t_state = TCPS_SYN_RECEIVED; 269 goto trimthenstep6; 270 271 trimthenstep6: 272 /* 273 * Advance ti->ti_seq to correspond to first data byte. 274 * If data, trim to stay within window, 275 * dropping FIN if necessary. 276 */ 277 ti->ti_seq++; 278 if (ti->ti_len > tp->rcv_wnd) { 279 todrop = ti->ti_len - tp->rcv_wnd; 280 m_adj(m, -todrop); 281 ti->ti_len = tp->rcv_wnd; 282 ti->ti_flags &= ~TH_FIN; 283 } 284 tp->snd_wl1 = ti->ti_seq - 1; 285 goto step6; 286 } 287 288 /* 289 * States other than LISTEN or SYN_SENT. 290 * First check that at least some bytes of segment are within 291 * receive window. 292 */ 293 if (tp->rcv_wnd == 0) { 294 /* 295 * If window is closed can only take segments at 296 * window edge, and have to drop data and PUSH from 297 * incoming segments. 298 */ 299 if (tp->rcv_nxt != ti->ti_seq) 300 goto dropafterack; 301 if (ti->ti_len > 0) { 302 m_adj(m, ti->ti_len); 303 ti->ti_len = 0; 304 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 305 } 306 } else { 307 /* 308 * If segment begins before rcv_nxt, drop leading 309 * data (and SYN); if nothing left, just ack. 310 */ 311 todrop = tp->rcv_nxt - ti->ti_seq; 312 if (todrop > 0) { 313 if (tiflags & TH_SYN) { 314 tiflags &= ~TH_SYN; 315 ti->ti_flags &= ~TH_SYN; 316 ti->ti_seq++; 317 if (ti->ti_urp > 1) 318 ti->ti_urp--; 319 else 320 tiflags &= ~TH_URG; 321 todrop--; 322 } 323 if (todrop > ti->ti_len || 324 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 325 goto dropafterack; 326 m_adj(m, todrop); 327 ti->ti_seq += todrop; 328 ti->ti_len -= todrop; 329 if (ti->ti_urp > todrop) 330 ti->ti_urp -= todrop; 331 else { 332 tiflags &= ~TH_URG; 333 ti->ti_flags &= ~TH_URG; 334 ti->ti_urp = 0; 335 } 336 } 337 /* 338 * If segment ends after window, drop trailing data 339 * (and PUSH and FIN); if nothing left, just ACK. 340 */ 341 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 342 if (todrop > 0) { 343 if (todrop >= ti->ti_len) 344 goto dropafterack; 345 m_adj(m, -todrop); 346 ti->ti_len -= todrop; 347 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 348 } 349 } 350 351 /* 352 * If a segment is received on a connection after the 353 * user processes are gone, then RST the other end. 354 */ 355 if (so->so_state & SS_USERGONE) { 356 tcp_close(tp); 357 tp = 0; 358 goto dropwithreset; 359 } 360 361 /* 362 * If the RST bit is set examine the state: 363 * SYN_RECEIVED STATE: 364 * If passive open, return to LISTEN state. 365 * If active open, inform user that connection was refused. 366 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 367 * Inform user that connection was reset, and close tcb. 368 * CLOSING, LAST_ACK, TIME_WAIT STATES 369 * Close the tcb. 370 */ 371 if (tiflags&TH_RST) switch (tp->t_state) { 372 373 case TCPS_SYN_RECEIVED: 374 if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 375 /* a miniature tcp_close, but invisible to user */ 376 (void) m_free(dtom(tp->t_template)); 377 (void) m_free(dtom(tp)); 378 inp->inp_ppcb = 0; 379 tp = tcp_newtcpcb(inp); 380 tp->t_state = TCPS_LISTEN; 381 inp->inp_faddr.s_addr = 0; 382 inp->inp_fport = 0; 383 inp->inp_laddr.s_addr = 0; /* not quite right */ 384 tp = 0; 385 goto drop; 386 } 387 tcp_drop(tp, ECONNREFUSED); 388 tp = 0; 389 goto drop; 390 391 case TCPS_ESTABLISHED: 392 case TCPS_FIN_WAIT_1: 393 case TCPS_FIN_WAIT_2: 394 case TCPS_CLOSE_WAIT: 395 tcp_drop(tp, ECONNRESET); 396 tp = 0; 397 goto drop; 398 399 case TCPS_CLOSING: 400 case TCPS_LAST_ACK: 401 case TCPS_TIME_WAIT: 402 tcp_close(tp); 403 tp = 0; 404 goto drop; 405 } 406 407 /* 408 * If a SYN is in the window, then this is an 409 * error and we send an RST and drop the connection. 410 */ 411 if (tiflags & TH_SYN) { 412 tcp_drop(tp, ECONNRESET); 413 tp = 0; 414 goto dropwithreset; 415 } 416 417 /* 418 * If the ACK bit is off we drop the segment and return. 419 */ 420 if ((tiflags & TH_ACK) == 0) 421 goto drop; 422 423 /* 424 * Ack processing. 425 */ 426 switch (tp->t_state) { 427 428 /* 429 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 430 * ESTABLISHED state and continue processing, othewise 431 * send an RST. 432 */ 433 case TCPS_SYN_RECEIVED: 434 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 435 SEQ_GT(ti->ti_ack, tp->snd_max)) 436 goto dropwithreset; 437 tp->snd_una++; /* SYN acked */ 438 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 439 tp->snd_nxt = tp->snd_una; 440 tp->t_timer[TCPT_REXMT] = 0; 441 if (so->so_options & SO_ACCEPTCONN) 442 so->so_state |= SS_CONNAWAITING; 443 soisconnected(so); 444 tp->t_state = TCPS_ESTABLISHED; 445 (void) tcp_reass(tp, (struct tcpiphdr *)0); 446 tp->snd_wl1 = ti->ti_seq - 1; 447 /* fall into ... */ 448 449 /* 450 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 451 * ACKs. If the ack is in the range 452 * tp->snd_una < ti->ti_ack <= tp->snd_max 453 * then advance tp->snd_una to ti->ti_ack and drop 454 * data from the retransmission queue. If this ACK reflects 455 * more up to date window information we update our window information. 456 */ 457 case TCPS_ESTABLISHED: 458 case TCPS_FIN_WAIT_1: 459 case TCPS_FIN_WAIT_2: 460 case TCPS_CLOSE_WAIT: 461 case TCPS_CLOSING: 462 case TCPS_LAST_ACK: 463 case TCPS_TIME_WAIT: 464 #define ourfinisacked (acked > 0) 465 466 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 467 break; 468 if (SEQ_GT(ti->ti_ack, tp->snd_max)) 469 goto dropafterack; 470 acked = ti->ti_ack - tp->snd_una; 471 472 /* 473 * If transmit timer is running and timed sequence 474 * number was acked, update smoothed round trip time. 475 */ 476 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 477 if (tp->t_srtt == 0) 478 tp->t_srtt = tp->t_rtt; 479 else 480 tp->t_srtt = 481 tcp_alpha * tp->t_srtt + 482 (1 - tcp_alpha) * tp->t_rtt; 483 /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */ 484 tp->t_rtt = 0; 485 } 486 487 if (ti->ti_ack == tp->snd_max) 488 tp->t_timer[TCPT_REXMT] = 0; 489 else { 490 TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 491 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 492 tp->t_rtt = 1; 493 tp->t_rxtshift = 0; 494 } 495 if (acked > so->so_snd.sb_cc) { 496 sbdrop(&so->so_snd, so->so_snd.sb_cc); 497 tp->snd_wnd -= so->so_snd.sb_cc; 498 } else { 499 sbdrop(&so->so_snd, acked); 500 tp->snd_wnd -= acked; 501 acked = 0; 502 } 503 if (so->so_snd.sb_flags & SB_WAIT) 504 sowwakeup(so); 505 tp->snd_una = ti->ti_ack; 506 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 507 tp->snd_nxt = tp->snd_una; 508 509 switch (tp->t_state) { 510 511 /* 512 * In FIN_WAIT_1 STATE in addition to the processing 513 * for the ESTABLISHED state if our FIN is now acknowledged 514 * then enter FIN_WAIT_2. 515 */ 516 case TCPS_FIN_WAIT_1: 517 if (ourfinisacked) { 518 /* 519 * If we can't receive any more 520 * data, then closing user can proceed. 521 */ 522 if (so->so_state & SS_CANTRCVMORE) 523 soisdisconnected(so); 524 tp->t_state = TCPS_FIN_WAIT_2; 525 } 526 break; 527 528 /* 529 * In CLOSING STATE in addition to the processing for 530 * the ESTABLISHED state if the ACK acknowledges our FIN 531 * then enter the TIME-WAIT state, otherwise ignore 532 * the segment. 533 */ 534 case TCPS_CLOSING: 535 if (ourfinisacked) { 536 tp->t_state = TCPS_TIME_WAIT; 537 tcp_canceltimers(tp); 538 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 539 soisdisconnected(so); 540 } 541 break; 542 543 /* 544 * The only thing that can arrive in LAST_ACK state 545 * is an acknowledgment of our FIN. If our FIN is now 546 * acknowledged, delete the TCB, enter the closed state 547 * and return. 548 */ 549 case TCPS_LAST_ACK: 550 if (ourfinisacked) { 551 tcp_close(tp); 552 tp = 0; 553 } 554 goto drop; 555 556 /* 557 * In TIME_WAIT state the only thing that should arrive 558 * is a retransmission of the remote FIN. Acknowledge 559 * it and restart the finack timer. 560 */ 561 case TCPS_TIME_WAIT: 562 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 563 goto dropafterack; 564 } 565 #undef ourfinisacked 566 } 567 568 step6: 569 /* 570 * Update window information. 571 */ 572 if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 573 (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 574 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 575 tp->snd_wnd = ti->ti_win; 576 tp->snd_wl1 = ti->ti_seq; 577 tp->snd_wl2 = ti->ti_ack; 578 if (tp->snd_wnd > 0) 579 tp->t_timer[TCPT_PERSIST] = 0; 580 } 581 582 /* 583 * Process segments with URG. 584 */ 585 if ((tiflags & TH_URG) && 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 COUNT(TCP_REASS); 870 871 /* 872 * Call with ti==0 after become established to 873 * force pre-ESTABLISHED data up to user socket. 874 */ 875 if (ti == 0) 876 goto present; 877 878 /* 879 * Find a segment which begins after this one does. 880 */ 881 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 882 q = (struct tcpiphdr *)q->ti_next) 883 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 884 break; 885 886 /* 887 * If there is a preceding segment, it may provide some of 888 * our data already. If so, drop the data from the incoming 889 * segment. If it provides all of our data, drop us. 890 */ 891 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 892 register int i; 893 q = (struct tcpiphdr *)q->ti_prev; 894 /* conversion to int (in i) handles seq wraparound */ 895 i = q->ti_seq + q->ti_len - ti->ti_seq; 896 if (i > 0) { 897 if (i >= ti->ti_len) 898 goto drop; 899 m_adj(dtom(tp), i); 900 ti->ti_len -= i; 901 ti->ti_seq += i; 902 } 903 q = (struct tcpiphdr *)(q->ti_next); 904 } 905 906 /* 907 * While we overlap succeeding segments trim them or, 908 * if they are completely covered, dequeue them. 909 */ 910 while (q != (struct tcpiphdr *)tp) { 911 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 912 if (i <= 0) 913 break; 914 if (i < q->ti_len) { 915 q->ti_seq += i; 916 q->ti_len -= i; 917 m_adj(dtom(q), i); 918 break; 919 } 920 q = (struct tcpiphdr *)q->ti_next; 921 m = dtom(q->ti_prev); 922 remque(q->ti_prev); 923 m_freem(m); 924 } 925 926 /* 927 * Stick new segment in its place. 928 */ 929 insque(ti, q->ti_prev); 930 931 present: 932 /* 933 * Present data to user, advancing rcv_nxt through 934 * completed sequence space. 935 */ 936 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 937 return (0); 938 ti = tp->seg_next; 939 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 940 return (0); 941 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 942 return (0); 943 do { 944 tp->rcv_nxt += ti->ti_len; 945 flags = ti->ti_flags & TH_FIN; 946 remque(ti); 947 m = dtom(ti); 948 ti = (struct tcpiphdr *)ti->ti_next; 949 if (so->so_state & SS_CANTRCVMORE) 950 m_freem(m); 951 else 952 sbappend(&so->so_rcv, m); 953 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 954 sorwakeup(so); 955 return (flags); 956 drop: 957 m_freem(dtom(ti)); 958 return (0); 959 } 960