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