1*10145Ssam /* tcp_input.c 1.85 83/01/04 */ 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" 98401Swnj #include "../netinet/in.h" 106351Ssam #include "../net/route.h" 118401Swnj #include "../netinet/in_pcb.h" 128401Swnj #include "../netinet/in_systm.h" 135085Swnj #include "../net/if.h" 148401Swnj #include "../netinet/ip.h" 158401Swnj #include "../netinet/ip_var.h" 168401Swnj #include "../netinet/tcp.h" 178401Swnj #include "../netinet/tcp_fsm.h" 188401Swnj #include "../netinet/tcp_seq.h" 198401Swnj #include "../netinet/tcp_timer.h" 208401Swnj #include "../netinet/tcp_var.h" 218401Swnj #include "../netinet/tcpip.h" 228401Swnj #include "../netinet/tcp_debug.h" 237300Ssam #include <errno.h> 244601Swnj 255300Sroot int tcpprintfs = 0; 264679Swnj int tcpcksum = 1; 275267Sroot struct tcpiphdr tcp_saveti; 285440Swnj extern tcpnodelack; 294601Swnj 305267Sroot struct tcpcb *tcp_newtcpcb(); 315065Swnj /* 325065Swnj * TCP input routine, follows pages 65-76 of the 335065Swnj * protocol specification dated September, 1981 very closely. 345065Swnj */ 354924Swnj tcp_input(m0) 364924Swnj struct mbuf *m0; 374601Swnj { 384924Swnj register struct tcpiphdr *ti; 394924Swnj struct inpcb *inp; 404924Swnj register struct mbuf *m; 415440Swnj struct mbuf *om = 0; 424924Swnj int len, tlen, off; 435391Swnj register struct tcpcb *tp = 0; 444924Swnj register int tiflags; 454803Swnj struct socket *so; 465109Swnj int todrop, acked; 475267Sroot short ostate; 486028Sroot struct in_addr laddr; 494924Swnj 504924Swnj /* 515244Sroot * Get IP and TCP header together in first mbuf. 525244Sroot * Note: IP leaves IP header in first mbuf. 534924Swnj */ 544924Swnj m = m0; 555020Sroot ti = mtod(m, struct tcpiphdr *); 565244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 575208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 585307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 595307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 605085Swnj tcpstat.tcps_hdrops++; 615307Sroot return; 625085Swnj } 635085Swnj ti = mtod(m, struct tcpiphdr *); 645085Swnj } 654601Swnj 664601Swnj /* 675244Sroot * Checksum extended TCP header and data. 684601Swnj */ 694924Swnj tlen = ((struct ip *)ti)->ip_len; 704924Swnj len = sizeof (struct ip) + tlen; 714679Swnj if (tcpcksum) { 724924Swnj ti->ti_next = ti->ti_prev = 0; 734924Swnj ti->ti_x1 = 0; 745223Swnj ti->ti_len = (u_short)tlen; 756161Ssam ti->ti_len = htons((u_short)ti->ti_len); 765231Swnj if (ti->ti_sum = in_cksum(m, len)) { 774924Swnj tcpstat.tcps_badsum++; 786211Swnj if (tcpprintfs) 796211Swnj printf("tcp cksum %x\n", ti->ti_sum); 805085Swnj goto drop; 814601Swnj } 824601Swnj } 834601Swnj 844601Swnj /* 855244Sroot * Check that TCP offset makes sense, 865440Swnj * pull out TCP options and adjust length. 874601Swnj */ 884924Swnj off = ti->ti_off << 2; 895231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 904924Swnj tcpstat.tcps_badoff++; 915085Swnj goto drop; 924924Swnj } 936211Swnj tlen -= off; 946211Swnj ti->ti_len = tlen; 955440Swnj if (off > sizeof (struct tcphdr)) { 965440Swnj if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 975440Swnj tcpstat.tcps_hdrops++; 985440Swnj goto drop; 995440Swnj } 1005440Swnj ti = mtod(m, struct tcpiphdr *); 1019642Ssam om = m_get(M_DONTWAIT, MT_DATA); 1025440Swnj if (om == 0) 1035440Swnj goto drop; 1045440Swnj om->m_len = off - sizeof (struct tcphdr); 1055440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 1066161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 1075440Swnj m->m_len -= om->m_len; 1086161Ssam bcopy(op+om->m_len, op, 1096161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 1105440Swnj } 1115440Swnj } 1125065Swnj tiflags = ti->ti_flags; 1134924Swnj 1146093Sroot /* 1156211Swnj * Drop TCP and IP headers. 1166093Sroot */ 1176093Sroot off += sizeof (struct ip); 1186093Sroot m->m_off += off; 1196093Sroot m->m_len -= off; 1206093Sroot 1214924Swnj /* 1225244Sroot * Convert TCP protocol specific fields to host format. 1235085Swnj */ 1245085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1255085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1265085Swnj ti->ti_win = ntohs(ti->ti_win); 1275085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1285085Swnj 1295085Swnj /* 1308271Sroot * Locate pcb for segment. 1314924Swnj */ 1325065Swnj inp = in_pcblookup 1336028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 1346028Sroot INPLOOKUP_WILDCARD); 1355065Swnj 1365065Swnj /* 1375065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1385244Sroot * all data in the incoming segment is discarded. 1395065Swnj */ 1405300Sroot if (inp == 0) 1415085Swnj goto dropwithreset; 1425065Swnj tp = intotcpcb(inp); 1435300Sroot if (tp == 0) 1445085Swnj goto dropwithreset; 1455109Swnj so = inp->inp_socket; 1465267Sroot if (so->so_options & SO_DEBUG) { 1475267Sroot ostate = tp->t_state; 1485267Sroot tcp_saveti = *ti; 1495267Sroot } 1507510Sroot if (so->so_options & SO_ACCEPTCONN) { 1517510Sroot so = sonewconn(so); 1527510Sroot if (so == 0) 1537510Sroot goto drop; 1547510Sroot inp = (struct inpcb *)so->so_pcb; 1557510Sroot inp->inp_laddr = ti->ti_dst; 1567510Sroot inp->inp_lport = ti->ti_dport; 1577510Sroot tp = intotcpcb(inp); 1587510Sroot tp->t_state = TCPS_LISTEN; 1597510Sroot } 1604601Swnj 1614601Swnj /* 1625162Swnj * Segment received on connection. 1635162Swnj * Reset idle time and keep-alive timer. 1645162Swnj */ 1655162Swnj tp->t_idle = 0; 1665162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1675162Swnj 1685162Swnj /* 1695440Swnj * Process options. 1705440Swnj */ 1715440Swnj if (om) { 1725440Swnj tcp_dooptions(tp, om); 1735440Swnj om = 0; 1745440Swnj } 1755440Swnj 1765440Swnj /* 1775085Swnj * Calculate amount of space in receive window, 1785085Swnj * and then do TCP input processing. 1794601Swnj */ 1805085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1815231Swnj if (tp->rcv_wnd < 0) 1825231Swnj tp->rcv_wnd = 0; 1834601Swnj 1844601Swnj switch (tp->t_state) { 1854601Swnj 1865065Swnj /* 1875065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1885065Swnj * If the segment contains an ACK then it is bad and send a RST. 1895065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1905085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1915065Swnj * tp->iss, and send a segment: 1925085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1935065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1945065Swnj * Fill in remote peer address fields if not previously specified. 1955065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1965244Sroot * segment in this state. 1975065Swnj */ 1988271Sroot case TCPS_LISTEN: { 199*10145Ssam struct mbuf *am; 2008271Sroot register struct sockaddr_in *sin; 2018271Sroot 2025065Swnj if (tiflags & TH_RST) 2035065Swnj goto drop; 2045300Sroot if (tiflags & TH_ACK) 2055085Swnj goto dropwithreset; 2065300Sroot if ((tiflags & TH_SYN) == 0) 2075065Swnj goto drop; 208*10145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 209*10145Ssam if (am == NULL) 210*10145Ssam goto drop; 211*10145Ssam am->m_len = sizeof (struct sockaddr_in); 2128599Sroot sin = mtod(am, struct sockaddr_in *); 2138271Sroot sin->sin_family = AF_INET; 2148271Sroot sin->sin_addr = ti->ti_src; 2158271Sroot sin->sin_port = ti->ti_sport; 2166028Sroot laddr = inp->inp_laddr; 217*10145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 2186028Sroot inp->inp_laddr = ti->ti_dst; 2198599Sroot if (in_pcbconnect(inp, am)) { 2206028Sroot inp->inp_laddr = laddr; 2218716Sroot (void) m_free(am); 2225244Sroot goto drop; 2236028Sroot } 2248716Sroot (void) m_free(am); 2255244Sroot tp->t_template = tcp_template(tp); 2265244Sroot if (tp->t_template == 0) { 2275244Sroot in_pcbdisconnect(inp); 2286028Sroot inp->inp_laddr = laddr; 2296320Swnj tp = 0; 2305244Sroot goto drop; 2315244Sroot } 2325085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 2335065Swnj tp->irs = ti->ti_seq; 2345085Swnj tcp_sendseqinit(tp); 2355085Swnj tcp_rcvseqinit(tp); 2365065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2375244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 2385085Swnj goto trimthenstep6; 2398271Sroot } 2404601Swnj 2415065Swnj /* 2425065Swnj * If the state is SYN_SENT: 2435065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2445065Swnj * if seg contains a RST, then drop the connection. 2455065Swnj * if seg does not contain SYN, then drop it. 2465065Swnj * Otherwise this is an acceptable SYN segment 2475065Swnj * initialize tp->rcv_nxt and tp->irs 2485065Swnj * if seg contains ack then advance tp->snd_una 2495065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2505065Swnj * arrange for segment to be acked (eventually) 2515065Swnj * continue processing rest of data/controls, beginning with URG 2525065Swnj */ 2535065Swnj case TCPS_SYN_SENT: 2545065Swnj if ((tiflags & TH_ACK) && 2555300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2565300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2575231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2585085Swnj goto dropwithreset; 2595065Swnj if (tiflags & TH_RST) { 2606320Swnj if (tiflags & TH_ACK) { 2615267Sroot tcp_drop(tp, ECONNREFUSED); 2626320Swnj tp = 0; 2636320Swnj } 2645065Swnj goto drop; 2654601Swnj } 2665065Swnj if ((tiflags & TH_SYN) == 0) 2675065Swnj goto drop; 2685231Swnj tp->snd_una = ti->ti_ack; 2695357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2705357Sroot tp->snd_nxt = tp->snd_una; 2715244Sroot tp->t_timer[TCPT_REXMT] = 0; 2725065Swnj tp->irs = ti->ti_seq; 2735085Swnj tcp_rcvseqinit(tp); 2745085Swnj tp->t_flags |= TF_ACKNOW; 2755162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2765244Sroot soisconnected(so); 2775065Swnj tp->t_state = TCPS_ESTABLISHED; 2785162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2795162Swnj } else 2805085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2815085Swnj goto trimthenstep6; 2825085Swnj 2835085Swnj trimthenstep6: 2845085Swnj /* 2855231Swnj * Advance ti->ti_seq to correspond to first data byte. 2865085Swnj * If data, trim to stay within window, 2875085Swnj * dropping FIN if necessary. 2885085Swnj */ 2895231Swnj ti->ti_seq++; 2905085Swnj if (ti->ti_len > tp->rcv_wnd) { 2915085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2925085Swnj m_adj(m, -todrop); 2935085Swnj ti->ti_len = tp->rcv_wnd; 2945085Swnj ti->ti_flags &= ~TH_FIN; 2955065Swnj } 2965263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2975085Swnj goto step6; 2985065Swnj } 2994601Swnj 3005065Swnj /* 3015065Swnj * States other than LISTEN or SYN_SENT. 3025065Swnj * First check that at least some bytes of segment are within 3035065Swnj * receive window. 3045065Swnj */ 3055065Swnj if (tp->rcv_wnd == 0) { 3065065Swnj /* 3075065Swnj * If window is closed can only take segments at 3085231Swnj * window edge, and have to drop data and PUSH from 3095065Swnj * incoming segments. 3105065Swnj */ 3115300Sroot if (tp->rcv_nxt != ti->ti_seq) 3125065Swnj goto dropafterack; 3135085Swnj if (ti->ti_len > 0) { 3145690Swnj m_adj(m, ti->ti_len); 3155085Swnj ti->ti_len = 0; 3165085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3175065Swnj } 3185065Swnj } else { 3195065Swnj /* 3205231Swnj * If segment begins before rcv_nxt, drop leading 3215065Swnj * data (and SYN); if nothing left, just ack. 3225065Swnj */ 3235690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 3245690Swnj if (todrop > 0) { 3255085Swnj if (tiflags & TH_SYN) { 3265300Sroot tiflags &= ~TH_SYN; 3275690Swnj ti->ti_flags &= ~TH_SYN; 3285085Swnj ti->ti_seq++; 3295085Swnj if (ti->ti_urp > 1) 3305085Swnj ti->ti_urp--; 3315085Swnj else 3325085Swnj tiflags &= ~TH_URG; 3335085Swnj todrop--; 3345085Swnj } 3356211Swnj if (todrop > ti->ti_len || 3366211Swnj todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 3375065Swnj goto dropafterack; 3385065Swnj m_adj(m, todrop); 3395065Swnj ti->ti_seq += todrop; 3405065Swnj ti->ti_len -= todrop; 3415085Swnj if (ti->ti_urp > todrop) 3425085Swnj ti->ti_urp -= todrop; 3435085Swnj else { 3445085Swnj tiflags &= ~TH_URG; 3455690Swnj ti->ti_flags &= ~TH_URG; 3465690Swnj ti->ti_urp = 0; 3475085Swnj } 3485065Swnj } 3495065Swnj /* 3505065Swnj * If segment ends after window, drop trailing data 3515085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3525065Swnj */ 3535690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3545690Swnj if (todrop > 0) { 3556211Swnj if (todrop >= ti->ti_len) 3565065Swnj goto dropafterack; 3575065Swnj m_adj(m, -todrop); 3585065Swnj ti->ti_len -= todrop; 3595085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3605065Swnj } 3615065Swnj } 3624601Swnj 3635065Swnj /* 36410013Ssam * If data is received on a connection after the 3655951Swnj * user processes are gone, then RST the other end. 3665951Swnj */ 36710013Ssam if ((so->so_state & SS_NOFDREF) && ti->ti_len) { 3685951Swnj tcp_close(tp); 3696266Swnj tp = 0; 3705951Swnj goto dropwithreset; 3715951Swnj } 3725951Swnj 3735951Swnj /* 3745065Swnj * If the RST bit is set examine the state: 3755065Swnj * SYN_RECEIVED STATE: 3765065Swnj * If passive open, return to LISTEN state. 3775065Swnj * If active open, inform user that connection was refused. 3785065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3795065Swnj * Inform user that connection was reset, and close tcb. 3805065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3815065Swnj * Close the tcb. 3825065Swnj */ 3835065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3845267Sroot 3855065Swnj case TCPS_SYN_RECEIVED: 3865085Swnj tcp_drop(tp, ECONNREFUSED); 3876320Swnj tp = 0; 3885065Swnj goto drop; 3894601Swnj 3905065Swnj case TCPS_ESTABLISHED: 3915065Swnj case TCPS_FIN_WAIT_1: 3925065Swnj case TCPS_FIN_WAIT_2: 3935065Swnj case TCPS_CLOSE_WAIT: 3945065Swnj tcp_drop(tp, ECONNRESET); 3956320Swnj tp = 0; 3965065Swnj goto drop; 3975065Swnj 3985065Swnj case TCPS_CLOSING: 3995065Swnj case TCPS_LAST_ACK: 4005065Swnj case TCPS_TIME_WAIT: 4015065Swnj tcp_close(tp); 4026320Swnj tp = 0; 4035065Swnj goto drop; 4044601Swnj } 4054601Swnj 4064601Swnj /* 4075065Swnj * If a SYN is in the window, then this is an 4085065Swnj * error and we send an RST and drop the connection. 4094601Swnj */ 4105065Swnj if (tiflags & TH_SYN) { 4115231Swnj tcp_drop(tp, ECONNRESET); 4126266Swnj tp = 0; 4135085Swnj goto dropwithreset; 4144601Swnj } 4154601Swnj 4164601Swnj /* 4175065Swnj * If the ACK bit is off we drop the segment and return. 4184601Swnj */ 4195085Swnj if ((tiflags & TH_ACK) == 0) 4205065Swnj goto drop; 4215065Swnj 4225065Swnj /* 4235065Swnj * Ack processing. 4245065Swnj */ 4254601Swnj switch (tp->t_state) { 4264601Swnj 4275065Swnj /* 4285065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4295065Swnj * ESTABLISHED state and continue processing, othewise 4305065Swnj * send an RST. 4315065Swnj */ 4325065Swnj case TCPS_SYN_RECEIVED: 4335085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4345231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4355085Swnj goto dropwithreset; 4365244Sroot tp->snd_una++; /* SYN acked */ 4375357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4385357Sroot tp->snd_nxt = tp->snd_una; 4395244Sroot tp->t_timer[TCPT_REXMT] = 0; 4405085Swnj soisconnected(so); 4415085Swnj tp->t_state = TCPS_ESTABLISHED; 4425162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4435244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4445085Swnj /* fall into ... */ 4454601Swnj 4465065Swnj /* 4475065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4485065Swnj * ACKs. If the ack is in the range 4495231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4505065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4515065Swnj * data from the retransmission queue. If this ACK reflects 4525065Swnj * more up to date window information we update our window information. 4535065Swnj */ 4545065Swnj case TCPS_ESTABLISHED: 4555065Swnj case TCPS_FIN_WAIT_1: 4565065Swnj case TCPS_FIN_WAIT_2: 4575065Swnj case TCPS_CLOSE_WAIT: 4585065Swnj case TCPS_CLOSING: 4595244Sroot case TCPS_LAST_ACK: 4605244Sroot case TCPS_TIME_WAIT: 4615085Swnj #define ourfinisacked (acked > 0) 4625085Swnj 4635244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4645065Swnj break; 4655300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4665065Swnj goto dropafterack; 4675085Swnj acked = ti->ti_ack - tp->snd_una; 4685951Swnj 4695951Swnj /* 4705951Swnj * If transmit timer is running and timed sequence 4715951Swnj * number was acked, update smoothed round trip time. 4725951Swnj */ 4735951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4745951Swnj if (tp->t_srtt == 0) 4755951Swnj tp->t_srtt = tp->t_rtt; 4765951Swnj else 4775951Swnj tp->t_srtt = 4785951Swnj tcp_alpha * tp->t_srtt + 4795951Swnj (1 - tcp_alpha) * tp->t_rtt; 4805951Swnj tp->t_rtt = 0; 4815951Swnj } 4825951Swnj 4835307Sroot if (ti->ti_ack == tp->snd_max) 4845244Sroot tp->t_timer[TCPT_REXMT] = 0; 4855307Sroot else { 4865244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4875244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4885951Swnj tp->t_rtt = 1; 4895300Sroot tp->t_rxtshift = 0; 4905085Swnj } 4915307Sroot if (acked > so->so_snd.sb_cc) { 4925307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4935307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4945307Sroot } else { 4956161Ssam sbdrop(&so->so_snd, acked); 4965307Sroot tp->snd_wnd -= acked; 4975307Sroot acked = 0; 4985307Sroot } 4996434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 5005300Sroot sowwakeup(so); 5015231Swnj tp->snd_una = ti->ti_ack; 5025357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 5035357Sroot tp->snd_nxt = tp->snd_una; 5045162Swnj 5054601Swnj switch (tp->t_state) { 5064601Swnj 5075065Swnj /* 5085065Swnj * In FIN_WAIT_1 STATE in addition to the processing 5095065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 5105085Swnj * then enter FIN_WAIT_2. 5115065Swnj */ 5125065Swnj case TCPS_FIN_WAIT_1: 5135896Swnj if (ourfinisacked) { 5145896Swnj /* 5155896Swnj * If we can't receive any more 5165896Swnj * data, then closing user can proceed. 5175896Swnj */ 5185896Swnj if (so->so_state & SS_CANTRCVMORE) 5195896Swnj soisdisconnected(so); 5205085Swnj tp->t_state = TCPS_FIN_WAIT_2; 5215896Swnj } 5224601Swnj break; 5234601Swnj 5245065Swnj /* 5255065Swnj * In CLOSING STATE in addition to the processing for 5265065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5275065Swnj * then enter the TIME-WAIT state, otherwise ignore 5285065Swnj * the segment. 5295065Swnj */ 5305065Swnj case TCPS_CLOSING: 5315244Sroot if (ourfinisacked) { 5325065Swnj tp->t_state = TCPS_TIME_WAIT; 5335244Sroot tcp_canceltimers(tp); 5345244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5355244Sroot soisdisconnected(so); 5365244Sroot } 5375244Sroot break; 5384601Swnj 5395065Swnj /* 5405085Swnj * The only thing that can arrive in LAST_ACK state 5415085Swnj * is an acknowledgment of our FIN. If our FIN is now 5425085Swnj * acknowledged, delete the TCB, enter the closed state 5435085Swnj * and return. 5445065Swnj */ 5455065Swnj case TCPS_LAST_ACK: 5466320Swnj if (ourfinisacked) { 5475065Swnj tcp_close(tp); 5486320Swnj tp = 0; 5496320Swnj } 5505065Swnj goto drop; 5514601Swnj 5525065Swnj /* 5535065Swnj * In TIME_WAIT state the only thing that should arrive 5545065Swnj * is a retransmission of the remote FIN. Acknowledge 5555065Swnj * it and restart the finack timer. 5565065Swnj */ 5575065Swnj case TCPS_TIME_WAIT: 5585162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5595065Swnj goto dropafterack; 5604601Swnj } 5615085Swnj #undef ourfinisacked 5625085Swnj } 5634601Swnj 5645065Swnj step6: 5655065Swnj /* 5665244Sroot * Update window information. 5675244Sroot */ 5685300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5695391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5705300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5715244Sroot tp->snd_wnd = ti->ti_win; 5725244Sroot tp->snd_wl1 = ti->ti_seq; 5735244Sroot tp->snd_wl2 = ti->ti_ack; 5748599Sroot if (tp->snd_wnd != 0) 5755244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5765244Sroot } 5775244Sroot 5785244Sroot /* 5795547Swnj * Process segments with URG. 5805065Swnj */ 5817267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 5827267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5835547Swnj /* 5845547Swnj * If this segment advances the known urgent pointer, 5855547Swnj * then mark the data stream. This should not happen 5865547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5875547Swnj * a FIN has been received from the remote side. 5885547Swnj * In these states we ignore the URG. 5895547Swnj */ 5905547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5915547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 5925547Swnj so->so_oobmark = so->so_rcv.sb_cc + 5935547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 5945547Swnj if (so->so_oobmark == 0) 5955547Swnj so->so_state |= SS_RCVATMARK; 5968313Sroot sohasoutofband(so); 5975547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 5985440Swnj } 5995547Swnj /* 6005547Swnj * Remove out of band data so doesn't get presented to user. 6015547Swnj * This can happen independent of advancing the URG pointer, 6025547Swnj * but if two URG's are pending at once, some out-of-band 6035547Swnj * data may creep in... ick. 6045547Swnj */ 6057510Sroot if (ti->ti_urp <= ti->ti_len) 6065547Swnj tcp_pulloutofband(so, ti); 6075419Swnj } 6084601Swnj 6094601Swnj /* 6105065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6115065Swnj * and arranging for acknowledgment of receipt if necessary. 6125065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6135065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6145065Swnj * case PRU_RCVD). If a FIN has already been received on this 6155065Swnj * connection then we just ignore the text. 6164601Swnj */ 6175263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6185263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6195065Swnj tiflags = tcp_reass(tp, ti); 6205440Swnj if (tcpnodelack == 0) 6215440Swnj tp->t_flags |= TF_DELACK; 6225440Swnj else 6235440Swnj tp->t_flags |= TF_ACKNOW; 6245244Sroot } else { 6254924Swnj m_freem(m); 6265263Swnj tiflags &= ~TH_FIN; 6275244Sroot } 6284601Swnj 6294601Swnj /* 6305263Swnj * If FIN is received ACK the FIN and let the user know 6315263Swnj * that the connection is closing. 6324601Swnj */ 6335263Swnj if (tiflags & TH_FIN) { 6345244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6355244Sroot socantrcvmore(so); 6365244Sroot tp->t_flags |= TF_ACKNOW; 6375244Sroot tp->rcv_nxt++; 6385244Sroot } 6395065Swnj switch (tp->t_state) { 6404601Swnj 6415065Swnj /* 6425065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6435065Swnj * enter the CLOSE_WAIT state. 6444884Swnj */ 6455065Swnj case TCPS_SYN_RECEIVED: 6465065Swnj case TCPS_ESTABLISHED: 6475065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6485065Swnj break; 6494884Swnj 6505065Swnj /* 6515085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6525085Swnj * enter the CLOSING state. 6534884Swnj */ 6545065Swnj case TCPS_FIN_WAIT_1: 6555085Swnj tp->t_state = TCPS_CLOSING; 6565065Swnj break; 6574601Swnj 6585065Swnj /* 6595065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6605065Swnj * starting the time-wait timer, turning off the other 6615065Swnj * standard timers. 6625065Swnj */ 6635065Swnj case TCPS_FIN_WAIT_2: 6645244Sroot tp->t_state = TCPS_TIME_WAIT; 6655074Swnj tcp_canceltimers(tp); 6665162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6675244Sroot soisdisconnected(so); 6685065Swnj break; 6695065Swnj 6704884Swnj /* 6715065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6724884Swnj */ 6735065Swnj case TCPS_TIME_WAIT: 6745162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6755065Swnj break; 6765085Swnj } 6774601Swnj } 6785267Sroot if (so->so_options & SO_DEBUG) 6795267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6805085Swnj 6815085Swnj /* 6825085Swnj * Return any desired output. 6835085Swnj */ 6846161Ssam (void) tcp_output(tp); 6855065Swnj return; 6865085Swnj 6875065Swnj dropafterack: 6885085Swnj /* 6896211Swnj * Generate an ACK dropping incoming segment if it occupies 6906211Swnj * sequence space, where the ACK reflects our state. 6915085Swnj */ 6926211Swnj if ((tiflags&TH_RST) || 6936211Swnj tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 6945085Swnj goto drop; 6956303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 6966303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 6975391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6985231Swnj return; 6995085Swnj 7005085Swnj dropwithreset: 7015440Swnj if (om) 7026161Ssam (void) m_free(om); 7035085Swnj /* 7045244Sroot * Generate a RST, dropping incoming segment. 7055085Swnj * Make ACK acceptable to originator of segment. 7065085Swnj */ 7075085Swnj if (tiflags & TH_RST) 7085085Swnj goto drop; 7095085Swnj if (tiflags & TH_ACK) 7105391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7115085Swnj else { 7125085Swnj if (tiflags & TH_SYN) 7135085Swnj ti->ti_len++; 7146211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 7156211Swnj TH_RST|TH_ACK); 7165085Swnj } 7175231Swnj return; 7185085Swnj 7195065Swnj drop: 7205085Swnj /* 7215085Swnj * Drop space held by incoming segment and return. 7225085Swnj */ 7236303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 7246303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 7255065Swnj m_freem(m); 7265267Sroot return; 7275065Swnj } 7285065Swnj 7295440Swnj tcp_dooptions(tp, om) 7305440Swnj struct tcpcb *tp; 7315440Swnj struct mbuf *om; 7325419Swnj { 7335440Swnj register u_char *cp; 7345440Swnj int opt, optlen, cnt; 7355419Swnj 7365440Swnj cp = mtod(om, u_char *); 7375440Swnj cnt = om->m_len; 7385440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7395440Swnj opt = cp[0]; 7405440Swnj if (opt == TCPOPT_EOL) 7415440Swnj break; 7425440Swnj if (opt == TCPOPT_NOP) 7435440Swnj optlen = 1; 7445440Swnj else 7455440Swnj optlen = cp[1]; 7465440Swnj switch (opt) { 7475440Swnj 7485440Swnj default: 7495440Swnj break; 7505440Swnj 7515440Swnj case TCPOPT_MAXSEG: 7525440Swnj if (optlen != 4) 7535440Swnj continue; 7545440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7556161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7565440Swnj break; 7575419Swnj } 7585419Swnj } 7596161Ssam (void) m_free(om); 7605419Swnj } 7615419Swnj 7625419Swnj /* 7635547Swnj * Pull out of band byte out of a segment so 7645547Swnj * it doesn't appear in the user's data queue. 7655547Swnj * It is still reflected in the segment length for 7665547Swnj * sequencing purposes. 7675547Swnj */ 7685547Swnj tcp_pulloutofband(so, ti) 7695547Swnj struct socket *so; 7705547Swnj struct tcpiphdr *ti; 7715547Swnj { 7725547Swnj register struct mbuf *m; 7736116Swnj int cnt = ti->ti_urp - 1; 7745547Swnj 7755547Swnj m = dtom(ti); 7765547Swnj while (cnt >= 0) { 7775547Swnj if (m->m_len > cnt) { 7785547Swnj char *cp = mtod(m, caddr_t) + cnt; 7795547Swnj struct tcpcb *tp = sototcpcb(so); 7805547Swnj 7815547Swnj tp->t_iobc = *cp; 7825547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 7836161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 7845547Swnj m->m_len--; 7855547Swnj return; 7865547Swnj } 7875547Swnj cnt -= m->m_len; 7885547Swnj m = m->m_next; 7895547Swnj if (m == 0) 7905547Swnj break; 7915547Swnj } 7925547Swnj panic("tcp_pulloutofband"); 7935547Swnj } 7945547Swnj 7955547Swnj /* 7965065Swnj * Insert segment ti into reassembly queue of tcp with 7975065Swnj * control block tp. Return TH_FIN if reassembly now includes 7985065Swnj * a segment with FIN. 7995065Swnj */ 8005109Swnj tcp_reass(tp, ti) 8015065Swnj register struct tcpcb *tp; 8025065Swnj register struct tcpiphdr *ti; 8035065Swnj { 8045065Swnj register struct tcpiphdr *q; 8055085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8065263Swnj struct mbuf *m; 8075263Swnj int flags; 8085065Swnj 8095065Swnj /* 8105162Swnj * Call with ti==0 after become established to 8115162Swnj * force pre-ESTABLISHED data up to user socket. 8125065Swnj */ 8135162Swnj if (ti == 0) 8145065Swnj goto present; 8154601Swnj 8165065Swnj /* 8175065Swnj * Find a segment which begins after this one does. 8185065Swnj */ 8195065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8205065Swnj q = (struct tcpiphdr *)q->ti_next) 8215065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8225065Swnj break; 8234601Swnj 8245065Swnj /* 8255065Swnj * If there is a preceding segment, it may provide some of 8265065Swnj * our data already. If so, drop the data from the incoming 8275065Swnj * segment. If it provides all of our data, drop us. 8285065Swnj */ 8295065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8305065Swnj register int i; 8315690Swnj q = (struct tcpiphdr *)q->ti_prev; 8325065Swnj /* conversion to int (in i) handles seq wraparound */ 8335065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8345065Swnj if (i > 0) { 8354924Swnj if (i >= ti->ti_len) 8365065Swnj goto drop; 8377338Swnj m_adj(dtom(ti), i); 8385065Swnj ti->ti_len -= i; 8394924Swnj ti->ti_seq += i; 8404601Swnj } 8415065Swnj q = (struct tcpiphdr *)(q->ti_next); 8425065Swnj } 8434601Swnj 8445065Swnj /* 8455065Swnj * While we overlap succeeding segments trim them or, 8465065Swnj * if they are completely covered, dequeue them. 8475065Swnj */ 8485690Swnj while (q != (struct tcpiphdr *)tp) { 8495065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8505690Swnj if (i <= 0) 8515690Swnj break; 8525065Swnj if (i < q->ti_len) { 8535690Swnj q->ti_seq += i; 8545065Swnj q->ti_len -= i; 8555065Swnj m_adj(dtom(q), i); 8565065Swnj break; 8574601Swnj } 8585065Swnj q = (struct tcpiphdr *)q->ti_next; 8595623Swnj m = dtom(q->ti_prev); 8605065Swnj remque(q->ti_prev); 8615623Swnj m_freem(m); 8625065Swnj } 8634601Swnj 8645065Swnj /* 8655065Swnj * Stick new segment in its place. 8665065Swnj */ 8675065Swnj insque(ti, q->ti_prev); 8684601Swnj 8695065Swnj present: 8705065Swnj /* 8715244Sroot * Present data to user, advancing rcv_nxt through 8725244Sroot * completed sequence space. 8735065Swnj */ 8745263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 8755244Sroot return (0); 8764924Swnj ti = tp->seg_next; 8775263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 8785263Swnj return (0); 8795263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 8805263Swnj return (0); 8815263Swnj do { 8825244Sroot tp->rcv_nxt += ti->ti_len; 8835244Sroot flags = ti->ti_flags & TH_FIN; 8844924Swnj remque(ti); 8855263Swnj m = dtom(ti); 8864924Swnj ti = (struct tcpiphdr *)ti->ti_next; 8875263Swnj if (so->so_state & SS_CANTRCVMORE) 8886161Ssam m_freem(m); 889*10145Ssam else 8905263Swnj sbappend(&so->so_rcv, m); 8915263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 8925263Swnj sorwakeup(so); 8935065Swnj return (flags); 8945065Swnj drop: 8955065Swnj m_freem(dtom(ti)); 8965263Swnj return (0); 8974601Swnj } 898