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