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