1*5391Swnj /* tcp_input.c 1.47 82/01/13 */ 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; 41*5391Swnj register struct tcpcb *tp = 0; 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; 2095357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2105357Sroot 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)) { 216*5391Swnj if (so->so_options & SO_ACCEPTCONN) 217*5391Swnj so->so_state |= SS_CONNAWAITING; 2185244Sroot soisconnected(so); 2195065Swnj tp->t_state = TCPS_ESTABLISHED; 2205162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2215162Swnj } else 2225085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2235085Swnj goto trimthenstep6; 2245085Swnj 2255085Swnj trimthenstep6: 2265085Swnj /* 2275231Swnj * Advance ti->ti_seq to correspond to first data byte. 2285085Swnj * If data, trim to stay within window, 2295085Swnj * dropping FIN if necessary. 2305085Swnj */ 2315231Swnj ti->ti_seq++; 2325085Swnj if (ti->ti_len > tp->rcv_wnd) { 2335085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2345085Swnj m_adj(m, -todrop); 2355085Swnj ti->ti_len = tp->rcv_wnd; 2365085Swnj ti->ti_flags &= ~TH_FIN; 2375065Swnj } 2385263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2395085Swnj goto step6; 2405065Swnj } 2414601Swnj 2425065Swnj /* 2435065Swnj * States other than LISTEN or SYN_SENT. 2445065Swnj * First check that at least some bytes of segment are within 2455065Swnj * receive window. 2465065Swnj */ 2475065Swnj if (tp->rcv_wnd == 0) { 2485065Swnj /* 2495065Swnj * If window is closed can only take segments at 2505231Swnj * window edge, and have to drop data and PUSH from 2515065Swnj * incoming segments. 2525065Swnj */ 2535300Sroot if (tp->rcv_nxt != ti->ti_seq) 2545065Swnj goto dropafterack; 2555085Swnj if (ti->ti_len > 0) { 2565085Swnj ti->ti_len = 0; 2575085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2585065Swnj } 2595065Swnj } else { 2605065Swnj /* 2615231Swnj * If segment begins before rcv_nxt, drop leading 2625065Swnj * data (and SYN); if nothing left, just ack. 2635065Swnj */ 2645065Swnj if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 2655085Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2665085Swnj if (tiflags & TH_SYN) { 2675300Sroot tiflags &= ~TH_SYN; 2685085Swnj ti->ti_seq++; 2695085Swnj if (ti->ti_urp > 1) 2705085Swnj ti->ti_urp--; 2715085Swnj else 2725085Swnj tiflags &= ~TH_URG; 2735085Swnj todrop--; 2745085Swnj } 2755300Sroot if (todrop > ti->ti_len) 2765065Swnj goto dropafterack; 2775065Swnj m_adj(m, todrop); 2785065Swnj ti->ti_seq += todrop; 2795065Swnj ti->ti_len -= todrop; 2805085Swnj if (ti->ti_urp > todrop) 2815085Swnj ti->ti_urp -= todrop; 2825085Swnj else { 2835085Swnj tiflags &= ~TH_URG; 2845085Swnj /* ti->ti_flags &= ~TH_URG; */ 2855085Swnj /* ti->ti_urp = 0; */ 2865085Swnj } 2875085Swnj /* tiflags &= ~TH_SYN; */ 2885085Swnj /* ti->ti_flags &= ~TH_SYN; */ 2895065Swnj } 2905065Swnj /* 2915065Swnj * If segment ends after window, drop trailing data 2925085Swnj * (and PUSH and FIN); if nothing left, just ACK. 2935065Swnj */ 2945065Swnj if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 2955085Swnj todrop = 2965065Swnj ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 2975300Sroot if (todrop > ti->ti_len) 2985065Swnj goto dropafterack; 2995065Swnj m_adj(m, -todrop); 3005065Swnj ti->ti_len -= todrop; 3015085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3025065Swnj } 3035065Swnj } 3044601Swnj 3055065Swnj /* 3065065Swnj * If the RST bit is set examine the state: 3075065Swnj * SYN_RECEIVED STATE: 3085065Swnj * If passive open, return to LISTEN state. 3095065Swnj * If active open, inform user that connection was refused. 3105065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3115065Swnj * Inform user that connection was reset, and close tcb. 3125065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3135065Swnj * Close the tcb. 3145065Swnj */ 3155065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3165267Sroot 3175065Swnj case TCPS_SYN_RECEIVED: 3185065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3195267Sroot /* a miniature tcp_close, but invisible to user */ 3205267Sroot (void) m_free(dtom(tp->t_template)); 3215267Sroot (void) m_free(dtom(tp)); 3225267Sroot inp->inp_ppcb = 0; 3235267Sroot tp = tcp_newtcpcb(inp); 3245085Swnj tp->t_state = TCPS_LISTEN; 3255065Swnj goto drop; 3264601Swnj } 3275085Swnj tcp_drop(tp, ECONNREFUSED); 3285065Swnj goto drop; 3294601Swnj 3305065Swnj case TCPS_ESTABLISHED: 3315065Swnj case TCPS_FIN_WAIT_1: 3325065Swnj case TCPS_FIN_WAIT_2: 3335065Swnj case TCPS_CLOSE_WAIT: 3345065Swnj tcp_drop(tp, ECONNRESET); 3355065Swnj goto drop; 3365065Swnj 3375065Swnj case TCPS_CLOSING: 3385065Swnj case TCPS_LAST_ACK: 3395065Swnj case TCPS_TIME_WAIT: 3405065Swnj tcp_close(tp); 3415065Swnj goto drop; 3424601Swnj } 3434601Swnj 3444601Swnj /* 3455065Swnj * If a SYN is in the window, then this is an 3465065Swnj * error and we send an RST and drop the connection. 3474601Swnj */ 3485065Swnj if (tiflags & TH_SYN) { 3495231Swnj tcp_drop(tp, ECONNRESET); 3505085Swnj goto dropwithreset; 3514601Swnj } 3524601Swnj 3534601Swnj /* 3545065Swnj * If the ACK bit is off we drop the segment and return. 3554601Swnj */ 3565085Swnj if ((tiflags & TH_ACK) == 0) 3575065Swnj goto drop; 3585065Swnj 3595065Swnj /* 3605065Swnj * Ack processing. 3615065Swnj */ 3624601Swnj switch (tp->t_state) { 3634601Swnj 3645065Swnj /* 3655065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3665065Swnj * ESTABLISHED state and continue processing, othewise 3675065Swnj * send an RST. 3685065Swnj */ 3695065Swnj case TCPS_SYN_RECEIVED: 3705085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 3715231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 3725085Swnj goto dropwithreset; 3735244Sroot tp->snd_una++; /* SYN acked */ 3745357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 3755357Sroot tp->snd_nxt = tp->snd_una; 3765244Sroot tp->t_timer[TCPT_REXMT] = 0; 377*5391Swnj if (so->so_options & SO_ACCEPTCONN) 378*5391Swnj so->so_state |= SS_CONNAWAITING; 3795085Swnj soisconnected(so); 3805085Swnj tp->t_state = TCPS_ESTABLISHED; 3815162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 3825244Sroot tp->snd_wl1 = ti->ti_seq - 1; 3835085Swnj /* fall into ... */ 3844601Swnj 3855065Swnj /* 3865065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 3875065Swnj * ACKs. If the ack is in the range 3885231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 3895065Swnj * then advance tp->snd_una to ti->ti_ack and drop 3905065Swnj * data from the retransmission queue. If this ACK reflects 3915065Swnj * more up to date window information we update our window information. 3925065Swnj */ 3935065Swnj case TCPS_ESTABLISHED: 3945065Swnj case TCPS_FIN_WAIT_1: 3955065Swnj case TCPS_FIN_WAIT_2: 3965065Swnj case TCPS_CLOSE_WAIT: 3975065Swnj case TCPS_CLOSING: 3985244Sroot case TCPS_LAST_ACK: 3995244Sroot case TCPS_TIME_WAIT: 4005085Swnj #define ourfinisacked (acked > 0) 4015085Swnj 4025244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4035065Swnj break; 4045300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4055065Swnj goto dropafterack; 4065085Swnj acked = ti->ti_ack - tp->snd_una; 4075307Sroot if (ti->ti_ack == tp->snd_max) 4085244Sroot tp->t_timer[TCPT_REXMT] = 0; 4095307Sroot else { 4105244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4115244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4125300Sroot tp->t_rtt = 0; 4135300Sroot tp->t_rxtshift = 0; 4145085Swnj } 4155307Sroot if (acked > so->so_snd.sb_cc) { 4165307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4175307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4185307Sroot } else { 4195307Sroot sbdrop(&so->so_snd.sb_cc, acked); 4205307Sroot tp->snd_wnd -= acked; 4215307Sroot acked = 0; 4225307Sroot } 4235300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4245300Sroot sowwakeup(so); 4255231Swnj tp->snd_una = ti->ti_ack; 4265357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4275357Sroot tp->snd_nxt = tp->snd_una; 4285162Swnj 4295162Swnj /* 4305162Swnj * If transmit timer is running and timed sequence 4315162Swnj * number was acked, update smoothed round trip time. 4325162Swnj */ 4335162Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4345244Sroot if (tp->t_srtt == 0) 4355244Sroot tp->t_srtt = tp->t_rtt; 4365244Sroot else 4375244Sroot tp->t_srtt = 4385244Sroot tcp_alpha * tp->t_srtt + 4395244Sroot (1 - tcp_alpha) * tp->t_rtt; 4405162Swnj tp->t_rtt = 0; 4415162Swnj } 4425162Swnj 4434601Swnj switch (tp->t_state) { 4444601Swnj 4455065Swnj /* 4465065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4475065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4485085Swnj * then enter FIN_WAIT_2. 4495065Swnj */ 4505065Swnj case TCPS_FIN_WAIT_1: 4515085Swnj if (ourfinisacked) 4525085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4534601Swnj break; 4544601Swnj 4555065Swnj /* 4565065Swnj * In CLOSING STATE in addition to the processing for 4575065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4585065Swnj * then enter the TIME-WAIT state, otherwise ignore 4595065Swnj * the segment. 4605065Swnj */ 4615065Swnj case TCPS_CLOSING: 4625244Sroot if (ourfinisacked) { 4635065Swnj tp->t_state = TCPS_TIME_WAIT; 4645244Sroot tcp_canceltimers(tp); 4655244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4665244Sroot soisdisconnected(so); 4675244Sroot } 4685244Sroot break; 4694601Swnj 4705065Swnj /* 4715085Swnj * The only thing that can arrive in LAST_ACK state 4725085Swnj * is an acknowledgment of our FIN. If our FIN is now 4735085Swnj * acknowledged, delete the TCB, enter the closed state 4745085Swnj * and return. 4755065Swnj */ 4765065Swnj case TCPS_LAST_ACK: 4775251Sroot if (ourfinisacked) 4785065Swnj tcp_close(tp); 4795065Swnj goto drop; 4804601Swnj 4815065Swnj /* 4825065Swnj * In TIME_WAIT state the only thing that should arrive 4835065Swnj * is a retransmission of the remote FIN. Acknowledge 4845065Swnj * it and restart the finack timer. 4855065Swnj */ 4865065Swnj case TCPS_TIME_WAIT: 4875162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4885065Swnj goto dropafterack; 4894601Swnj } 4905085Swnj #undef ourfinisacked 4915085Swnj } 4924601Swnj 4935065Swnj step6: 4945065Swnj /* 4955244Sroot * Update window information. 4965244Sroot */ 4975300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 498*5391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 4995300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 500*5391Swnj /* 501*5391Swnj printf("wl1 %x seq %x wl2 %x ack %x win %x wnd %x\n", tp->snd_wl1, ti->ti_seq, tp->snd_wl2, ti->ti_ack, ti->ti_win, tp->snd_wnd); 502*5391Swnj */ 5035244Sroot tp->snd_wnd = ti->ti_win; 5045244Sroot tp->snd_wl1 = ti->ti_seq; 5055244Sroot tp->snd_wl2 = ti->ti_ack; 5065244Sroot if (tp->snd_wnd > 0) 5075244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5085244Sroot } 5095244Sroot 5105244Sroot /* 5115065Swnj * If an URG bit is set in the segment and is greater than the 5125065Swnj * current known urgent pointer, then signal the user that the 5135244Sroot * remote side has out of band data. This should not happen 5145065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 5155065Swnj * a FIN has been received from the remote side. In these states 5165065Swnj * we ignore the URG. 5175065Swnj */ 5185085Swnj if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 5195085Swnj if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 5205065Swnj tp->rcv_up = ti->ti_urp; 5215085Swnj #if 0 5225244Sroot sohasoutofband(so); /* XXX */ 5235085Swnj #endif 5244601Swnj } 5254601Swnj 5264601Swnj /* 5275065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5285065Swnj * and arranging for acknowledgment of receipt if necessary. 5295065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5305065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5315065Swnj * case PRU_RCVD). If a FIN has already been received on this 5325065Swnj * connection then we just ignore the text. 5334601Swnj */ 5345263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 5355263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5365085Swnj off += sizeof (struct ip); /* drop IP header */ 5375085Swnj m->m_off += off; 5385085Swnj m->m_len -= off; 5395065Swnj tiflags = tcp_reass(tp, ti); 5405287Sroot { extern tcpdelack; 5415287Sroot if (tcpdelack) tp->t_flags |= TF_DELACK; else 5425085Swnj tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 5435287Sroot } 5445244Sroot } else { 5454924Swnj m_freem(m); 5465263Swnj tiflags &= ~TH_FIN; 5475244Sroot } 5484601Swnj 5494601Swnj /* 5505263Swnj * If FIN is received ACK the FIN and let the user know 5515263Swnj * that the connection is closing. 5524601Swnj */ 5535263Swnj if (tiflags & TH_FIN) { 5545244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5555244Sroot socantrcvmore(so); 5565244Sroot tp->t_flags |= TF_ACKNOW; 5575244Sroot tp->rcv_nxt++; 5585244Sroot } 5595065Swnj switch (tp->t_state) { 5604601Swnj 5615065Swnj /* 5625065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 5635065Swnj * enter the CLOSE_WAIT state. 5644884Swnj */ 5655065Swnj case TCPS_SYN_RECEIVED: 5665065Swnj case TCPS_ESTABLISHED: 5675065Swnj tp->t_state = TCPS_CLOSE_WAIT; 5685065Swnj break; 5694884Swnj 5705065Swnj /* 5715085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 5725085Swnj * enter the CLOSING state. 5734884Swnj */ 5745065Swnj case TCPS_FIN_WAIT_1: 5755085Swnj tp->t_state = TCPS_CLOSING; 5765065Swnj break; 5774601Swnj 5785065Swnj /* 5795065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 5805065Swnj * starting the time-wait timer, turning off the other 5815065Swnj * standard timers. 5825065Swnj */ 5835065Swnj case TCPS_FIN_WAIT_2: 5845244Sroot tp->t_state = TCPS_TIME_WAIT; 5855074Swnj tcp_canceltimers(tp); 5865162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5875244Sroot soisdisconnected(so); 5885065Swnj break; 5895065Swnj 5904884Swnj /* 5915065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 5924884Swnj */ 5935065Swnj case TCPS_TIME_WAIT: 5945162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5955065Swnj break; 5965085Swnj } 5974601Swnj } 5985267Sroot if (so->so_options & SO_DEBUG) 5995267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6005085Swnj 6015085Swnj /* 6025085Swnj * Return any desired output. 6035085Swnj */ 6045085Swnj tcp_output(tp); 6055065Swnj return; 6065085Swnj 6075065Swnj dropafterack: 6085085Swnj /* 6095244Sroot * Generate an ACK dropping incoming segment. 6105085Swnj * Make ACK reflect our state. 6115085Swnj */ 6125085Swnj if (tiflags & TH_RST) 6135085Swnj goto drop; 614*5391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6155231Swnj return; 6165085Swnj 6175085Swnj dropwithreset: 6185085Swnj /* 6195244Sroot * Generate a RST, dropping incoming segment. 6205085Swnj * Make ACK acceptable to originator of segment. 6215085Swnj */ 6225085Swnj if (tiflags & TH_RST) 6235085Swnj goto drop; 6245085Swnj if (tiflags & TH_ACK) 625*5391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6265085Swnj else { 6275085Swnj if (tiflags & TH_SYN) 6285085Swnj ti->ti_len++; 629*5391Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6305085Swnj } 6315231Swnj return; 6325085Swnj 6335065Swnj drop: 6345085Swnj /* 6355085Swnj * Drop space held by incoming segment and return. 6365085Swnj */ 6375065Swnj m_freem(m); 6385267Sroot return; 6395065Swnj } 6405065Swnj 6415065Swnj /* 6425065Swnj * Insert segment ti into reassembly queue of tcp with 6435065Swnj * control block tp. Return TH_FIN if reassembly now includes 6445065Swnj * a segment with FIN. 6455065Swnj */ 6465109Swnj tcp_reass(tp, ti) 6475065Swnj register struct tcpcb *tp; 6485065Swnj register struct tcpiphdr *ti; 6495065Swnj { 6505065Swnj register struct tcpiphdr *q; 6515085Swnj struct socket *so = tp->t_inpcb->inp_socket; 6525263Swnj struct mbuf *m; 6535263Swnj int flags; 6545085Swnj COUNT(TCP_REASS); 6555065Swnj 6565065Swnj /* 6575162Swnj * Call with ti==0 after become established to 6585162Swnj * force pre-ESTABLISHED data up to user socket. 6595065Swnj */ 6605162Swnj if (ti == 0) 6615065Swnj goto present; 6624601Swnj 6635065Swnj /* 6645065Swnj * Find a segment which begins after this one does. 6655065Swnj */ 6665065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 6675065Swnj q = (struct tcpiphdr *)q->ti_next) 6685065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 6695065Swnj break; 6704601Swnj 6715065Swnj /* 6725065Swnj * If there is a preceding segment, it may provide some of 6735065Swnj * our data already. If so, drop the data from the incoming 6745065Swnj * segment. If it provides all of our data, drop us. 6755065Swnj */ 6765065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 6775065Swnj register int i; 6785065Swnj q = (struct tcpiphdr *)(q->ti_prev); 6795065Swnj /* conversion to int (in i) handles seq wraparound */ 6805065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 6815065Swnj if (i > 0) { 6824924Swnj if (i >= ti->ti_len) 6835065Swnj goto drop; 6845065Swnj m_adj(dtom(tp), i); 6855065Swnj ti->ti_len -= i; 6864924Swnj ti->ti_seq += i; 6874601Swnj } 6885065Swnj q = (struct tcpiphdr *)(q->ti_next); 6895065Swnj } 6904601Swnj 6915065Swnj /* 6925065Swnj * While we overlap succeeding segments trim them or, 6935065Swnj * if they are completely covered, dequeue them. 6945065Swnj */ 6955065Swnj while (q != (struct tcpiphdr *)tp && 6965065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 6975065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 6985065Swnj if (i < q->ti_len) { 6995065Swnj q->ti_len -= i; 7005065Swnj m_adj(dtom(q), i); 7015065Swnj break; 7024601Swnj } 7035065Swnj q = (struct tcpiphdr *)q->ti_next; 7045065Swnj m_freem(dtom(q->ti_prev)); 7055065Swnj remque(q->ti_prev); 7065065Swnj } 7074601Swnj 7085065Swnj /* 7095065Swnj * Stick new segment in its place. 7105065Swnj */ 7115065Swnj insque(ti, q->ti_prev); 7124601Swnj 7135065Swnj present: 7145065Swnj /* 7155244Sroot * Present data to user, advancing rcv_nxt through 7165244Sroot * completed sequence space. 7175065Swnj */ 7185263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 7195244Sroot return (0); 7204924Swnj ti = tp->seg_next; 7215263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 7225263Swnj return (0); 7235263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 7245263Swnj return (0); 7255263Swnj do { 7265244Sroot tp->rcv_nxt += ti->ti_len; 7275244Sroot flags = ti->ti_flags & TH_FIN; 7284924Swnj remque(ti); 7295263Swnj m = dtom(ti); 7304924Swnj ti = (struct tcpiphdr *)ti->ti_next; 7315263Swnj if (so->so_state & SS_CANTRCVMORE) 7325263Swnj (void) m_freem(m); 7335263Swnj else 7345263Swnj sbappend(&so->so_rcv, m); 7355263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 7365263Swnj sorwakeup(so); 7375065Swnj return (flags); 7385065Swnj drop: 7395065Swnj m_freem(dtom(ti)); 7405263Swnj return (0); 7414601Swnj } 742