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