1*5162Swnj /* tcp_input.c 1.34 81/12/02 */ 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" 215109Swnj #include "../errno.h" 224601Swnj 234679Swnj int tcpcksum = 1; 244601Swnj 255065Swnj /* 265065Swnj * TCP input routine, follows pages 65-76 of the 275065Swnj * protocol specification dated September, 1981 very closely. 285065Swnj */ 294924Swnj tcp_input(m0) 304924Swnj struct mbuf *m0; 314601Swnj { 324924Swnj register struct tcpiphdr *ti; 334924Swnj struct inpcb *inp; 344924Swnj register struct mbuf *m; 354924Swnj int len, tlen, off; 364924Swnj register struct tcpcb *tp; 374924Swnj register int tiflags; 384803Swnj struct socket *so; 395109Swnj int todrop, acked; 404924Swnj 414601Swnj COUNT(TCP_INPUT); 424924Swnj /* 434924Swnj * Get ip and tcp header together in first mbuf. 444924Swnj */ 454924Swnj m = m0; 465020Sroot ti = mtod(m, struct tcpiphdr *); 475020Sroot if (ti->ti_len > sizeof (struct ip)) 485020Sroot ip_stripoptions((struct ip *)ti, (char *)0); 495085Swnj if (m->m_len < sizeof (struct tcpiphdr)) { 505085Swnj if (m_pullup(m, sizeof (struct tcpiphdr)) == 0) { 515085Swnj tcpstat.tcps_hdrops++; 525085Swnj goto drop; 535085Swnj } 545085Swnj ti = mtod(m, struct tcpiphdr *); 555085Swnj } 564601Swnj 574601Swnj /* 584924Swnj * Checksum extended tcp header and data. 594601Swnj */ 604924Swnj tlen = ((struct ip *)ti)->ip_len; 614924Swnj len = sizeof (struct ip) + tlen; 624679Swnj if (tcpcksum) { 634924Swnj ti->ti_next = ti->ti_prev = 0; 644924Swnj ti->ti_x1 = 0; 654953Swnj ti->ti_len = htons((u_short)tlen); 665085Swnj if ((ti->ti_sum = in_cksum(m, len)) != 0xffff) { 674924Swnj tcpstat.tcps_badsum++; 685065Swnj printf("tcp cksum %x\n", ti->ti_sum); 695085Swnj goto drop; 704601Swnj } 714601Swnj } 724601Swnj 734601Swnj /* 744924Swnj * Check that tcp offset makes sense, 754924Swnj * process tcp options and adjust length. 764601Swnj */ 774924Swnj off = ti->ti_off << 2; 784924Swnj if (off < sizeof (struct tcphdr) || off > ti->ti_len) { 794924Swnj tcpstat.tcps_badoff++; 805085Swnj goto drop; 814924Swnj } 824924Swnj ti->ti_len = tlen - off; 835085Swnj #if 0 845085Swnj if (off > sizeof (struct tcphdr) >> 2) 855085Swnj tcp_options(ti); 865085Swnj #endif 875065Swnj tiflags = ti->ti_flags; 884924Swnj 894924Swnj /* 905085Swnj * Convert tcp protocol specific fields to host format. 915085Swnj */ 925085Swnj ti->ti_seq = ntohl(ti->ti_seq); 935085Swnj ti->ti_ack = ntohl(ti->ti_ack); 945085Swnj ti->ti_win = ntohs(ti->ti_win); 955085Swnj ti->ti_urp = ntohs(ti->ti_urp); 965085Swnj 975085Swnj /* 984924Swnj * Locate pcb for segment. 994924Swnj */ 1005065Swnj inp = in_pcblookup 1015065Swnj (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 1025065Swnj 1035065Swnj /* 1045065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1055065Swnj * all data in the incoming segment is discarded. (p. 65). 1065065Swnj */ 1074884Swnj if (inp == 0) 1085085Swnj goto dropwithreset; 1095065Swnj tp = intotcpcb(inp); 1105065Swnj if (tp == 0) 1115085Swnj goto dropwithreset; 1125109Swnj so = inp->inp_socket; 1134601Swnj 1144601Swnj /* 115*5162Swnj * Segment received on connection. 116*5162Swnj * Reset idle time and keep-alive timer. 117*5162Swnj */ 118*5162Swnj tp->t_idle = 0; 119*5162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 120*5162Swnj 121*5162Swnj /* 1225085Swnj * Calculate amount of space in receive window, 1235085Swnj * and then do TCP input processing. 1244601Swnj */ 1255085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1264601Swnj 1274601Swnj switch (tp->t_state) { 1284601Swnj 1295065Swnj /* 1305065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1315065Swnj * If the segment contains an ACK then it is bad and send a RST. 1325065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1335085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1345065Swnj * tp->iss, and send a segment: 1355085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1365065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1375065Swnj * Fill in remote peer address fields if not previously specified. 1385065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1395065Swnj * segment in this state. (p. 65) 1405065Swnj */ 1415065Swnj case TCPS_LISTEN: 1425065Swnj if (tiflags & TH_RST) 1435065Swnj goto drop; 1445065Swnj if (tiflags & TH_ACK) 1455085Swnj goto dropwithreset; 1465065Swnj if ((tiflags & TH_SYN) == 0) 1475065Swnj goto drop; 1485085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1495065Swnj tp->irs = ti->ti_seq; 1505085Swnj tcp_sendseqinit(tp); 1515085Swnj tcp_rcvseqinit(tp); 1525065Swnj tp->t_state = TCPS_SYN_RECEIVED; 1535065Swnj if (inp->inp_faddr.s_addr == 0) { 1545065Swnj inp->inp_faddr = ti->ti_src; 1555065Swnj inp->inp_fport = ti->ti_sport; 1564601Swnj } 1575085Swnj goto trimthenstep6; 1584601Swnj 1595065Swnj /* 1605065Swnj * If the state is SYN_SENT: 1615065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 1625065Swnj * if seg contains a RST, then drop the connection. 1635065Swnj * if seg does not contain SYN, then drop it. 1645065Swnj * Otherwise this is an acceptable SYN segment 1655065Swnj * initialize tp->rcv_nxt and tp->irs 1665065Swnj * if seg contains ack then advance tp->snd_una 1675065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1685065Swnj * arrange for segment to be acked (eventually) 1695065Swnj * continue processing rest of data/controls, beginning with URG 1705065Swnj */ 1715065Swnj case TCPS_SYN_SENT: 1725065Swnj if ((tiflags & TH_ACK) && 1735065Swnj (SEQ_LEQ(ti->ti_ack, tp->iss) || 1745085Swnj SEQ_GT(ti->ti_ack, tp->snd_nxt))) 1755085Swnj goto dropwithreset; 1765065Swnj if (tiflags & TH_RST) { 1775065Swnj if (tiflags & TH_ACK) 1785085Swnj tcp_drop(tp, ECONNRESET); 1795065Swnj goto drop; 1804601Swnj } 1815065Swnj if ((tiflags & TH_SYN) == 0) 1825065Swnj goto drop; 1835085Swnj tp->iss = ti->ti_ack; 1845085Swnj tcp_sendseqinit(tp); 1855065Swnj tp->irs = ti->ti_seq; 1865085Swnj tcp_rcvseqinit(tp); 1875085Swnj tp->t_flags |= TF_ACKNOW; 188*5162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 1895065Swnj tp->t_state = TCPS_ESTABLISHED; 190*5162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 191*5162Swnj } else 1925085Swnj tp->t_state = TCPS_SYN_RECEIVED; 1935085Swnj goto trimthenstep6; 1945085Swnj 1955085Swnj trimthenstep6: 1965085Swnj /* 1975085Swnj * If had syn, advance ti->ti_seq to correspond 1985085Swnj * to first data byte. 1995085Swnj */ 2005085Swnj if (tiflags & TH_SYN) 2015085Swnj ti->ti_seq++; 2025085Swnj 2035085Swnj /* 2045085Swnj * If data, trim to stay within window, 2055085Swnj * dropping FIN if necessary. 2065085Swnj */ 2075085Swnj if (ti->ti_len > tp->rcv_wnd) { 2085085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2095085Swnj m_adj(m, -todrop); 2105085Swnj ti->ti_len = tp->rcv_wnd; 2115085Swnj ti->ti_flags &= ~TH_FIN; 2125065Swnj } 2135085Swnj goto step6; 2145065Swnj } 2154601Swnj 2165065Swnj /* 2175065Swnj * States other than LISTEN or SYN_SENT. 2185065Swnj * First check that at least some bytes of segment are within 2195065Swnj * receive window. 2205065Swnj */ 2215065Swnj if (tp->rcv_wnd == 0) { 2225065Swnj /* 2235065Swnj * If window is closed can only take segments at 2245065Swnj * window edge, and have to drop data and EOL from 2255065Swnj * incoming segments. 2265065Swnj */ 2275065Swnj if (tp->rcv_nxt != ti->ti_seq) 2285065Swnj goto dropafterack; 2295085Swnj if (ti->ti_len > 0) { 2305085Swnj ti->ti_len = 0; 2315085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2325065Swnj } 2335065Swnj } else { 2345065Swnj /* 2355065Swnj * If segment begins before rcv_next, drop leading 2365065Swnj * data (and SYN); if nothing left, just ack. 2375065Swnj */ 2385065Swnj if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 2395085Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2405085Swnj if (tiflags & TH_SYN) { 2415085Swnj ti->ti_seq++; 2425085Swnj if (ti->ti_urp > 1) 2435085Swnj ti->ti_urp--; 2445085Swnj else 2455085Swnj tiflags &= ~TH_URG; 2465085Swnj todrop--; 2475085Swnj } 2485065Swnj if (todrop > ti->ti_len) 2495065Swnj goto dropafterack; 2505065Swnj m_adj(m, todrop); 2515065Swnj ti->ti_seq += todrop; 2525065Swnj ti->ti_len -= todrop; 2535085Swnj if (ti->ti_urp > todrop) 2545085Swnj ti->ti_urp -= todrop; 2555085Swnj else { 2565085Swnj tiflags &= ~TH_URG; 2575085Swnj /* ti->ti_flags &= ~TH_URG; */ 2585085Swnj /* ti->ti_urp = 0; */ 2595085Swnj } 2605085Swnj /* tiflags &= ~TH_SYN; */ 2615085Swnj /* ti->ti_flags &= ~TH_SYN; */ 2625065Swnj } 2635065Swnj /* 2645065Swnj * If segment ends after window, drop trailing data 2655085Swnj * (and PUSH and FIN); if nothing left, just ACK. 2665065Swnj */ 2675065Swnj if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 2685085Swnj todrop = 2695065Swnj ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 2705065Swnj if (todrop > ti->ti_len) 2715065Swnj goto dropafterack; 2725065Swnj m_adj(m, -todrop); 2735065Swnj ti->ti_len -= todrop; 2745085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2755065Swnj } 2765065Swnj } 2774601Swnj 2785065Swnj /* 2795065Swnj * If the RST bit is set examine the state: 2805065Swnj * SYN_RECEIVED STATE: 2815065Swnj * If passive open, return to LISTEN state. 2825065Swnj * If active open, inform user that connection was refused. 2835065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 2845065Swnj * Inform user that connection was reset, and close tcb. 2855065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 2865065Swnj * Close the tcb. 2875065Swnj */ 2885065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 2895065Swnj 2905065Swnj case TCPS_SYN_RECEIVED: 2915065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 2925085Swnj tp->t_state = TCPS_LISTEN; 2935085Swnj inp->inp_faddr.s_addr = 0; 2945065Swnj goto drop; 2954601Swnj } 2965085Swnj tcp_drop(tp, ECONNREFUSED); 2975065Swnj goto drop; 2984601Swnj 2995065Swnj case TCPS_ESTABLISHED: 3005065Swnj case TCPS_FIN_WAIT_1: 3015065Swnj case TCPS_FIN_WAIT_2: 3025065Swnj case TCPS_CLOSE_WAIT: 3035065Swnj tcp_drop(tp, ECONNRESET); 3045065Swnj goto drop; 3055065Swnj 3065065Swnj case TCPS_CLOSING: 3075065Swnj case TCPS_LAST_ACK: 3085065Swnj case TCPS_TIME_WAIT: 3095065Swnj tcp_close(tp); 3105065Swnj goto drop; 3114601Swnj } 3124601Swnj 3134601Swnj /* 3145065Swnj * If a SYN is in the window, then this is an 3155065Swnj * error and we send an RST and drop the connection. 3164601Swnj */ 3175065Swnj if (tiflags & TH_SYN) { 3185085Swnj tcp_drop(tp, ECONNABORTED); 3195085Swnj goto dropwithreset; 3204601Swnj } 3214601Swnj 3224601Swnj /* 3235065Swnj * If the ACK bit is off we drop the segment and return. 3244601Swnj */ 3255085Swnj if ((tiflags & TH_ACK) == 0) 3265065Swnj goto drop; 3275065Swnj 3285065Swnj /* 3295065Swnj * Ack processing. 3305065Swnj */ 3314601Swnj switch (tp->t_state) { 3324601Swnj 3335065Swnj /* 3345065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3355065Swnj * ESTABLISHED state and continue processing, othewise 3365065Swnj * send an RST. 3375065Swnj */ 3385065Swnj case TCPS_SYN_RECEIVED: 3395085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 3405085Swnj SEQ_GT(ti->ti_ack, tp->snd_nxt)) 3415085Swnj goto dropwithreset; 3425085Swnj soisconnected(so); 3435085Swnj tp->t_state = TCPS_ESTABLISHED; 344*5162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 3455085Swnj /* fall into ... */ 3464601Swnj 3475065Swnj /* 3485065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 3495065Swnj * ACKs. If the ack is in the range 3505065Swnj * tp->snd_una < ti->ti_ack <= tp->snd_nxt 3515065Swnj * then advance tp->snd_una to ti->ti_ack and drop 3525065Swnj * data from the retransmission queue. If this ACK reflects 3535065Swnj * more up to date window information we update our window information. 3545065Swnj */ 3555065Swnj case TCPS_ESTABLISHED: 3565065Swnj case TCPS_FIN_WAIT_1: 3575065Swnj case TCPS_FIN_WAIT_2: 3585065Swnj case TCPS_CLOSE_WAIT: 3595065Swnj case TCPS_CLOSING: 3605085Swnj #define ourfinisacked (acked > 0) 3615085Swnj 3625065Swnj if (SEQ_LT(ti->ti_ack, tp->snd_una)) 3635065Swnj break; 3645065Swnj if (SEQ_GT(ti->ti_ack, tp->snd_nxt)) 3655065Swnj goto dropafterack; 3665085Swnj acked = ti->ti_ack - tp->snd_una; 3675085Swnj if (acked > so->so_snd.sb_cc) { 3685085Swnj sbflush(&so->so_snd); 3695085Swnj acked -= so->so_snd.sb_cc; 370*5162Swnj /* if acked our FIN is acked */ 3715085Swnj } else { 3725085Swnj sbdrop(&so->so_snd, acked); 3735085Swnj acked = 0; 3745085Swnj } 375*5162Swnj 376*5162Swnj /* 377*5162Swnj * If transmit timer is running and timed sequence 378*5162Swnj * number was acked, update smoothed round trip time. 379*5162Swnj */ 380*5162Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 381*5162Swnj tp->t_srtt = 382*5162Swnj tcp_beta * tp->t_srtt + 383*5162Swnj (1 - tcp_beta) * tp->t_rtt; 384*5162Swnj tp->t_rtt = 0; 385*5162Swnj } 386*5162Swnj 3875065Swnj tp->snd_una = ti->ti_ack; 3885085Swnj 3895085Swnj /* 3905085Swnj * Update window information. 3915085Swnj */ 3925065Swnj if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 3935085Swnj tp->snd_wl1==ti->ti_seq && SEQ_LEQ(tp->snd_wl2,ti->ti_seq)) { 3945065Swnj tp->snd_wnd = ti->ti_win; 3955065Swnj tp->snd_wl1 = ti->ti_seq; 3965065Swnj tp->snd_wl2 = ti->ti_ack; 3974601Swnj } 3984601Swnj 3994601Swnj switch (tp->t_state) { 4004601Swnj 4015065Swnj /* 4025065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4035065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4045085Swnj * then enter FIN_WAIT_2. 4055065Swnj */ 4065065Swnj case TCPS_FIN_WAIT_1: 4075085Swnj if (ourfinisacked) 4085085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4094601Swnj break; 4104601Swnj 4115065Swnj /* 4125065Swnj * In CLOSING STATE in addition to the processing for 4135065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4145065Swnj * then enter the TIME-WAIT state, otherwise ignore 4155065Swnj * the segment. 4165065Swnj */ 4175065Swnj case TCPS_CLOSING: 4185085Swnj if (ourfinisacked) 4195065Swnj tp->t_state = TCPS_TIME_WAIT; 4205085Swnj goto drop; 4214601Swnj 4225065Swnj /* 4235085Swnj * The only thing that can arrive in LAST_ACK state 4245085Swnj * is an acknowledgment of our FIN. If our FIN is now 4255085Swnj * acknowledged, delete the TCB, enter the closed state 4265085Swnj * and return. 4275065Swnj */ 4285065Swnj case TCPS_LAST_ACK: 4295085Swnj if (ourfinisacked) 4305065Swnj tcp_close(tp); 4315065Swnj goto drop; 4324601Swnj 4335065Swnj /* 4345065Swnj * In TIME_WAIT state the only thing that should arrive 4355065Swnj * is a retransmission of the remote FIN. Acknowledge 4365065Swnj * it and restart the finack timer. 4375065Swnj */ 4385065Swnj case TCPS_TIME_WAIT: 439*5162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4405065Swnj goto dropafterack; 4414601Swnj } 4425085Swnj #undef ourfinisacked 4435085Swnj } 4444601Swnj 4455065Swnj step6: 4465065Swnj /* 4475065Swnj * If an URG bit is set in the segment and is greater than the 4485065Swnj * current known urgent pointer, then signal the user that the 4495065Swnj * remote side has urgent data. This should not happen 4505065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 4515065Swnj * a FIN has been received from the remote side. In these states 4525065Swnj * we ignore the URG. 4535065Swnj */ 4545085Swnj if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 4555085Swnj if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 4565065Swnj tp->rcv_up = ti->ti_urp; 4575085Swnj #if 0 4585065Swnj soisurgendata(so); /* XXX */ 4595085Swnj #endif 4604601Swnj } 4614601Swnj 4624601Swnj /* 4635065Swnj * Process the segment text, merging it into the TCP sequencing queue, 4645065Swnj * and arranging for acknowledgment of receipt if necessary. 4655065Swnj * This process logically involves adjusting tp->rcv_wnd as data 4665065Swnj * is presented to the user (this happens in tcp_usrreq.c, 4675065Swnj * case PRU_RCVD). If a FIN has already been received on this 4685065Swnj * connection then we just ignore the text. 4694601Swnj */ 4705065Swnj if (ti->ti_len) { 4715085Swnj if (TCPS_HAVERCVDFIN(tp->t_state)) 4725065Swnj goto drop; 4735085Swnj off += sizeof (struct ip); /* drop IP header */ 4745085Swnj m->m_off += off; 4755085Swnj m->m_len -= off; 4765065Swnj tiflags = tcp_reass(tp, ti); 4775085Swnj tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 4785085Swnj } else 4794924Swnj m_freem(m); 4804601Swnj 4814601Swnj /* 4825065Swnj * If FIN is received then if we haven't received SYN and 4835065Swnj * therefore can't validate drop the segment. Otherwise ACK 4845065Swnj * the FIN and let the user know that the connection is closing. 4854601Swnj */ 4865085Swnj if ((tiflags & TH_FIN)) { 4875074Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 4885074Swnj goto drop; 4895074Swnj socantrcvmore(so); 4905065Swnj tp->t_flags |= TF_ACKNOW; 4915065Swnj tp->rcv_nxt++; 4925065Swnj switch (tp->t_state) { 4934601Swnj 4945065Swnj /* 4955065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 4965065Swnj * enter the CLOSE_WAIT state. 4974884Swnj */ 4985065Swnj case TCPS_SYN_RECEIVED: 4995065Swnj case TCPS_ESTABLISHED: 5005065Swnj tp->t_state = TCPS_CLOSE_WAIT; 5015065Swnj break; 5024884Swnj 5035065Swnj /* 5045085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 5055085Swnj * enter the CLOSING state. 5064884Swnj */ 5075065Swnj case TCPS_FIN_WAIT_1: 5085085Swnj tp->t_state = TCPS_CLOSING; 5095065Swnj break; 5104601Swnj 5115065Swnj /* 5125065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 5135065Swnj * starting the time-wait timer, turning off the other 5145065Swnj * standard timers. 5155065Swnj */ 5165065Swnj case TCPS_FIN_WAIT_2: 5175085Swnj tp->t_state = TCPS_TIME_WAIT;; 5185074Swnj tcp_canceltimers(tp); 519*5162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5205065Swnj break; 5215065Swnj 5224884Swnj /* 5235065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 5244884Swnj */ 5255065Swnj case TCPS_TIME_WAIT: 526*5162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5275065Swnj break; 5285085Swnj } 5294601Swnj } 5305085Swnj 5315085Swnj /* 5325085Swnj * Return any desired output. 5335085Swnj */ 5345085Swnj tcp_output(tp); 5355065Swnj return; 5365085Swnj 5375065Swnj dropafterack: 5385085Swnj /* 5395085Swnj * Generate an ACK, then drop incoming segment. 5405085Swnj * Make ACK reflect our state. 5415085Swnj */ 5425085Swnj if (tiflags & TH_RST) 5435085Swnj goto drop; 5445085Swnj tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 5455085Swnj goto drop; 5465085Swnj 5475085Swnj dropwithreset: 5485085Swnj /* 5495085Swnj * Generate a RST, then drop incoming segment. 5505085Swnj * Make ACK acceptable to originator of segment. 5515085Swnj */ 5525085Swnj if (tiflags & TH_RST) 5535085Swnj goto drop; 5545085Swnj if (tiflags & TH_ACK) 5555109Swnj tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST); 5565085Swnj else { 5575085Swnj if (tiflags & TH_SYN) 5585085Swnj ti->ti_len++; 5595109Swnj tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 5605085Swnj } 5615085Swnj goto drop; 5625085Swnj 5635065Swnj drop: 5645085Swnj /* 5655085Swnj * Drop space held by incoming segment and return. 5665085Swnj */ 5675065Swnj m_freem(m); 5685065Swnj } 5695065Swnj 5705065Swnj /* 5715065Swnj * Insert segment ti into reassembly queue of tcp with 5725065Swnj * control block tp. Return TH_FIN if reassembly now includes 5735065Swnj * a segment with FIN. 5745065Swnj */ 5755109Swnj tcp_reass(tp, ti) 5765065Swnj register struct tcpcb *tp; 5775065Swnj register struct tcpiphdr *ti; 5785065Swnj { 5795065Swnj register struct tcpiphdr *q; 5805085Swnj struct socket *so = tp->t_inpcb->inp_socket; 5815065Swnj int flags = 0; /* no FIN */ 5825065Swnj int overage; 5835085Swnj COUNT(TCP_REASS); 5845065Swnj 5855065Swnj /* 586*5162Swnj * Call with ti==0 after become established to 587*5162Swnj * force pre-ESTABLISHED data up to user socket. 5885065Swnj */ 589*5162Swnj if (ti == 0) 5905065Swnj goto present; 5914601Swnj 5925065Swnj /* 5935065Swnj * Find a segment which begins after this one does. 5945065Swnj */ 5955065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 5965065Swnj q = (struct tcpiphdr *)q->ti_next) 5975065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 5985065Swnj break; 5994601Swnj 6005065Swnj /* 6015065Swnj * If there is a preceding segment, it may provide some of 6025065Swnj * our data already. If so, drop the data from the incoming 6035065Swnj * segment. If it provides all of our data, drop us. 6045065Swnj */ 6055065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 6065065Swnj register int i; 6075065Swnj q = (struct tcpiphdr *)(q->ti_prev); 6085065Swnj /* conversion to int (in i) handles seq wraparound */ 6095065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 6105065Swnj if (i > 0) { 6114924Swnj if (i >= ti->ti_len) 6125065Swnj goto drop; 6135065Swnj m_adj(dtom(tp), i); 6145065Swnj ti->ti_len -= i; 6154924Swnj ti->ti_seq += i; 6164601Swnj } 6175065Swnj q = (struct tcpiphdr *)(q->ti_next); 6185065Swnj } 6194601Swnj 6205065Swnj /* 6215065Swnj * While we overlap succeeding segments trim them or, 6225065Swnj * if they are completely covered, dequeue them. 6235065Swnj */ 6245065Swnj while (q != (struct tcpiphdr *)tp && 6255065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 6265065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 6275065Swnj if (i < q->ti_len) { 6285065Swnj q->ti_len -= i; 6295065Swnj m_adj(dtom(q), i); 6305065Swnj break; 6314601Swnj } 6325065Swnj q = (struct tcpiphdr *)q->ti_next; 6335065Swnj m_freem(dtom(q->ti_prev)); 6345065Swnj remque(q->ti_prev); 6355065Swnj } 6364601Swnj 6375065Swnj /* 6385065Swnj * Stick new segment in its place. 6395065Swnj */ 6405065Swnj insque(ti, q->ti_prev); 6414601Swnj 6425065Swnj /* 6435065Swnj * Advance rcv_next through newly completed sequence space. 6445065Swnj */ 6455065Swnj while (ti->ti_seq == tp->rcv_nxt) { 6465065Swnj tp->rcv_nxt += ti->ti_len; 6475065Swnj flags = ti->ti_flags & TH_FIN; 6485065Swnj ti = (struct tcpiphdr *)ti->ti_next; 6495065Swnj if (ti == (struct tcpiphdr *)tp) 6505065Swnj break; 6514679Swnj } 6524679Swnj 6535065Swnj present: 6545065Swnj /* 6555065Swnj * Present data to user. 6565065Swnj */ 6575085Swnj if (tp->t_state < TCPS_ESTABLISHED) 6585065Swnj return (flags); 6594924Swnj ti = tp->seg_next; 6604924Swnj while (ti != (struct tcpiphdr *)tp && ti->ti_seq < tp->rcv_nxt) { 6614924Swnj remque(ti); 6624924Swnj sbappend(&so->so_rcv, dtom(ti)); 6634924Swnj ti = (struct tcpiphdr *)ti->ti_next; 6644601Swnj } 6655074Swnj if (so->so_state & SS_CANTRCVMORE) 6665074Swnj sbflush(&so->so_rcv); 6675074Swnj else 6685074Swnj sorwakeup(so); 6695065Swnj return (flags); 6705065Swnj drop: 6715065Swnj m_freem(dtom(ti)); 6725065Swnj return (flags); 6734601Swnj } 674