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