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