1*5357Sroot /* tcp_input.c 1.46 82/01/07 */ 24601Swnj 34601Swnj #include "../h/param.h" 44601Swnj #include "../h/systm.h" 54663Swnj #include "../h/mbuf.h" 65085Swnj #include "../h/protosw.h" 74663Swnj #include "../h/socket.h" 84803Swnj #include "../h/socketvar.h" 95085Swnj #include "../net/in.h" 105085Swnj #include "../net/in_pcb.h" 115085Swnj #include "../net/in_systm.h" 125085Swnj #include "../net/if.h" 134803Swnj #include "../net/ip.h" 144899Swnj #include "../net/ip_var.h" 154803Swnj #include "../net/tcp.h" 164803Swnj #include "../net/tcp_fsm.h" 175085Swnj #include "../net/tcp_seq.h" 185085Swnj #include "../net/tcp_timer.h" 194803Swnj #include "../net/tcp_var.h" 205085Swnj #include "../net/tcpip.h" 215267Sroot #include "../net/tcp_debug.h" 225109Swnj #include "../errno.h" 234601Swnj 245300Sroot int tcpprintfs = 0; 254679Swnj int tcpcksum = 1; 265244Sroot struct sockaddr_in tcp_in = { AF_INET }; 275267Sroot struct tcpiphdr tcp_saveti; 284601Swnj 295267Sroot struct tcpcb *tcp_newtcpcb(); 305065Swnj /* 315065Swnj * TCP input routine, follows pages 65-76 of the 325065Swnj * protocol specification dated September, 1981 very closely. 335065Swnj */ 344924Swnj tcp_input(m0) 354924Swnj struct mbuf *m0; 364601Swnj { 374924Swnj register struct tcpiphdr *ti; 384924Swnj struct inpcb *inp; 394924Swnj register struct mbuf *m; 404924Swnj int len, tlen, off; 414924Swnj register struct tcpcb *tp; 424924Swnj register int tiflags; 434803Swnj struct socket *so; 445109Swnj int todrop, acked; 455267Sroot short ostate; 464924Swnj 474601Swnj COUNT(TCP_INPUT); 484924Swnj /* 495244Sroot * Get IP and TCP header together in first mbuf. 505244Sroot * Note: IP leaves IP header in first mbuf. 514924Swnj */ 524924Swnj m = m0; 535020Sroot ti = mtod(m, struct tcpiphdr *); 545244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 555208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 565307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 575307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 585085Swnj tcpstat.tcps_hdrops++; 595307Sroot return; 605085Swnj } 615085Swnj ti = mtod(m, struct tcpiphdr *); 625085Swnj } 634601Swnj 644601Swnj /* 655244Sroot * Checksum extended TCP header and data. 664601Swnj */ 674924Swnj tlen = ((struct ip *)ti)->ip_len; 684924Swnj len = sizeof (struct ip) + tlen; 694679Swnj if (tcpcksum) { 704924Swnj ti->ti_next = ti->ti_prev = 0; 714924Swnj ti->ti_x1 = 0; 725223Swnj ti->ti_len = (u_short)tlen; 735223Swnj #if vax 745223Swnj ti->ti_len = htons(ti->ti_len); 755223Swnj #endif 765231Swnj if (ti->ti_sum = in_cksum(m, len)) { 774924Swnj tcpstat.tcps_badsum++; 785065Swnj printf("tcp cksum %x\n", ti->ti_sum); 795085Swnj goto drop; 804601Swnj } 814601Swnj } 824601Swnj 834601Swnj /* 845244Sroot * Check that TCP offset makes sense, 855244Sroot * process TCP options and adjust length. 864601Swnj */ 874924Swnj off = ti->ti_off << 2; 885231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 894924Swnj tcpstat.tcps_badoff++; 905085Swnj goto drop; 914924Swnj } 924924Swnj ti->ti_len = tlen - off; 935085Swnj #if 0 945231Swnj if (off > sizeof (struct tcphdr)) 955085Swnj tcp_options(ti); 965085Swnj #endif 975065Swnj tiflags = ti->ti_flags; 984924Swnj 995231Swnj #if vax 1004924Swnj /* 1015244Sroot * Convert TCP protocol specific fields to host format. 1025085Swnj */ 1035085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1045085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1055085Swnj ti->ti_win = ntohs(ti->ti_win); 1065085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1075231Swnj #endif 1085085Swnj 1095085Swnj /* 1104924Swnj * Locate pcb for segment. 1114924Swnj */ 1125065Swnj inp = in_pcblookup 1135065Swnj (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 1145065Swnj 1155065Swnj /* 1165065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1175244Sroot * all data in the incoming segment is discarded. 1185065Swnj */ 1195300Sroot if (inp == 0) 1205085Swnj goto dropwithreset; 1215065Swnj tp = intotcpcb(inp); 1225300Sroot if (tp == 0) 1235085Swnj goto dropwithreset; 1245109Swnj so = inp->inp_socket; 1255267Sroot if (so->so_options & SO_DEBUG) { 1265267Sroot ostate = tp->t_state; 1275267Sroot tcp_saveti = *ti; 1285267Sroot } 1294601Swnj 1304601Swnj /* 1315162Swnj * Segment received on connection. 1325162Swnj * Reset idle time and keep-alive timer. 1335162Swnj */ 1345162Swnj tp->t_idle = 0; 1355162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1365162Swnj 1375162Swnj /* 1385085Swnj * Calculate amount of space in receive window, 1395085Swnj * and then do TCP input processing. 1404601Swnj */ 1415085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1425231Swnj if (tp->rcv_wnd < 0) 1435231Swnj tp->rcv_wnd = 0; 1444601Swnj 1454601Swnj switch (tp->t_state) { 1464601Swnj 1475065Swnj /* 1485065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1495065Swnj * If the segment contains an ACK then it is bad and send a RST. 1505065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1515085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1525065Swnj * tp->iss, and send a segment: 1535085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1545065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1555065Swnj * Fill in remote peer address fields if not previously specified. 1565065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1575244Sroot * segment in this state. 1585065Swnj */ 1595065Swnj case TCPS_LISTEN: 1605065Swnj if (tiflags & TH_RST) 1615065Swnj goto drop; 1625300Sroot if (tiflags & TH_ACK) 1635085Swnj goto dropwithreset; 1645300Sroot if ((tiflags & TH_SYN) == 0) 1655065Swnj goto drop; 1665244Sroot tcp_in.sin_addr = ti->ti_src; 1675244Sroot tcp_in.sin_port = ti->ti_sport; 1685300Sroot if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in)) 1695244Sroot goto drop; 1705244Sroot tp->t_template = tcp_template(tp); 1715244Sroot if (tp->t_template == 0) { 1725244Sroot in_pcbdisconnect(inp); 1735244Sroot goto drop; 1745244Sroot } 1755085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1765065Swnj tp->irs = ti->ti_seq; 1775085Swnj tcp_sendseqinit(tp); 1785085Swnj tcp_rcvseqinit(tp); 1795065Swnj tp->t_state = TCPS_SYN_RECEIVED; 1805244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1815085Swnj goto trimthenstep6; 1824601Swnj 1835065Swnj /* 1845065Swnj * If the state is SYN_SENT: 1855065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 1865065Swnj * if seg contains a RST, then drop the connection. 1875065Swnj * if seg does not contain SYN, then drop it. 1885065Swnj * Otherwise this is an acceptable SYN segment 1895065Swnj * initialize tp->rcv_nxt and tp->irs 1905065Swnj * if seg contains ack then advance tp->snd_una 1915065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1925065Swnj * arrange for segment to be acked (eventually) 1935065Swnj * continue processing rest of data/controls, beginning with URG 1945065Swnj */ 1955065Swnj case TCPS_SYN_SENT: 1965065Swnj if ((tiflags & TH_ACK) && 1975300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 1985300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 1995231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2005085Swnj goto dropwithreset; 2015065Swnj if (tiflags & TH_RST) { 2025065Swnj if (tiflags & TH_ACK) 2035267Sroot tcp_drop(tp, ECONNREFUSED); 2045065Swnj goto drop; 2054601Swnj } 2065065Swnj if ((tiflags & TH_SYN) == 0) 2075065Swnj goto drop; 2085231Swnj tp->snd_una = ti->ti_ack; 209*5357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 210*5357Sroot tp->snd_nxt = tp->snd_una; 2115244Sroot tp->t_timer[TCPT_REXMT] = 0; 2125065Swnj tp->irs = ti->ti_seq; 2135085Swnj tcp_rcvseqinit(tp); 2145085Swnj tp->t_flags |= TF_ACKNOW; 2155162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2165263Swnj so->so_state |= SS_CONNAWAITING; 2175244Sroot soisconnected(so); 2185065Swnj tp->t_state = TCPS_ESTABLISHED; 2195162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2205162Swnj } else 2215085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2225085Swnj goto trimthenstep6; 2235085Swnj 2245085Swnj trimthenstep6: 2255085Swnj /* 2265231Swnj * Advance ti->ti_seq to correspond to first data byte. 2275085Swnj * If data, trim to stay within window, 2285085Swnj * dropping FIN if necessary. 2295085Swnj */ 2305231Swnj ti->ti_seq++; 2315085Swnj if (ti->ti_len > tp->rcv_wnd) { 2325085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2335085Swnj m_adj(m, -todrop); 2345085Swnj ti->ti_len = tp->rcv_wnd; 2355085Swnj ti->ti_flags &= ~TH_FIN; 2365065Swnj } 2375263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2385085Swnj goto step6; 2395065Swnj } 2404601Swnj 2415065Swnj /* 2425065Swnj * States other than LISTEN or SYN_SENT. 2435065Swnj * First check that at least some bytes of segment are within 2445065Swnj * receive window. 2455065Swnj */ 2465065Swnj if (tp->rcv_wnd == 0) { 2475065Swnj /* 2485065Swnj * If window is closed can only take segments at 2495231Swnj * window edge, and have to drop data and PUSH from 2505065Swnj * incoming segments. 2515065Swnj */ 2525300Sroot if (tp->rcv_nxt != ti->ti_seq) 2535065Swnj goto dropafterack; 2545085Swnj if (ti->ti_len > 0) { 2555085Swnj ti->ti_len = 0; 2565085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2575065Swnj } 2585065Swnj } else { 2595065Swnj /* 2605231Swnj * If segment begins before rcv_nxt, drop leading 2615065Swnj * data (and SYN); if nothing left, just ack. 2625065Swnj */ 2635065Swnj if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 2645085Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2655085Swnj if (tiflags & TH_SYN) { 2665300Sroot tiflags &= ~TH_SYN; 2675085Swnj ti->ti_seq++; 2685085Swnj if (ti->ti_urp > 1) 2695085Swnj ti->ti_urp--; 2705085Swnj else 2715085Swnj tiflags &= ~TH_URG; 2725085Swnj todrop--; 2735085Swnj } 2745300Sroot if (todrop > ti->ti_len) 2755065Swnj goto dropafterack; 2765065Swnj m_adj(m, todrop); 2775065Swnj ti->ti_seq += todrop; 2785065Swnj ti->ti_len -= todrop; 2795085Swnj if (ti->ti_urp > todrop) 2805085Swnj ti->ti_urp -= todrop; 2815085Swnj else { 2825085Swnj tiflags &= ~TH_URG; 2835085Swnj /* ti->ti_flags &= ~TH_URG; */ 2845085Swnj /* ti->ti_urp = 0; */ 2855085Swnj } 2865085Swnj /* tiflags &= ~TH_SYN; */ 2875085Swnj /* ti->ti_flags &= ~TH_SYN; */ 2885065Swnj } 2895065Swnj /* 2905065Swnj * If segment ends after window, drop trailing data 2915085Swnj * (and PUSH and FIN); if nothing left, just ACK. 2925065Swnj */ 2935065Swnj if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 2945085Swnj todrop = 2955065Swnj ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 2965300Sroot if (todrop > ti->ti_len) 2975065Swnj goto dropafterack; 2985065Swnj m_adj(m, -todrop); 2995065Swnj ti->ti_len -= todrop; 3005085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3015065Swnj } 3025065Swnj } 3034601Swnj 3045065Swnj /* 3055065Swnj * If the RST bit is set examine the state: 3065065Swnj * SYN_RECEIVED STATE: 3075065Swnj * If passive open, return to LISTEN state. 3085065Swnj * If active open, inform user that connection was refused. 3095065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3105065Swnj * Inform user that connection was reset, and close tcb. 3115065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3125065Swnj * Close the tcb. 3135065Swnj */ 3145065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3155267Sroot 3165065Swnj case TCPS_SYN_RECEIVED: 3175065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3185267Sroot /* a miniature tcp_close, but invisible to user */ 3195267Sroot (void) m_free(dtom(tp->t_template)); 3205267Sroot (void) m_free(dtom(tp)); 3215267Sroot inp->inp_ppcb = 0; 3225267Sroot tp = tcp_newtcpcb(inp); 3235085Swnj tp->t_state = TCPS_LISTEN; 3245065Swnj goto drop; 3254601Swnj } 3265085Swnj tcp_drop(tp, ECONNREFUSED); 3275065Swnj goto drop; 3284601Swnj 3295065Swnj case TCPS_ESTABLISHED: 3305065Swnj case TCPS_FIN_WAIT_1: 3315065Swnj case TCPS_FIN_WAIT_2: 3325065Swnj case TCPS_CLOSE_WAIT: 3335065Swnj tcp_drop(tp, ECONNRESET); 3345065Swnj goto drop; 3355065Swnj 3365065Swnj case TCPS_CLOSING: 3375065Swnj case TCPS_LAST_ACK: 3385065Swnj case TCPS_TIME_WAIT: 3395065Swnj tcp_close(tp); 3405065Swnj goto drop; 3414601Swnj } 3424601Swnj 3434601Swnj /* 3445065Swnj * If a SYN is in the window, then this is an 3455065Swnj * error and we send an RST and drop the connection. 3464601Swnj */ 3475065Swnj if (tiflags & TH_SYN) { 3485231Swnj tcp_drop(tp, ECONNRESET); 3495085Swnj goto dropwithreset; 3504601Swnj } 3514601Swnj 3524601Swnj /* 3535065Swnj * If the ACK bit is off we drop the segment and return. 3544601Swnj */ 3555085Swnj if ((tiflags & TH_ACK) == 0) 3565065Swnj goto drop; 3575065Swnj 3585065Swnj /* 3595065Swnj * Ack processing. 3605065Swnj */ 3614601Swnj switch (tp->t_state) { 3624601Swnj 3635065Swnj /* 3645065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3655065Swnj * ESTABLISHED state and continue processing, othewise 3665065Swnj * send an RST. 3675065Swnj */ 3685065Swnj case TCPS_SYN_RECEIVED: 3695085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 3705231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 3715085Swnj goto dropwithreset; 3725244Sroot tp->snd_una++; /* SYN acked */ 373*5357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 374*5357Sroot tp->snd_nxt = tp->snd_una; 3755244Sroot tp->t_timer[TCPT_REXMT] = 0; 3765263Swnj so->so_state |= SS_CONNAWAITING; 3775085Swnj soisconnected(so); 3785085Swnj tp->t_state = TCPS_ESTABLISHED; 3795162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 3805244Sroot tp->snd_wl1 = ti->ti_seq - 1; 3815085Swnj /* fall into ... */ 3824601Swnj 3835065Swnj /* 3845065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 3855065Swnj * ACKs. If the ack is in the range 3865231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 3875065Swnj * then advance tp->snd_una to ti->ti_ack and drop 3885065Swnj * data from the retransmission queue. If this ACK reflects 3895065Swnj * more up to date window information we update our window information. 3905065Swnj */ 3915065Swnj case TCPS_ESTABLISHED: 3925065Swnj case TCPS_FIN_WAIT_1: 3935065Swnj case TCPS_FIN_WAIT_2: 3945065Swnj case TCPS_CLOSE_WAIT: 3955065Swnj case TCPS_CLOSING: 3965244Sroot case TCPS_LAST_ACK: 3975244Sroot case TCPS_TIME_WAIT: 3985085Swnj #define ourfinisacked (acked > 0) 3995085Swnj 4005244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4015065Swnj break; 4025300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4035065Swnj goto dropafterack; 4045085Swnj acked = ti->ti_ack - tp->snd_una; 4055307Sroot if (ti->ti_ack == tp->snd_max) 4065244Sroot tp->t_timer[TCPT_REXMT] = 0; 4075307Sroot else { 4085244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4095244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4105300Sroot tp->t_rtt = 0; 4115300Sroot tp->t_rxtshift = 0; 4125085Swnj } 4135307Sroot if (acked > so->so_snd.sb_cc) { 4145307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4155307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4165307Sroot } else { 4175307Sroot sbdrop(&so->so_snd.sb_cc, acked); 4185307Sroot tp->snd_wnd -= acked; 4195307Sroot acked = 0; 4205307Sroot } 4215300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4225300Sroot sowwakeup(so); 4235231Swnj tp->snd_una = ti->ti_ack; 424*5357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 425*5357Sroot tp->snd_nxt = tp->snd_una; 4265162Swnj 4275162Swnj /* 4285162Swnj * If transmit timer is running and timed sequence 4295162Swnj * number was acked, update smoothed round trip time. 4305162Swnj */ 4315162Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4325244Sroot if (tp->t_srtt == 0) 4335244Sroot tp->t_srtt = tp->t_rtt; 4345244Sroot else 4355244Sroot tp->t_srtt = 4365244Sroot tcp_alpha * tp->t_srtt + 4375244Sroot (1 - tcp_alpha) * tp->t_rtt; 4385162Swnj tp->t_rtt = 0; 4395162Swnj } 4405162Swnj 4414601Swnj switch (tp->t_state) { 4424601Swnj 4435065Swnj /* 4445065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4455065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4465085Swnj * then enter FIN_WAIT_2. 4475065Swnj */ 4485065Swnj case TCPS_FIN_WAIT_1: 4495085Swnj if (ourfinisacked) 4505085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4514601Swnj break; 4524601Swnj 4535065Swnj /* 4545065Swnj * In CLOSING STATE in addition to the processing for 4555065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4565065Swnj * then enter the TIME-WAIT state, otherwise ignore 4575065Swnj * the segment. 4585065Swnj */ 4595065Swnj case TCPS_CLOSING: 4605244Sroot if (ourfinisacked) { 4615065Swnj tp->t_state = TCPS_TIME_WAIT; 4625244Sroot tcp_canceltimers(tp); 4635244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4645244Sroot soisdisconnected(so); 4655244Sroot } 4665244Sroot break; 4674601Swnj 4685065Swnj /* 4695085Swnj * The only thing that can arrive in LAST_ACK state 4705085Swnj * is an acknowledgment of our FIN. If our FIN is now 4715085Swnj * acknowledged, delete the TCB, enter the closed state 4725085Swnj * and return. 4735065Swnj */ 4745065Swnj case TCPS_LAST_ACK: 4755251Sroot if (ourfinisacked) 4765065Swnj tcp_close(tp); 4775065Swnj goto drop; 4784601Swnj 4795065Swnj /* 4805065Swnj * In TIME_WAIT state the only thing that should arrive 4815065Swnj * is a retransmission of the remote FIN. Acknowledge 4825065Swnj * it and restart the finack timer. 4835065Swnj */ 4845065Swnj case TCPS_TIME_WAIT: 4855162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4865065Swnj goto dropafterack; 4874601Swnj } 4885085Swnj #undef ourfinisacked 4895085Swnj } 4904601Swnj 4915065Swnj step6: 4925065Swnj /* 4935244Sroot * Update window information. 4945244Sroot */ 4955300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 4965300Sroot (SEQ_LEQ(tp->snd_wl2, ti->ti_ack) || 4975300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 4985244Sroot tp->snd_wnd = ti->ti_win; 4995244Sroot tp->snd_wl1 = ti->ti_seq; 5005244Sroot tp->snd_wl2 = ti->ti_ack; 5015244Sroot if (tp->snd_wnd > 0) 5025244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5035244Sroot } 5045244Sroot 5055244Sroot /* 5065065Swnj * If an URG bit is set in the segment and is greater than the 5075065Swnj * current known urgent pointer, then signal the user that the 5085244Sroot * remote side has out of band data. This should not happen 5095065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 5105065Swnj * a FIN has been received from the remote side. In these states 5115065Swnj * we ignore the URG. 5125065Swnj */ 5135085Swnj if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 5145085Swnj if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 5155065Swnj tp->rcv_up = ti->ti_urp; 5165085Swnj #if 0 5175244Sroot sohasoutofband(so); /* XXX */ 5185085Swnj #endif 5194601Swnj } 5204601Swnj 5214601Swnj /* 5225065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5235065Swnj * and arranging for acknowledgment of receipt if necessary. 5245065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5255065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5265065Swnj * case PRU_RCVD). If a FIN has already been received on this 5275065Swnj * connection then we just ignore the text. 5284601Swnj */ 5295263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 5305263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5315085Swnj off += sizeof (struct ip); /* drop IP header */ 5325085Swnj m->m_off += off; 5335085Swnj m->m_len -= off; 5345065Swnj tiflags = tcp_reass(tp, ti); 5355287Sroot { extern tcpdelack; 5365287Sroot if (tcpdelack) tp->t_flags |= TF_DELACK; else 5375085Swnj tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 5385287Sroot } 5395244Sroot } else { 5404924Swnj m_freem(m); 5415263Swnj tiflags &= ~TH_FIN; 5425244Sroot } 5434601Swnj 5444601Swnj /* 5455263Swnj * If FIN is received ACK the FIN and let the user know 5465263Swnj * that the connection is closing. 5474601Swnj */ 5485263Swnj if (tiflags & TH_FIN) { 5495244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5505244Sroot socantrcvmore(so); 5515244Sroot tp->t_flags |= TF_ACKNOW; 5525244Sroot tp->rcv_nxt++; 5535244Sroot } 5545065Swnj switch (tp->t_state) { 5554601Swnj 5565065Swnj /* 5575065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 5585065Swnj * enter the CLOSE_WAIT state. 5594884Swnj */ 5605065Swnj case TCPS_SYN_RECEIVED: 5615065Swnj case TCPS_ESTABLISHED: 5625065Swnj tp->t_state = TCPS_CLOSE_WAIT; 5635065Swnj break; 5644884Swnj 5655065Swnj /* 5665085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 5675085Swnj * enter the CLOSING state. 5684884Swnj */ 5695065Swnj case TCPS_FIN_WAIT_1: 5705085Swnj tp->t_state = TCPS_CLOSING; 5715065Swnj break; 5724601Swnj 5735065Swnj /* 5745065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 5755065Swnj * starting the time-wait timer, turning off the other 5765065Swnj * standard timers. 5775065Swnj */ 5785065Swnj case TCPS_FIN_WAIT_2: 5795244Sroot tp->t_state = TCPS_TIME_WAIT; 5805074Swnj tcp_canceltimers(tp); 5815162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5825244Sroot soisdisconnected(so); 5835065Swnj break; 5845065Swnj 5854884Swnj /* 5865065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 5874884Swnj */ 5885065Swnj case TCPS_TIME_WAIT: 5895162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5905065Swnj break; 5915085Swnj } 5924601Swnj } 5935267Sroot if (so->so_options & SO_DEBUG) 5945267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 5955085Swnj 5965085Swnj /* 5975085Swnj * Return any desired output. 5985085Swnj */ 5995085Swnj tcp_output(tp); 6005065Swnj return; 6015085Swnj 6025065Swnj dropafterack: 6035085Swnj /* 6045244Sroot * Generate an ACK dropping incoming segment. 6055085Swnj * Make ACK reflect our state. 6065085Swnj */ 6075085Swnj if (tiflags & TH_RST) 6085085Swnj goto drop; 6095085Swnj tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6105231Swnj return; 6115085Swnj 6125085Swnj dropwithreset: 6135085Swnj /* 6145244Sroot * Generate a RST, dropping incoming segment. 6155085Swnj * Make ACK acceptable to originator of segment. 6165085Swnj */ 6175085Swnj if (tiflags & TH_RST) 6185085Swnj goto drop; 6195085Swnj if (tiflags & TH_ACK) 6205109Swnj tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6215085Swnj else { 6225085Swnj if (tiflags & TH_SYN) 6235085Swnj ti->ti_len++; 6245109Swnj tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6255085Swnj } 6265231Swnj return; 6275085Swnj 6285065Swnj drop: 6295085Swnj /* 6305085Swnj * Drop space held by incoming segment and return. 6315085Swnj */ 6325065Swnj m_freem(m); 6335267Sroot return; 6345065Swnj } 6355065Swnj 6365065Swnj /* 6375065Swnj * Insert segment ti into reassembly queue of tcp with 6385065Swnj * control block tp. Return TH_FIN if reassembly now includes 6395065Swnj * a segment with FIN. 6405065Swnj */ 6415109Swnj tcp_reass(tp, ti) 6425065Swnj register struct tcpcb *tp; 6435065Swnj register struct tcpiphdr *ti; 6445065Swnj { 6455065Swnj register struct tcpiphdr *q; 6465085Swnj struct socket *so = tp->t_inpcb->inp_socket; 6475263Swnj struct mbuf *m; 6485263Swnj int flags; 6495085Swnj COUNT(TCP_REASS); 6505065Swnj 6515065Swnj /* 6525162Swnj * Call with ti==0 after become established to 6535162Swnj * force pre-ESTABLISHED data up to user socket. 6545065Swnj */ 6555162Swnj if (ti == 0) 6565065Swnj goto present; 6574601Swnj 6585065Swnj /* 6595065Swnj * Find a segment which begins after this one does. 6605065Swnj */ 6615065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 6625065Swnj q = (struct tcpiphdr *)q->ti_next) 6635065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 6645065Swnj break; 6654601Swnj 6665065Swnj /* 6675065Swnj * If there is a preceding segment, it may provide some of 6685065Swnj * our data already. If so, drop the data from the incoming 6695065Swnj * segment. If it provides all of our data, drop us. 6705065Swnj */ 6715065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 6725065Swnj register int i; 6735065Swnj q = (struct tcpiphdr *)(q->ti_prev); 6745065Swnj /* conversion to int (in i) handles seq wraparound */ 6755065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 6765065Swnj if (i > 0) { 6774924Swnj if (i >= ti->ti_len) 6785065Swnj goto drop; 6795065Swnj m_adj(dtom(tp), i); 6805065Swnj ti->ti_len -= i; 6814924Swnj ti->ti_seq += i; 6824601Swnj } 6835065Swnj q = (struct tcpiphdr *)(q->ti_next); 6845065Swnj } 6854601Swnj 6865065Swnj /* 6875065Swnj * While we overlap succeeding segments trim them or, 6885065Swnj * if they are completely covered, dequeue them. 6895065Swnj */ 6905065Swnj while (q != (struct tcpiphdr *)tp && 6915065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 6925065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 6935065Swnj if (i < q->ti_len) { 6945065Swnj q->ti_len -= i; 6955065Swnj m_adj(dtom(q), i); 6965065Swnj break; 6974601Swnj } 6985065Swnj q = (struct tcpiphdr *)q->ti_next; 6995065Swnj m_freem(dtom(q->ti_prev)); 7005065Swnj remque(q->ti_prev); 7015065Swnj } 7024601Swnj 7035065Swnj /* 7045065Swnj * Stick new segment in its place. 7055065Swnj */ 7065065Swnj insque(ti, q->ti_prev); 7074601Swnj 7085065Swnj present: 7095065Swnj /* 7105244Sroot * Present data to user, advancing rcv_nxt through 7115244Sroot * completed sequence space. 7125065Swnj */ 7135263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 7145244Sroot return (0); 7154924Swnj ti = tp->seg_next; 7165263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 7175263Swnj return (0); 7185263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 7195263Swnj return (0); 7205263Swnj do { 7215244Sroot tp->rcv_nxt += ti->ti_len; 7225244Sroot flags = ti->ti_flags & TH_FIN; 7234924Swnj remque(ti); 7245263Swnj m = dtom(ti); 7254924Swnj ti = (struct tcpiphdr *)ti->ti_next; 7265263Swnj if (so->so_state & SS_CANTRCVMORE) 7275263Swnj (void) m_freem(m); 7285263Swnj else 7295263Swnj sbappend(&so->so_rcv, m); 7305263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 7315263Swnj sorwakeup(so); 7325065Swnj return (flags); 7335065Swnj drop: 7345065Swnj m_freem(dtom(ti)); 7355263Swnj return (0); 7364601Swnj } 737