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