1*5251Sroot /* tcp_input.c 1.40 81/12/12 */ 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" 165244Sroot #define TCPSTATES 174803Swnj #include "../net/tcp_fsm.h" 185085Swnj #include "../net/tcp_seq.h" 195085Swnj #include "../net/tcp_timer.h" 204803Swnj #include "../net/tcp_var.h" 215085Swnj #include "../net/tcpip.h" 225109Swnj #include "../errno.h" 234601Swnj 244679Swnj int tcpcksum = 1; 255244Sroot struct sockaddr_in tcp_in = { AF_INET }; 264601Swnj 275065Swnj /* 285065Swnj * TCP input routine, follows pages 65-76 of the 295065Swnj * protocol specification dated September, 1981 very closely. 305065Swnj */ 314924Swnj tcp_input(m0) 324924Swnj struct mbuf *m0; 334601Swnj { 344924Swnj register struct tcpiphdr *ti; 354924Swnj struct inpcb *inp; 364924Swnj register struct mbuf *m; 374924Swnj int len, tlen, off; 384924Swnj register struct tcpcb *tp; 394924Swnj register int tiflags; 404803Swnj struct socket *so; 415109Swnj int todrop, acked; 424924Swnj 434601Swnj COUNT(TCP_INPUT); 444924Swnj /* 455244Sroot * Get IP and TCP header together in first mbuf. 465244Sroot * Note: IP leaves IP header in first mbuf. 474924Swnj */ 484924Swnj m = m0; 495020Sroot ti = mtod(m, struct tcpiphdr *); 505244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 515208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 525085Swnj if (m->m_len < sizeof (struct tcpiphdr)) { 535085Swnj if (m_pullup(m, sizeof (struct tcpiphdr)) == 0) { 545085Swnj tcpstat.tcps_hdrops++; 555085Swnj goto drop; 565085Swnj } 575085Swnj ti = mtod(m, struct tcpiphdr *); 585085Swnj } 594601Swnj 604601Swnj /* 615244Sroot * Checksum extended TCP header and data. 624601Swnj */ 634924Swnj tlen = ((struct ip *)ti)->ip_len; 644924Swnj len = sizeof (struct ip) + tlen; 654679Swnj if (tcpcksum) { 664924Swnj ti->ti_next = ti->ti_prev = 0; 674924Swnj ti->ti_x1 = 0; 685223Swnj ti->ti_len = (u_short)tlen; 695223Swnj #if vax 705223Swnj ti->ti_len = htons(ti->ti_len); 715223Swnj #endif 725231Swnj if (ti->ti_sum = in_cksum(m, len)) { 734924Swnj tcpstat.tcps_badsum++; 745065Swnj printf("tcp cksum %x\n", ti->ti_sum); 755085Swnj goto drop; 764601Swnj } 774601Swnj } 784601Swnj 794601Swnj /* 805244Sroot * Check that TCP offset makes sense, 815244Sroot * process TCP options and adjust length. 824601Swnj */ 834924Swnj off = ti->ti_off << 2; 845231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 854924Swnj tcpstat.tcps_badoff++; 865085Swnj goto drop; 874924Swnj } 884924Swnj ti->ti_len = tlen - off; 895085Swnj #if 0 905231Swnj if (off > sizeof (struct tcphdr)) 915085Swnj tcp_options(ti); 925085Swnj #endif 935065Swnj tiflags = ti->ti_flags; 944924Swnj 955231Swnj #if vax 964924Swnj /* 975244Sroot * Convert TCP protocol specific fields to host format. 985085Swnj */ 995085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1005085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1015085Swnj ti->ti_win = ntohs(ti->ti_win); 1025085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1035231Swnj #endif 1045085Swnj 1055085Swnj /* 1064924Swnj * Locate pcb for segment. 1074924Swnj */ 1085065Swnj inp = in_pcblookup 1095065Swnj (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 1105065Swnj 1115065Swnj /* 1125065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1135244Sroot * all data in the incoming segment is discarded. 1145065Swnj */ 1154884Swnj if (inp == 0) 1165085Swnj goto dropwithreset; 1175065Swnj tp = intotcpcb(inp); 1185065Swnj if (tp == 0) 1195085Swnj goto dropwithreset; 1205109Swnj so = inp->inp_socket; 1214601Swnj 1224601Swnj /* 1235162Swnj * Segment received on connection. 1245162Swnj * Reset idle time and keep-alive timer. 1255162Swnj */ 1265162Swnj tp->t_idle = 0; 1275162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1285162Swnj 1295162Swnj /* 1305085Swnj * Calculate amount of space in receive window, 1315085Swnj * and then do TCP input processing. 1324601Swnj */ 1335085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1345231Swnj if (tp->rcv_wnd < 0) 1355231Swnj tp->rcv_wnd = 0; 1364601Swnj 1374601Swnj switch (tp->t_state) { 1384601Swnj 1395065Swnj /* 1405065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1415065Swnj * If the segment contains an ACK then it is bad and send a RST. 1425065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1435085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1445065Swnj * tp->iss, and send a segment: 1455085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1465065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1475065Swnj * Fill in remote peer address fields if not previously specified. 1485065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1495244Sroot * segment in this state. 1505065Swnj */ 1515065Swnj case TCPS_LISTEN: 1525065Swnj if (tiflags & TH_RST) 1535065Swnj goto drop; 1545065Swnj if (tiflags & TH_ACK) 1555085Swnj goto dropwithreset; 1565065Swnj if ((tiflags & TH_SYN) == 0) 1575065Swnj goto drop; 1585244Sroot tcp_in.sin_addr = ti->ti_src; 1595244Sroot tcp_in.sin_port = ti->ti_sport; 1605244Sroot if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in)) 1615244Sroot goto drop; 1625244Sroot tp->t_template = tcp_template(tp); 1635244Sroot if (tp->t_template == 0) { 1645244Sroot in_pcbdisconnect(inp); 1655244Sroot goto drop; 1665244Sroot } 1675085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1685065Swnj tp->irs = ti->ti_seq; 1695085Swnj tcp_sendseqinit(tp); 1705085Swnj tcp_rcvseqinit(tp); 1715065Swnj tp->t_state = TCPS_SYN_RECEIVED; 1725244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1735085Swnj goto trimthenstep6; 1744601Swnj 1755065Swnj /* 1765065Swnj * If the state is SYN_SENT: 1775065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 1785065Swnj * if seg contains a RST, then drop the connection. 1795065Swnj * if seg does not contain SYN, then drop it. 1805065Swnj * Otherwise this is an acceptable SYN segment 1815065Swnj * initialize tp->rcv_nxt and tp->irs 1825065Swnj * if seg contains ack then advance tp->snd_una 1835065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1845065Swnj * arrange for segment to be acked (eventually) 1855065Swnj * continue processing rest of data/controls, beginning with URG 1865065Swnj */ 1875065Swnj case TCPS_SYN_SENT: 1885065Swnj if ((tiflags & TH_ACK) && 1895065Swnj (SEQ_LEQ(ti->ti_ack, tp->iss) || 1905231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 1915085Swnj goto dropwithreset; 1925065Swnj if (tiflags & TH_RST) { 1935065Swnj if (tiflags & TH_ACK) 1945085Swnj tcp_drop(tp, ECONNRESET); 1955065Swnj goto drop; 1964601Swnj } 1975065Swnj if ((tiflags & TH_SYN) == 0) 1985065Swnj goto drop; 1995231Swnj tp->snd_una = ti->ti_ack; 2005244Sroot tp->t_timer[TCPT_REXMT] = 0; 2015065Swnj tp->irs = ti->ti_seq; 2025085Swnj tcp_rcvseqinit(tp); 2035085Swnj tp->t_flags |= TF_ACKNOW; 2045162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2055244Sroot soisconnected(so); 2065065Swnj tp->t_state = TCPS_ESTABLISHED; 2075162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2085231Swnj tp->snd_wl1 = ti->ti_seq; 2095162Swnj } else 2105085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2115085Swnj goto trimthenstep6; 2125085Swnj 2135085Swnj trimthenstep6: 2145085Swnj /* 2155231Swnj * Advance ti->ti_seq to correspond to first data byte. 2165085Swnj * If data, trim to stay within window, 2175085Swnj * dropping FIN if necessary. 2185085Swnj */ 2195231Swnj ti->ti_seq++; 2205085Swnj if (ti->ti_len > tp->rcv_wnd) { 2215085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2225085Swnj m_adj(m, -todrop); 2235085Swnj ti->ti_len = tp->rcv_wnd; 2245085Swnj ti->ti_flags &= ~TH_FIN; 2255065Swnj } 2265085Swnj goto step6; 2275065Swnj } 2284601Swnj 2295065Swnj /* 2305065Swnj * States other than LISTEN or SYN_SENT. 2315065Swnj * First check that at least some bytes of segment are within 2325065Swnj * receive window. 2335065Swnj */ 2345065Swnj if (tp->rcv_wnd == 0) { 2355065Swnj /* 2365065Swnj * If window is closed can only take segments at 2375231Swnj * window edge, and have to drop data and PUSH from 2385065Swnj * incoming segments. 2395065Swnj */ 2405065Swnj if (tp->rcv_nxt != ti->ti_seq) 2415065Swnj goto dropafterack; 2425085Swnj if (ti->ti_len > 0) { 2435085Swnj ti->ti_len = 0; 2445085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2455065Swnj } 2465065Swnj } else { 2475065Swnj /* 2485231Swnj * If segment begins before rcv_nxt, drop leading 2495065Swnj * data (and SYN); if nothing left, just ack. 2505065Swnj */ 2515065Swnj if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 2525085Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2535085Swnj if (tiflags & TH_SYN) { 2545085Swnj ti->ti_seq++; 2555085Swnj if (ti->ti_urp > 1) 2565085Swnj ti->ti_urp--; 2575085Swnj else 2585085Swnj tiflags &= ~TH_URG; 2595085Swnj todrop--; 2605085Swnj } 2615065Swnj if (todrop > ti->ti_len) 2625065Swnj goto dropafterack; 2635065Swnj m_adj(m, todrop); 2645065Swnj ti->ti_seq += todrop; 2655065Swnj ti->ti_len -= todrop; 2665085Swnj if (ti->ti_urp > todrop) 2675085Swnj ti->ti_urp -= todrop; 2685085Swnj else { 2695085Swnj tiflags &= ~TH_URG; 2705085Swnj /* ti->ti_flags &= ~TH_URG; */ 2715085Swnj /* ti->ti_urp = 0; */ 2725085Swnj } 2735085Swnj /* tiflags &= ~TH_SYN; */ 2745085Swnj /* ti->ti_flags &= ~TH_SYN; */ 2755065Swnj } 2765065Swnj /* 2775065Swnj * If segment ends after window, drop trailing data 2785085Swnj * (and PUSH and FIN); if nothing left, just ACK. 2795065Swnj */ 2805065Swnj if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 2815085Swnj todrop = 2825065Swnj ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 2835065Swnj if (todrop > ti->ti_len) 2845065Swnj goto dropafterack; 2855065Swnj m_adj(m, -todrop); 2865065Swnj ti->ti_len -= todrop; 2875085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2885065Swnj } 2895065Swnj } 2904601Swnj 2915065Swnj /* 2925065Swnj * If the RST bit is set examine the state: 2935065Swnj * SYN_RECEIVED STATE: 2945065Swnj * If passive open, return to LISTEN state. 2955065Swnj * If active open, inform user that connection was refused. 2965065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 2975065Swnj * Inform user that connection was reset, and close tcb. 2985065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 2995065Swnj * Close the tcb. 3005065Swnj */ 3015065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3025065Swnj 3035065Swnj case TCPS_SYN_RECEIVED: 3045065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3055085Swnj tp->t_state = TCPS_LISTEN; 3065244Sroot tp->t_timer[TCPT_KEEP] = 0; 3075244Sroot (void) m_free(dtom(tp->t_template)); 3085244Sroot tp->t_template = 0; 3095231Swnj in_pcbdisconnect(inp); 3105065Swnj goto drop; 3114601Swnj } 3125085Swnj tcp_drop(tp, ECONNREFUSED); 3135065Swnj goto drop; 3144601Swnj 3155065Swnj case TCPS_ESTABLISHED: 3165065Swnj case TCPS_FIN_WAIT_1: 3175065Swnj case TCPS_FIN_WAIT_2: 3185065Swnj case TCPS_CLOSE_WAIT: 3195065Swnj tcp_drop(tp, ECONNRESET); 3205065Swnj goto drop; 3215065Swnj 3225065Swnj case TCPS_CLOSING: 3235065Swnj case TCPS_LAST_ACK: 3245065Swnj case TCPS_TIME_WAIT: 3255065Swnj tcp_close(tp); 3265065Swnj goto drop; 3274601Swnj } 3284601Swnj 3294601Swnj /* 3305065Swnj * If a SYN is in the window, then this is an 3315065Swnj * error and we send an RST and drop the connection. 3324601Swnj */ 3335065Swnj if (tiflags & TH_SYN) { 3345231Swnj tcp_drop(tp, ECONNRESET); 3355085Swnj goto dropwithreset; 3364601Swnj } 3374601Swnj 3384601Swnj /* 3395065Swnj * If the ACK bit is off we drop the segment and return. 3404601Swnj */ 3415085Swnj if ((tiflags & TH_ACK) == 0) 3425065Swnj goto drop; 3435065Swnj 3445065Swnj /* 3455065Swnj * Ack processing. 3465065Swnj */ 3474601Swnj switch (tp->t_state) { 3484601Swnj 3495065Swnj /* 3505065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3515065Swnj * ESTABLISHED state and continue processing, othewise 3525065Swnj * send an RST. 3535065Swnj */ 3545065Swnj case TCPS_SYN_RECEIVED: 3555085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 3565231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 3575085Swnj goto dropwithreset; 3585244Sroot tp->snd_una++; /* SYN acked */ 3595244Sroot tp->t_timer[TCPT_REXMT] = 0; 3605085Swnj soisconnected(so); 3615085Swnj tp->t_state = TCPS_ESTABLISHED; 3625162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 3635244Sroot tp->snd_wl1 = ti->ti_seq - 1; 3645085Swnj /* fall into ... */ 3654601Swnj 3665065Swnj /* 3675065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 3685065Swnj * ACKs. If the ack is in the range 3695231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 3705065Swnj * then advance tp->snd_una to ti->ti_ack and drop 3715065Swnj * data from the retransmission queue. If this ACK reflects 3725065Swnj * more up to date window information we update our window information. 3735065Swnj */ 3745065Swnj case TCPS_ESTABLISHED: 3755065Swnj case TCPS_FIN_WAIT_1: 3765065Swnj case TCPS_FIN_WAIT_2: 3775065Swnj case TCPS_CLOSE_WAIT: 3785065Swnj case TCPS_CLOSING: 3795244Sroot case TCPS_LAST_ACK: 3805244Sroot case TCPS_TIME_WAIT: 3815085Swnj #define ourfinisacked (acked > 0) 3825085Swnj 3835244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 3845065Swnj break; 3855231Swnj if (SEQ_GT(ti->ti_ack, tp->snd_max)) 3865065Swnj goto dropafterack; 3875085Swnj acked = ti->ti_ack - tp->snd_una; 3885244Sroot if (acked >= so->so_snd.sb_cc) { 3895085Swnj acked -= so->so_snd.sb_cc; 3905244Sroot /* if acked > 0 our FIN is acked */ 3915244Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 3925244Sroot tp->t_timer[TCPT_REXMT] = 0; 3935085Swnj } else { 3945085Swnj sbdrop(&so->so_snd, acked); 3955085Swnj acked = 0; 3965244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 3975244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 3985085Swnj } 3995231Swnj tp->snd_una = ti->ti_ack; 4005162Swnj 4015162Swnj /* 4025162Swnj * If transmit timer is running and timed sequence 4035162Swnj * number was acked, update smoothed round trip time. 4045162Swnj */ 4055162Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4065244Sroot if (tp->t_srtt == 0) 4075244Sroot tp->t_srtt = tp->t_rtt; 4085244Sroot else 4095244Sroot tp->t_srtt = 4105244Sroot tcp_alpha * tp->t_srtt + 4115244Sroot (1 - tcp_alpha) * tp->t_rtt; 4125162Swnj tp->t_rtt = 0; 4135162Swnj } 4145162Swnj 4154601Swnj switch (tp->t_state) { 4164601Swnj 4175065Swnj /* 4185065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4195065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4205085Swnj * then enter FIN_WAIT_2. 4215065Swnj */ 4225065Swnj case TCPS_FIN_WAIT_1: 4235085Swnj if (ourfinisacked) 4245085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4254601Swnj break; 4264601Swnj 4275065Swnj /* 4285065Swnj * In CLOSING STATE in addition to the processing for 4295065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4305065Swnj * then enter the TIME-WAIT state, otherwise ignore 4315065Swnj * the segment. 4325065Swnj */ 4335065Swnj case TCPS_CLOSING: 4345244Sroot if (ourfinisacked) { 4355065Swnj tp->t_state = TCPS_TIME_WAIT; 4365244Sroot tcp_canceltimers(tp); 4375244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4385244Sroot soisdisconnected(so); 4395244Sroot } 4405244Sroot break; 4414601Swnj 4425065Swnj /* 4435085Swnj * The only thing that can arrive in LAST_ACK state 4445085Swnj * is an acknowledgment of our FIN. If our FIN is now 4455085Swnj * acknowledged, delete the TCB, enter the closed state 4465085Swnj * and return. 4475065Swnj */ 4485065Swnj case TCPS_LAST_ACK: 449*5251Sroot if (ourfinisacked) 4505065Swnj tcp_close(tp); 4515065Swnj goto drop; 4524601Swnj 4535065Swnj /* 4545065Swnj * In TIME_WAIT state the only thing that should arrive 4555065Swnj * is a retransmission of the remote FIN. Acknowledge 4565065Swnj * it and restart the finack timer. 4575065Swnj */ 4585065Swnj case TCPS_TIME_WAIT: 4595162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4605065Swnj goto dropafterack; 4614601Swnj } 4625085Swnj #undef ourfinisacked 4635085Swnj } 4644601Swnj 4655065Swnj step6: 4665065Swnj /* 4675244Sroot * Update window information. 4685244Sroot */ 4695244Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 4705244Sroot tp->snd_wl1==ti->ti_seq && SEQ_LEQ(tp->snd_wl2,ti->ti_seq)) { 4715244Sroot tp->snd_wnd = ti->ti_win; 4725244Sroot tp->snd_wl1 = ti->ti_seq; 4735244Sroot tp->snd_wl2 = ti->ti_ack; 4745244Sroot if (tp->snd_wnd > 0) 4755244Sroot tp->t_timer[TCPT_PERSIST] = 0; 4765244Sroot } 4775244Sroot 4785244Sroot /* 4795065Swnj * If an URG bit is set in the segment and is greater than the 4805065Swnj * current known urgent pointer, then signal the user that the 4815244Sroot * remote side has out of band data. This should not happen 4825065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 4835065Swnj * a FIN has been received from the remote side. In these states 4845065Swnj * we ignore the URG. 4855065Swnj */ 4865085Swnj if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 4875085Swnj if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 4885065Swnj tp->rcv_up = ti->ti_urp; 4895085Swnj #if 0 4905244Sroot sohasoutofband(so); /* XXX */ 4915085Swnj #endif 4924601Swnj } 4934601Swnj 4944601Swnj /* 4955065Swnj * Process the segment text, merging it into the TCP sequencing queue, 4965065Swnj * and arranging for acknowledgment of receipt if necessary. 4975065Swnj * This process logically involves adjusting tp->rcv_wnd as data 4985065Swnj * is presented to the user (this happens in tcp_usrreq.c, 4995065Swnj * case PRU_RCVD). If a FIN has already been received on this 5005065Swnj * connection then we just ignore the text. 5014601Swnj */ 5025244Sroot if (ti->ti_len && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5035085Swnj off += sizeof (struct ip); /* drop IP header */ 5045085Swnj m->m_off += off; 5055085Swnj m->m_len -= off; 5065065Swnj tiflags = tcp_reass(tp, ti); 5075085Swnj tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 5085244Sroot } else { 5094924Swnj m_freem(m); 5105244Sroot } 5114601Swnj 5124601Swnj /* 5135065Swnj * If FIN is received then if we haven't received SYN and 5145065Swnj * therefore can't validate drop the segment. Otherwise ACK 5155065Swnj * the FIN and let the user know that the connection is closing. 5164601Swnj */ 5175085Swnj if ((tiflags & TH_FIN)) { 5185074Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 5195074Swnj goto drop; 5205244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5215244Sroot socantrcvmore(so); 5225244Sroot tp->t_flags |= TF_ACKNOW; 5235244Sroot tp->rcv_nxt++; 5245244Sroot } 5255065Swnj switch (tp->t_state) { 5264601Swnj 5275065Swnj /* 5285065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 5295065Swnj * enter the CLOSE_WAIT state. 5304884Swnj */ 5315065Swnj case TCPS_SYN_RECEIVED: 5325065Swnj case TCPS_ESTABLISHED: 5335065Swnj tp->t_state = TCPS_CLOSE_WAIT; 5345065Swnj break; 5354884Swnj 5365065Swnj /* 5375085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 5385085Swnj * enter the CLOSING state. 5394884Swnj */ 5405065Swnj case TCPS_FIN_WAIT_1: 5415085Swnj tp->t_state = TCPS_CLOSING; 5425065Swnj break; 5434601Swnj 5445065Swnj /* 5455065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 5465065Swnj * starting the time-wait timer, turning off the other 5475065Swnj * standard timers. 5485065Swnj */ 5495065Swnj case TCPS_FIN_WAIT_2: 5505244Sroot tp->t_state = TCPS_TIME_WAIT; 5515074Swnj tcp_canceltimers(tp); 5525162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5535244Sroot soisdisconnected(so); 5545065Swnj break; 5555065Swnj 5564884Swnj /* 5575065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 5584884Swnj */ 5595065Swnj case TCPS_TIME_WAIT: 5605162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5615065Swnj break; 5625085Swnj } 5634601Swnj } 5645085Swnj 5655085Swnj /* 5665085Swnj * Return any desired output. 5675085Swnj */ 5685085Swnj tcp_output(tp); 5695065Swnj return; 5705085Swnj 5715065Swnj dropafterack: 5725085Swnj /* 5735244Sroot * Generate an ACK dropping incoming segment. 5745085Swnj * Make ACK reflect our state. 5755085Swnj */ 5765085Swnj if (tiflags & TH_RST) 5775085Swnj goto drop; 5785085Swnj tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 5795231Swnj return; 5805085Swnj 5815085Swnj dropwithreset: 5825085Swnj /* 5835244Sroot * Generate a RST, dropping incoming segment. 5845085Swnj * Make ACK acceptable to originator of segment. 5855085Swnj */ 5865085Swnj if (tiflags & TH_RST) 5875085Swnj goto drop; 5885085Swnj if (tiflags & TH_ACK) 5895109Swnj tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST); 5905085Swnj else { 5915085Swnj if (tiflags & TH_SYN) 5925085Swnj ti->ti_len++; 5935109Swnj tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 5945085Swnj } 5955231Swnj return; 5965085Swnj 5975065Swnj drop: 5985085Swnj /* 5995085Swnj * Drop space held by incoming segment and return. 6005085Swnj */ 6015065Swnj m_freem(m); 6025065Swnj } 6035065Swnj 6045065Swnj /* 6055065Swnj * Insert segment ti into reassembly queue of tcp with 6065065Swnj * control block tp. Return TH_FIN if reassembly now includes 6075065Swnj * a segment with FIN. 6085065Swnj */ 6095109Swnj tcp_reass(tp, ti) 6105065Swnj register struct tcpcb *tp; 6115065Swnj register struct tcpiphdr *ti; 6125065Swnj { 6135065Swnj register struct tcpiphdr *q; 6145085Swnj struct socket *so = tp->t_inpcb->inp_socket; 6155065Swnj int flags = 0; /* no FIN */ 6165085Swnj COUNT(TCP_REASS); 6175065Swnj 6185065Swnj /* 6195162Swnj * Call with ti==0 after become established to 6205162Swnj * force pre-ESTABLISHED data up to user socket. 6215065Swnj */ 6225162Swnj if (ti == 0) 6235065Swnj goto present; 6244601Swnj 6255065Swnj /* 6265065Swnj * Find a segment which begins after this one does. 6275065Swnj */ 6285065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 6295065Swnj q = (struct tcpiphdr *)q->ti_next) 6305065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 6315065Swnj break; 6324601Swnj 6335065Swnj /* 6345065Swnj * If there is a preceding segment, it may provide some of 6355065Swnj * our data already. If so, drop the data from the incoming 6365065Swnj * segment. If it provides all of our data, drop us. 6375065Swnj */ 6385065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 6395065Swnj register int i; 6405065Swnj q = (struct tcpiphdr *)(q->ti_prev); 6415065Swnj /* conversion to int (in i) handles seq wraparound */ 6425065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 6435065Swnj if (i > 0) { 6444924Swnj if (i >= ti->ti_len) 6455065Swnj goto drop; 6465065Swnj m_adj(dtom(tp), i); 6475065Swnj ti->ti_len -= i; 6484924Swnj ti->ti_seq += i; 6494601Swnj } 6505065Swnj q = (struct tcpiphdr *)(q->ti_next); 6515065Swnj } 6524601Swnj 6535065Swnj /* 6545065Swnj * While we overlap succeeding segments trim them or, 6555065Swnj * if they are completely covered, dequeue them. 6565065Swnj */ 6575065Swnj while (q != (struct tcpiphdr *)tp && 6585065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 6595065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 6605065Swnj if (i < q->ti_len) { 6615065Swnj q->ti_len -= i; 6625065Swnj m_adj(dtom(q), i); 6635065Swnj break; 6644601Swnj } 6655065Swnj q = (struct tcpiphdr *)q->ti_next; 6665065Swnj m_freem(dtom(q->ti_prev)); 6675065Swnj remque(q->ti_prev); 6685065Swnj } 6694601Swnj 6705065Swnj /* 6715065Swnj * Stick new segment in its place. 6725065Swnj */ 6735065Swnj insque(ti, q->ti_prev); 6744601Swnj 6755065Swnj present: 6765065Swnj /* 6775244Sroot * Present data to user, advancing rcv_nxt through 6785244Sroot * completed sequence space. 6795065Swnj */ 6805085Swnj if (tp->t_state < TCPS_ESTABLISHED) 6815244Sroot return (0); 6824924Swnj ti = tp->seg_next; 6835244Sroot while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt) { 6845244Sroot tp->rcv_nxt += ti->ti_len; 6855244Sroot flags = ti->ti_flags & TH_FIN; 6864924Swnj remque(ti); 6874924Swnj sbappend(&so->so_rcv, dtom(ti)); 6884924Swnj ti = (struct tcpiphdr *)ti->ti_next; 6894601Swnj } 6905074Swnj if (so->so_state & SS_CANTRCVMORE) 6915074Swnj sbflush(&so->so_rcv); 6925074Swnj else 6935074Swnj sorwakeup(so); 6945065Swnj return (flags); 6955065Swnj drop: 6965065Swnj m_freem(dtom(ti)); 6975065Swnj return (flags); 6984601Swnj } 699