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