1 /* tcp_input.c 1.61 82/03/15 */ 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 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 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 goto drop; 216 } 217 in_setsockaddr(inp); 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 goto drop; 248 } 249 if ((tiflags & TH_SYN) == 0) 250 goto drop; 251 tp->snd_una = ti->ti_ack; 252 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 253 tp->snd_nxt = tp->snd_una; 254 tp->t_timer[TCPT_REXMT] = 0; 255 tp->irs = ti->ti_seq; 256 tcp_rcvseqinit(tp); 257 tp->t_flags |= TF_ACKNOW; 258 if (SEQ_GT(tp->snd_una, tp->iss)) { 259 if (so->so_options & SO_ACCEPTCONN) 260 so->so_state |= SS_CONNAWAITING; 261 soisconnected(so); 262 tp->t_state = TCPS_ESTABLISHED; 263 (void) tcp_reass(tp, (struct tcpiphdr *)0); 264 } else 265 tp->t_state = TCPS_SYN_RECEIVED; 266 goto trimthenstep6; 267 268 trimthenstep6: 269 /* 270 * Advance ti->ti_seq to correspond to first data byte. 271 * If data, trim to stay within window, 272 * dropping FIN if necessary. 273 */ 274 ti->ti_seq++; 275 if (ti->ti_len > tp->rcv_wnd) { 276 todrop = ti->ti_len - tp->rcv_wnd; 277 m_adj(m, -todrop); 278 ti->ti_len = tp->rcv_wnd; 279 ti->ti_flags &= ~TH_FIN; 280 } 281 tp->snd_wl1 = ti->ti_seq - 1; 282 goto step6; 283 } 284 285 /* 286 * States other than LISTEN or SYN_SENT. 287 * First check that at least some bytes of segment are within 288 * receive window. 289 */ 290 if (tp->rcv_wnd == 0) { 291 /* 292 * If window is closed can only take segments at 293 * window edge, and have to drop data and PUSH from 294 * incoming segments. 295 */ 296 if (tp->rcv_nxt != ti->ti_seq) 297 goto dropafterack; 298 if (ti->ti_len > 0) { 299 m_adj(m, ti->ti_len); 300 ti->ti_len = 0; 301 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 302 } 303 } else { 304 /* 305 * If segment begins before rcv_nxt, drop leading 306 * data (and SYN); if nothing left, just ack. 307 */ 308 todrop = tp->rcv_nxt - ti->ti_seq; 309 if (todrop > 0) { 310 if (tiflags & TH_SYN) { 311 tiflags &= ~TH_SYN; 312 ti->ti_flags &= ~TH_SYN; 313 ti->ti_seq++; 314 if (ti->ti_urp > 1) 315 ti->ti_urp--; 316 else 317 tiflags &= ~TH_URG; 318 todrop--; 319 } 320 if (todrop > ti->ti_len || 321 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 322 goto dropafterack; 323 m_adj(m, todrop); 324 ti->ti_seq += todrop; 325 ti->ti_len -= todrop; 326 if (ti->ti_urp > todrop) 327 ti->ti_urp -= todrop; 328 else { 329 tiflags &= ~TH_URG; 330 ti->ti_flags &= ~TH_URG; 331 ti->ti_urp = 0; 332 } 333 } 334 /* 335 * If segment ends after window, drop trailing data 336 * (and PUSH and FIN); if nothing left, just ACK. 337 */ 338 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 339 if (todrop > 0) { 340 if (todrop >= ti->ti_len) 341 goto dropafterack; 342 m_adj(m, -todrop); 343 ti->ti_len -= todrop; 344 ti->ti_flags &= ~(TH_PUSH|TH_FIN); 345 } 346 } 347 348 /* 349 * If a segment is received on a connection after the 350 * user processes are gone, then RST the other end. 351 */ 352 if (so->so_state & SS_USERGONE) { 353 tcp_close(tp); 354 goto dropwithreset; 355 } 356 357 /* 358 * If the RST bit is set examine the state: 359 * SYN_RECEIVED STATE: 360 * If passive open, return to LISTEN state. 361 * If active open, inform user that connection was refused. 362 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 363 * Inform user that connection was reset, and close tcb. 364 * CLOSING, LAST_ACK, TIME_WAIT STATES 365 * Close the tcb. 366 */ 367 if (tiflags&TH_RST) switch (tp->t_state) { 368 369 case TCPS_SYN_RECEIVED: 370 if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 371 /* a miniature tcp_close, but invisible to user */ 372 (void) m_free(dtom(tp->t_template)); 373 (void) m_free(dtom(tp)); 374 inp->inp_ppcb = 0; 375 tp = tcp_newtcpcb(inp); 376 tp->t_state = TCPS_LISTEN; 377 inp->inp_faddr.s_addr = 0; 378 inp->inp_fport = 0; 379 inp->inp_laddr.s_addr = 0; /* not quite right */ 380 goto drop; 381 } 382 tcp_drop(tp, ECONNREFUSED); 383 goto drop; 384 385 case TCPS_ESTABLISHED: 386 case TCPS_FIN_WAIT_1: 387 case TCPS_FIN_WAIT_2: 388 case TCPS_CLOSE_WAIT: 389 tcp_drop(tp, ECONNRESET); 390 goto drop; 391 392 case TCPS_CLOSING: 393 case TCPS_LAST_ACK: 394 case TCPS_TIME_WAIT: 395 tcp_close(tp); 396 goto drop; 397 } 398 399 /* 400 * If a SYN is in the window, then this is an 401 * error and we send an RST and drop the connection. 402 */ 403 if (tiflags & TH_SYN) { 404 tcp_drop(tp, ECONNRESET); 405 goto dropwithreset; 406 } 407 408 /* 409 * If the ACK bit is off we drop the segment and return. 410 */ 411 if ((tiflags & TH_ACK) == 0) 412 goto drop; 413 414 /* 415 * Ack processing. 416 */ 417 switch (tp->t_state) { 418 419 /* 420 * In SYN_RECEIVED state if the ack ACKs our SYN then enter 421 * ESTABLISHED state and continue processing, othewise 422 * send an RST. 423 */ 424 case TCPS_SYN_RECEIVED: 425 if (SEQ_GT(tp->snd_una, ti->ti_ack) || 426 SEQ_GT(ti->ti_ack, tp->snd_max)) 427 goto dropwithreset; 428 tp->snd_una++; /* SYN acked */ 429 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 430 tp->snd_nxt = tp->snd_una; 431 tp->t_timer[TCPT_REXMT] = 0; 432 if (so->so_options & SO_ACCEPTCONN) 433 so->so_state |= SS_CONNAWAITING; 434 soisconnected(so); 435 tp->t_state = TCPS_ESTABLISHED; 436 (void) tcp_reass(tp, (struct tcpiphdr *)0); 437 tp->snd_wl1 = ti->ti_seq - 1; 438 /* fall into ... */ 439 440 /* 441 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 442 * ACKs. If the ack is in the range 443 * tp->snd_una < ti->ti_ack <= tp->snd_max 444 * then advance tp->snd_una to ti->ti_ack and drop 445 * data from the retransmission queue. If this ACK reflects 446 * more up to date window information we update our window information. 447 */ 448 case TCPS_ESTABLISHED: 449 case TCPS_FIN_WAIT_1: 450 case TCPS_FIN_WAIT_2: 451 case TCPS_CLOSE_WAIT: 452 case TCPS_CLOSING: 453 case TCPS_LAST_ACK: 454 case TCPS_TIME_WAIT: 455 #define ourfinisacked (acked > 0) 456 457 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 458 break; 459 if (SEQ_GT(ti->ti_ack, tp->snd_max)) 460 goto dropafterack; 461 acked = ti->ti_ack - tp->snd_una; 462 463 /* 464 * If transmit timer is running and timed sequence 465 * number was acked, update smoothed round trip time. 466 */ 467 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 468 if (tp->t_srtt == 0) 469 tp->t_srtt = tp->t_rtt; 470 else 471 tp->t_srtt = 472 tcp_alpha * tp->t_srtt + 473 (1 - tcp_alpha) * tp->t_rtt; 474 /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */ 475 tp->t_rtt = 0; 476 } 477 478 if (ti->ti_ack == tp->snd_max) 479 tp->t_timer[TCPT_REXMT] = 0; 480 else { 481 TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 482 tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 483 tp->t_rtt = 1; 484 tp->t_rxtshift = 0; 485 } 486 if (acked > so->so_snd.sb_cc) { 487 sbdrop(&so->so_snd, so->so_snd.sb_cc); 488 tp->snd_wnd -= so->so_snd.sb_cc; 489 } else { 490 sbdrop(&so->so_snd, acked); 491 tp->snd_wnd -= acked; 492 acked = 0; 493 } 494 if (so->so_snd.sb_flags & SB_WAIT) 495 sowwakeup(so); 496 tp->snd_una = ti->ti_ack; 497 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 498 tp->snd_nxt = tp->snd_una; 499 500 switch (tp->t_state) { 501 502 /* 503 * In FIN_WAIT_1 STATE in addition to the processing 504 * for the ESTABLISHED state if our FIN is now acknowledged 505 * then enter FIN_WAIT_2. 506 */ 507 case TCPS_FIN_WAIT_1: 508 if (ourfinisacked) { 509 /* 510 * If we can't receive any more 511 * data, then closing user can proceed. 512 */ 513 if (so->so_state & SS_CANTRCVMORE) 514 soisdisconnected(so); 515 tp->t_state = TCPS_FIN_WAIT_2; 516 } 517 break; 518 519 /* 520 * In CLOSING STATE in addition to the processing for 521 * the ESTABLISHED state if the ACK acknowledges our FIN 522 * then enter the TIME-WAIT state, otherwise ignore 523 * the segment. 524 */ 525 case TCPS_CLOSING: 526 if (ourfinisacked) { 527 tp->t_state = TCPS_TIME_WAIT; 528 tcp_canceltimers(tp); 529 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 530 soisdisconnected(so); 531 } 532 break; 533 534 /* 535 * The only thing that can arrive in LAST_ACK state 536 * is an acknowledgment of our FIN. If our FIN is now 537 * acknowledged, delete the TCB, enter the closed state 538 * and return. 539 */ 540 case TCPS_LAST_ACK: 541 if (ourfinisacked) 542 tcp_close(tp); 543 goto drop; 544 545 /* 546 * In TIME_WAIT state the only thing that should arrive 547 * is a retransmission of the remote FIN. Acknowledge 548 * it and restart the finack timer. 549 */ 550 case TCPS_TIME_WAIT: 551 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 552 goto dropafterack; 553 } 554 #undef ourfinisacked 555 } 556 557 step6: 558 /* 559 * Update window information. 560 */ 561 if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 562 (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 563 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 564 tp->snd_wnd = ti->ti_win; 565 tp->snd_wl1 = ti->ti_seq; 566 tp->snd_wl2 = ti->ti_ack; 567 if (tp->snd_wnd > 0) 568 tp->t_timer[TCPT_PERSIST] = 0; 569 } 570 571 /* 572 * Process segments with URG. 573 */ 574 if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 575 /* 576 * If this segment advances the known urgent pointer, 577 * then mark the data stream. This should not happen 578 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 579 * a FIN has been received from the remote side. 580 * In these states we ignore the URG. 581 */ 582 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 583 tp->rcv_up = ti->ti_seq + ti->ti_urp; 584 so->so_oobmark = so->so_rcv.sb_cc + 585 (tp->rcv_up - tp->rcv_nxt) - 1; 586 if (so->so_oobmark == 0) 587 so->so_state |= SS_RCVATMARK; 588 #ifdef TCPTRUEOOB 589 if ((tp->t_flags & TF_DOOOB) == 0) 590 #endif 591 sohasoutofband(so); 592 tp->t_oobflags &= ~TCPOOB_HAVEDATA; 593 } 594 /* 595 * Remove out of band data so doesn't get presented to user. 596 * This can happen independent of advancing the URG pointer, 597 * but if two URG's are pending at once, some out-of-band 598 * data may creep in... ick. 599 */ 600 if (ti->ti_urp <= ti->ti_len) { 601 tcp_pulloutofband(so, ti); 602 } 603 } 604 605 /* 606 * Process the segment text, merging it into the TCP sequencing queue, 607 * and arranging for acknowledgment of receipt if necessary. 608 * This process logically involves adjusting tp->rcv_wnd as data 609 * is presented to the user (this happens in tcp_usrreq.c, 610 * case PRU_RCVD). If a FIN has already been received on this 611 * connection then we just ignore the text. 612 */ 613 if ((ti->ti_len || (tiflags&TH_FIN)) && 614 TCPS_HAVERCVDFIN(tp->t_state) == 0) { 615 tiflags = tcp_reass(tp, ti); 616 if (tcpnodelack == 0) 617 tp->t_flags |= TF_DELACK; 618 else 619 tp->t_flags |= TF_ACKNOW; 620 } else { 621 m_freem(m); 622 tiflags &= ~TH_FIN; 623 } 624 625 /* 626 * If FIN is received ACK the FIN and let the user know 627 * that the connection is closing. 628 */ 629 if (tiflags & TH_FIN) { 630 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 631 socantrcvmore(so); 632 tp->t_flags |= TF_ACKNOW; 633 tp->rcv_nxt++; 634 } 635 switch (tp->t_state) { 636 637 /* 638 * In SYN_RECEIVED and ESTABLISHED STATES 639 * enter the CLOSE_WAIT state. 640 */ 641 case TCPS_SYN_RECEIVED: 642 case TCPS_ESTABLISHED: 643 tp->t_state = TCPS_CLOSE_WAIT; 644 break; 645 646 /* 647 * If still in FIN_WAIT_1 STATE FIN has not been acked so 648 * enter the CLOSING state. 649 */ 650 case TCPS_FIN_WAIT_1: 651 tp->t_state = TCPS_CLOSING; 652 break; 653 654 /* 655 * In FIN_WAIT_2 state enter the TIME_WAIT state, 656 * starting the time-wait timer, turning off the other 657 * standard timers. 658 */ 659 case TCPS_FIN_WAIT_2: 660 tp->t_state = TCPS_TIME_WAIT; 661 tcp_canceltimers(tp); 662 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 663 soisdisconnected(so); 664 break; 665 666 /* 667 * In TIME_WAIT state restart the 2 MSL time_wait timer. 668 */ 669 case TCPS_TIME_WAIT: 670 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 671 break; 672 } 673 } 674 if (so->so_options & SO_DEBUG) 675 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 676 677 /* 678 * Return any desired output. 679 */ 680 (void) tcp_output(tp); 681 return; 682 683 dropafterack: 684 /* 685 * Generate an ACK dropping incoming segment if it occupies 686 * sequence space, where the ACK reflects our state. 687 */ 688 if ((tiflags&TH_RST) || 689 tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 690 goto drop; 691 tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 692 return; 693 694 dropwithreset: 695 if (om) 696 (void) m_free(om); 697 /* 698 * Generate a RST, dropping incoming segment. 699 * Make ACK acceptable to originator of segment. 700 */ 701 if (tiflags & TH_RST) 702 goto drop; 703 if (tiflags & TH_ACK) 704 tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 705 else { 706 if (tiflags & TH_SYN) 707 ti->ti_len++; 708 tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 709 TH_RST|TH_ACK); 710 } 711 return; 712 713 drop: 714 /* 715 * Drop space held by incoming segment and return. 716 */ 717 m_freem(m); 718 return; 719 } 720 721 tcp_dooptions(tp, om) 722 struct tcpcb *tp; 723 struct mbuf *om; 724 { 725 register u_char *cp; 726 int opt, optlen, cnt; 727 728 cp = mtod(om, u_char *); 729 cnt = om->m_len; 730 for (; cnt > 0; cnt -= optlen, cp += optlen) { 731 opt = cp[0]; 732 if (opt == TCPOPT_EOL) 733 break; 734 if (opt == TCPOPT_NOP) 735 optlen = 1; 736 else 737 optlen = cp[1]; 738 switch (opt) { 739 740 default: 741 break; 742 743 case TCPOPT_MAXSEG: 744 if (optlen != 4) 745 continue; 746 tp->t_maxseg = *(u_short *)(cp + 2); 747 #if vax 748 tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 749 #endif 750 break; 751 752 #ifdef TCPTRUEOOB 753 case TCPOPT_WILLOOB: 754 tp->t_flags |= TF_DOOOB; 755 printf("tp %x dooob\n", tp); 756 break; 757 758 case TCPOPT_OOBDATA: { 759 int seq; 760 register struct socket *so = tp->t_inpcb->inp_socket; 761 tcp_seq mark; 762 763 if (optlen != 8) 764 continue; 765 seq = cp[2]; 766 if (seq < tp->t_iobseq) 767 seq += 256; 768 printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 769 if (seq - tp->t_iobseq > 128) { 770 printf("bad seq\n"); 771 tp->t_oobflags |= TCPOOB_OWEACK; 772 break; 773 } 774 tp->t_iobseq = cp[2]; 775 tp->t_iobc = cp[3]; 776 mark = *(tcp_seq *)(cp + 4); 777 #if vax 778 mark = ntohl(mark); 779 #endif 780 so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 781 if (so->so_oobmark == 0) 782 so->so_state |= SS_RCVATMARK; 783 printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 784 sohasoutofband(so); 785 break; 786 } 787 788 case TCPOPT_OOBACK: { 789 int seq; 790 791 if (optlen != 4) 792 continue; 793 if (tp->t_oobseq != cp[2]) { 794 printf("wrong ack\n"); 795 break; 796 } 797 printf("take oob ack %x and cancel rexmt\n", cp[2]); 798 tp->t_oobflags &= ~TCPOOB_NEEDACK; 799 tp->t_timer[TCPT_OOBREXMT] = 0; 800 break; 801 } 802 #endif TCPTRUEOOB 803 } 804 } 805 (void) m_free(om); 806 } 807 808 /* 809 * Pull out of band byte out of a segment so 810 * it doesn't appear in the user's data queue. 811 * It is still reflected in the segment length for 812 * sequencing purposes. 813 */ 814 tcp_pulloutofband(so, ti) 815 struct socket *so; 816 struct tcpiphdr *ti; 817 { 818 register struct mbuf *m; 819 int cnt = ti->ti_urp - 1; 820 821 m = dtom(ti); 822 while (cnt >= 0) { 823 if (m->m_len > cnt) { 824 char *cp = mtod(m, caddr_t) + cnt; 825 struct tcpcb *tp = sototcpcb(so); 826 827 tp->t_iobc = *cp; 828 tp->t_oobflags |= TCPOOB_HAVEDATA; 829 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 830 m->m_len--; 831 return; 832 } 833 cnt -= m->m_len; 834 m = m->m_next; 835 if (m == 0) 836 break; 837 } 838 panic("tcp_pulloutofband"); 839 } 840 841 /* 842 * Insert segment ti into reassembly queue of tcp with 843 * control block tp. Return TH_FIN if reassembly now includes 844 * a segment with FIN. 845 */ 846 tcp_reass(tp, ti) 847 register struct tcpcb *tp; 848 register struct tcpiphdr *ti; 849 { 850 register struct tcpiphdr *q; 851 struct socket *so = tp->t_inpcb->inp_socket; 852 struct mbuf *m; 853 int flags; 854 COUNT(TCP_REASS); 855 856 /* 857 * Call with ti==0 after become established to 858 * force pre-ESTABLISHED data up to user socket. 859 */ 860 if (ti == 0) 861 goto present; 862 863 /* 864 * Find a segment which begins after this one does. 865 */ 866 for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 867 q = (struct tcpiphdr *)q->ti_next) 868 if (SEQ_GT(q->ti_seq, ti->ti_seq)) 869 break; 870 871 /* 872 * If there is a preceding segment, it may provide some of 873 * our data already. If so, drop the data from the incoming 874 * segment. If it provides all of our data, drop us. 875 */ 876 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 877 register int i; 878 q = (struct tcpiphdr *)q->ti_prev; 879 /* conversion to int (in i) handles seq wraparound */ 880 i = q->ti_seq + q->ti_len - ti->ti_seq; 881 if (i > 0) { 882 if (i >= ti->ti_len) 883 goto drop; 884 m_adj(dtom(tp), i); 885 ti->ti_len -= i; 886 ti->ti_seq += i; 887 } 888 q = (struct tcpiphdr *)(q->ti_next); 889 } 890 891 /* 892 * While we overlap succeeding segments trim them or, 893 * if they are completely covered, dequeue them. 894 */ 895 while (q != (struct tcpiphdr *)tp) { 896 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 897 if (i <= 0) 898 break; 899 if (i < q->ti_len) { 900 q->ti_seq += i; 901 q->ti_len -= i; 902 m_adj(dtom(q), i); 903 break; 904 } 905 q = (struct tcpiphdr *)q->ti_next; 906 m = dtom(q->ti_prev); 907 remque(q->ti_prev); 908 m_freem(m); 909 } 910 911 /* 912 * Stick new segment in its place. 913 */ 914 insque(ti, q->ti_prev); 915 916 present: 917 /* 918 * Present data to user, advancing rcv_nxt through 919 * completed sequence space. 920 */ 921 if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 922 return (0); 923 ti = tp->seg_next; 924 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 925 return (0); 926 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 927 return (0); 928 do { 929 tp->rcv_nxt += ti->ti_len; 930 flags = ti->ti_flags & TH_FIN; 931 remque(ti); 932 m = dtom(ti); 933 ti = (struct tcpiphdr *)ti->ti_next; 934 if (so->so_state & SS_CANTRCVMORE) 935 m_freem(m); 936 else 937 sbappend(&so->so_rcv, m); 938 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 939 sorwakeup(so); 940 return (flags); 941 drop: 942 m_freem(dtom(ti)); 943 return (0); 944 } 945