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