1*10203Ssam /* tcp_input.c 1.86 83/01/08 */ 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: { 19910145Ssam 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; 20810145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 20910145Ssam if (am == NULL) 21010145Ssam goto drop; 21110145Ssam 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; 21710145Ssam 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 */ 367*10203Ssam if (so->so_state & SS_NOFDREF && tp->t_state > TCPS_CLOSE_WAIT && 368*10203Ssam ti->ti_len) { 3695951Swnj tcp_close(tp); 3706266Swnj tp = 0; 3715951Swnj goto dropwithreset; 3725951Swnj } 3735951Swnj 3745951Swnj /* 3755065Swnj * If the RST bit is set examine the state: 3765065Swnj * SYN_RECEIVED STATE: 3775065Swnj * If passive open, return to LISTEN state. 3785065Swnj * If active open, inform user that connection was refused. 3795065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3805065Swnj * Inform user that connection was reset, and close tcb. 3815065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3825065Swnj * Close the tcb. 3835065Swnj */ 3845065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3855267Sroot 3865065Swnj case TCPS_SYN_RECEIVED: 3875085Swnj tcp_drop(tp, ECONNREFUSED); 3886320Swnj tp = 0; 3895065Swnj goto drop; 3904601Swnj 3915065Swnj case TCPS_ESTABLISHED: 3925065Swnj case TCPS_FIN_WAIT_1: 3935065Swnj case TCPS_FIN_WAIT_2: 3945065Swnj case TCPS_CLOSE_WAIT: 3955065Swnj tcp_drop(tp, ECONNRESET); 3966320Swnj tp = 0; 3975065Swnj goto drop; 3985065Swnj 3995065Swnj case TCPS_CLOSING: 4005065Swnj case TCPS_LAST_ACK: 4015065Swnj case TCPS_TIME_WAIT: 4025065Swnj tcp_close(tp); 4036320Swnj tp = 0; 4045065Swnj goto drop; 4054601Swnj } 4064601Swnj 4074601Swnj /* 4085065Swnj * If a SYN is in the window, then this is an 4095065Swnj * error and we send an RST and drop the connection. 4104601Swnj */ 4115065Swnj if (tiflags & TH_SYN) { 4125231Swnj tcp_drop(tp, ECONNRESET); 4136266Swnj tp = 0; 4145085Swnj goto dropwithreset; 4154601Swnj } 4164601Swnj 4174601Swnj /* 4185065Swnj * If the ACK bit is off we drop the segment and return. 4194601Swnj */ 4205085Swnj if ((tiflags & TH_ACK) == 0) 4215065Swnj goto drop; 4225065Swnj 4235065Swnj /* 4245065Swnj * Ack processing. 4255065Swnj */ 4264601Swnj switch (tp->t_state) { 4274601Swnj 4285065Swnj /* 4295065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4305065Swnj * ESTABLISHED state and continue processing, othewise 4315065Swnj * send an RST. 4325065Swnj */ 4335065Swnj case TCPS_SYN_RECEIVED: 4345085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4355231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4365085Swnj goto dropwithreset; 4375244Sroot tp->snd_una++; /* SYN acked */ 4385357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4395357Sroot tp->snd_nxt = tp->snd_una; 4405244Sroot tp->t_timer[TCPT_REXMT] = 0; 4415085Swnj soisconnected(so); 4425085Swnj tp->t_state = TCPS_ESTABLISHED; 4435162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4445244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4455085Swnj /* fall into ... */ 4464601Swnj 4475065Swnj /* 4485065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4495065Swnj * ACKs. If the ack is in the range 4505231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4515065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4525065Swnj * data from the retransmission queue. If this ACK reflects 4535065Swnj * more up to date window information we update our window information. 4545065Swnj */ 4555065Swnj case TCPS_ESTABLISHED: 4565065Swnj case TCPS_FIN_WAIT_1: 4575065Swnj case TCPS_FIN_WAIT_2: 4585065Swnj case TCPS_CLOSE_WAIT: 4595065Swnj case TCPS_CLOSING: 4605244Sroot case TCPS_LAST_ACK: 4615244Sroot case TCPS_TIME_WAIT: 4625085Swnj #define ourfinisacked (acked > 0) 4635085Swnj 4645244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4655065Swnj break; 4665300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4675065Swnj goto dropafterack; 4685085Swnj acked = ti->ti_ack - tp->snd_una; 4695951Swnj 4705951Swnj /* 4715951Swnj * If transmit timer is running and timed sequence 4725951Swnj * number was acked, update smoothed round trip time. 4735951Swnj */ 4745951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4755951Swnj if (tp->t_srtt == 0) 4765951Swnj tp->t_srtt = tp->t_rtt; 4775951Swnj else 4785951Swnj tp->t_srtt = 4795951Swnj tcp_alpha * tp->t_srtt + 4805951Swnj (1 - tcp_alpha) * tp->t_rtt; 4815951Swnj tp->t_rtt = 0; 4825951Swnj } 4835951Swnj 4845307Sroot if (ti->ti_ack == tp->snd_max) 4855244Sroot tp->t_timer[TCPT_REXMT] = 0; 4865307Sroot else { 4875244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4885244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4895951Swnj tp->t_rtt = 1; 4905300Sroot tp->t_rxtshift = 0; 4915085Swnj } 4925307Sroot if (acked > so->so_snd.sb_cc) { 4935307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4945307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4955307Sroot } else { 4966161Ssam sbdrop(&so->so_snd, acked); 4975307Sroot tp->snd_wnd -= acked; 4985307Sroot acked = 0; 4995307Sroot } 5006434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 5015300Sroot sowwakeup(so); 5025231Swnj tp->snd_una = ti->ti_ack; 5035357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 5045357Sroot tp->snd_nxt = tp->snd_una; 5055162Swnj 5064601Swnj switch (tp->t_state) { 5074601Swnj 5085065Swnj /* 5095065Swnj * In FIN_WAIT_1 STATE in addition to the processing 5105065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 5115085Swnj * then enter FIN_WAIT_2. 5125065Swnj */ 5135065Swnj case TCPS_FIN_WAIT_1: 5145896Swnj if (ourfinisacked) { 5155896Swnj /* 5165896Swnj * If we can't receive any more 5175896Swnj * data, then closing user can proceed. 5185896Swnj */ 5195896Swnj if (so->so_state & SS_CANTRCVMORE) 5205896Swnj soisdisconnected(so); 5215085Swnj tp->t_state = TCPS_FIN_WAIT_2; 5225896Swnj } 5234601Swnj break; 5244601Swnj 5255065Swnj /* 5265065Swnj * In CLOSING STATE in addition to the processing for 5275065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5285065Swnj * then enter the TIME-WAIT state, otherwise ignore 5295065Swnj * the segment. 5305065Swnj */ 5315065Swnj case TCPS_CLOSING: 5325244Sroot if (ourfinisacked) { 5335065Swnj tp->t_state = TCPS_TIME_WAIT; 5345244Sroot tcp_canceltimers(tp); 5355244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5365244Sroot soisdisconnected(so); 5375244Sroot } 5385244Sroot break; 5394601Swnj 5405065Swnj /* 5415085Swnj * The only thing that can arrive in LAST_ACK state 5425085Swnj * is an acknowledgment of our FIN. If our FIN is now 5435085Swnj * acknowledged, delete the TCB, enter the closed state 5445085Swnj * and return. 5455065Swnj */ 5465065Swnj case TCPS_LAST_ACK: 5476320Swnj if (ourfinisacked) { 5485065Swnj tcp_close(tp); 5496320Swnj tp = 0; 5506320Swnj } 5515065Swnj goto drop; 5524601Swnj 5535065Swnj /* 5545065Swnj * In TIME_WAIT state the only thing that should arrive 5555065Swnj * is a retransmission of the remote FIN. Acknowledge 5565065Swnj * it and restart the finack timer. 5575065Swnj */ 5585065Swnj case TCPS_TIME_WAIT: 5595162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5605065Swnj goto dropafterack; 5614601Swnj } 5625085Swnj #undef ourfinisacked 5635085Swnj } 5644601Swnj 5655065Swnj step6: 5665065Swnj /* 5675244Sroot * Update window information. 5685244Sroot */ 5695300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5705391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5715300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5725244Sroot tp->snd_wnd = ti->ti_win; 5735244Sroot tp->snd_wl1 = ti->ti_seq; 5745244Sroot tp->snd_wl2 = ti->ti_ack; 5758599Sroot if (tp->snd_wnd != 0) 5765244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5775244Sroot } 5785244Sroot 5795244Sroot /* 5805547Swnj * Process segments with URG. 5815065Swnj */ 5827267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 5837267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5845547Swnj /* 5855547Swnj * If this segment advances the known urgent pointer, 5865547Swnj * then mark the data stream. This should not happen 5875547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5885547Swnj * a FIN has been received from the remote side. 5895547Swnj * In these states we ignore the URG. 5905547Swnj */ 5915547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5925547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 5935547Swnj so->so_oobmark = so->so_rcv.sb_cc + 5945547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 5955547Swnj if (so->so_oobmark == 0) 5965547Swnj so->so_state |= SS_RCVATMARK; 5978313Sroot sohasoutofband(so); 5985547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 5995440Swnj } 6005547Swnj /* 6015547Swnj * Remove out of band data so doesn't get presented to user. 6025547Swnj * This can happen independent of advancing the URG pointer, 6035547Swnj * but if two URG's are pending at once, some out-of-band 6045547Swnj * data may creep in... ick. 6055547Swnj */ 6067510Sroot if (ti->ti_urp <= ti->ti_len) 6075547Swnj tcp_pulloutofband(so, ti); 6085419Swnj } 6094601Swnj 6104601Swnj /* 6115065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6125065Swnj * and arranging for acknowledgment of receipt if necessary. 6135065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6145065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6155065Swnj * case PRU_RCVD). If a FIN has already been received on this 6165065Swnj * connection then we just ignore the text. 6174601Swnj */ 6185263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6195263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6205065Swnj tiflags = tcp_reass(tp, ti); 6215440Swnj if (tcpnodelack == 0) 6225440Swnj tp->t_flags |= TF_DELACK; 6235440Swnj else 6245440Swnj tp->t_flags |= TF_ACKNOW; 6255244Sroot } else { 6264924Swnj m_freem(m); 6275263Swnj tiflags &= ~TH_FIN; 6285244Sroot } 6294601Swnj 6304601Swnj /* 6315263Swnj * If FIN is received ACK the FIN and let the user know 6325263Swnj * that the connection is closing. 6334601Swnj */ 6345263Swnj if (tiflags & TH_FIN) { 6355244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6365244Sroot socantrcvmore(so); 6375244Sroot tp->t_flags |= TF_ACKNOW; 6385244Sroot tp->rcv_nxt++; 6395244Sroot } 6405065Swnj switch (tp->t_state) { 6414601Swnj 6425065Swnj /* 6435065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6445065Swnj * enter the CLOSE_WAIT state. 6454884Swnj */ 6465065Swnj case TCPS_SYN_RECEIVED: 6475065Swnj case TCPS_ESTABLISHED: 6485065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6495065Swnj break; 6504884Swnj 6515065Swnj /* 6525085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6535085Swnj * enter the CLOSING state. 6544884Swnj */ 6555065Swnj case TCPS_FIN_WAIT_1: 6565085Swnj tp->t_state = TCPS_CLOSING; 6575065Swnj break; 6584601Swnj 6595065Swnj /* 6605065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6615065Swnj * starting the time-wait timer, turning off the other 6625065Swnj * standard timers. 6635065Swnj */ 6645065Swnj case TCPS_FIN_WAIT_2: 6655244Sroot tp->t_state = TCPS_TIME_WAIT; 6665074Swnj tcp_canceltimers(tp); 6675162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6685244Sroot soisdisconnected(so); 6695065Swnj break; 6705065Swnj 6714884Swnj /* 6725065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6734884Swnj */ 6745065Swnj case TCPS_TIME_WAIT: 6755162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6765065Swnj break; 6775085Swnj } 6784601Swnj } 6795267Sroot if (so->so_options & SO_DEBUG) 6805267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6815085Swnj 6825085Swnj /* 6835085Swnj * Return any desired output. 6845085Swnj */ 6856161Ssam (void) tcp_output(tp); 6865065Swnj return; 6875085Swnj 6885065Swnj dropafterack: 6895085Swnj /* 6906211Swnj * Generate an ACK dropping incoming segment if it occupies 6916211Swnj * sequence space, where the ACK reflects our state. 6925085Swnj */ 6936211Swnj if ((tiflags&TH_RST) || 6946211Swnj tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 6955085Swnj goto drop; 6966303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 6976303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 6985391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6995231Swnj return; 7005085Swnj 7015085Swnj dropwithreset: 7025440Swnj if (om) 7036161Ssam (void) m_free(om); 7045085Swnj /* 7055244Sroot * Generate a RST, dropping incoming segment. 7065085Swnj * Make ACK acceptable to originator of segment. 7075085Swnj */ 7085085Swnj if (tiflags & TH_RST) 7095085Swnj goto drop; 7105085Swnj if (tiflags & TH_ACK) 7115391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7125085Swnj else { 7135085Swnj if (tiflags & TH_SYN) 7145085Swnj ti->ti_len++; 7156211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 7166211Swnj TH_RST|TH_ACK); 7175085Swnj } 7185231Swnj return; 7195085Swnj 7205065Swnj drop: 7215085Swnj /* 7225085Swnj * Drop space held by incoming segment and return. 7235085Swnj */ 7246303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 7256303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 7265065Swnj m_freem(m); 7275267Sroot return; 7285065Swnj } 7295065Swnj 7305440Swnj tcp_dooptions(tp, om) 7315440Swnj struct tcpcb *tp; 7325440Swnj struct mbuf *om; 7335419Swnj { 7345440Swnj register u_char *cp; 7355440Swnj int opt, optlen, cnt; 7365419Swnj 7375440Swnj cp = mtod(om, u_char *); 7385440Swnj cnt = om->m_len; 7395440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7405440Swnj opt = cp[0]; 7415440Swnj if (opt == TCPOPT_EOL) 7425440Swnj break; 7435440Swnj if (opt == TCPOPT_NOP) 7445440Swnj optlen = 1; 7455440Swnj else 7465440Swnj optlen = cp[1]; 7475440Swnj switch (opt) { 7485440Swnj 7495440Swnj default: 7505440Swnj break; 7515440Swnj 7525440Swnj case TCPOPT_MAXSEG: 7535440Swnj if (optlen != 4) 7545440Swnj continue; 7555440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7566161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7575440Swnj break; 7585419Swnj } 7595419Swnj } 7606161Ssam (void) m_free(om); 7615419Swnj } 7625419Swnj 7635419Swnj /* 7645547Swnj * Pull out of band byte out of a segment so 7655547Swnj * it doesn't appear in the user's data queue. 7665547Swnj * It is still reflected in the segment length for 7675547Swnj * sequencing purposes. 7685547Swnj */ 7695547Swnj tcp_pulloutofband(so, ti) 7705547Swnj struct socket *so; 7715547Swnj struct tcpiphdr *ti; 7725547Swnj { 7735547Swnj register struct mbuf *m; 7746116Swnj int cnt = ti->ti_urp - 1; 7755547Swnj 7765547Swnj m = dtom(ti); 7775547Swnj while (cnt >= 0) { 7785547Swnj if (m->m_len > cnt) { 7795547Swnj char *cp = mtod(m, caddr_t) + cnt; 7805547Swnj struct tcpcb *tp = sototcpcb(so); 7815547Swnj 7825547Swnj tp->t_iobc = *cp; 7835547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 7846161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 7855547Swnj m->m_len--; 7865547Swnj return; 7875547Swnj } 7885547Swnj cnt -= m->m_len; 7895547Swnj m = m->m_next; 7905547Swnj if (m == 0) 7915547Swnj break; 7925547Swnj } 7935547Swnj panic("tcp_pulloutofband"); 7945547Swnj } 7955547Swnj 7965547Swnj /* 7975065Swnj * Insert segment ti into reassembly queue of tcp with 7985065Swnj * control block tp. Return TH_FIN if reassembly now includes 7995065Swnj * a segment with FIN. 8005065Swnj */ 8015109Swnj tcp_reass(tp, ti) 8025065Swnj register struct tcpcb *tp; 8035065Swnj register struct tcpiphdr *ti; 8045065Swnj { 8055065Swnj register struct tcpiphdr *q; 8065085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8075263Swnj struct mbuf *m; 8085263Swnj int flags; 8095065Swnj 8105065Swnj /* 8115162Swnj * Call with ti==0 after become established to 8125162Swnj * force pre-ESTABLISHED data up to user socket. 8135065Swnj */ 8145162Swnj if (ti == 0) 8155065Swnj goto present; 8164601Swnj 8175065Swnj /* 8185065Swnj * Find a segment which begins after this one does. 8195065Swnj */ 8205065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8215065Swnj q = (struct tcpiphdr *)q->ti_next) 8225065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8235065Swnj break; 8244601Swnj 8255065Swnj /* 8265065Swnj * If there is a preceding segment, it may provide some of 8275065Swnj * our data already. If so, drop the data from the incoming 8285065Swnj * segment. If it provides all of our data, drop us. 8295065Swnj */ 8305065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8315065Swnj register int i; 8325690Swnj q = (struct tcpiphdr *)q->ti_prev; 8335065Swnj /* conversion to int (in i) handles seq wraparound */ 8345065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8355065Swnj if (i > 0) { 8364924Swnj if (i >= ti->ti_len) 8375065Swnj goto drop; 8387338Swnj m_adj(dtom(ti), i); 8395065Swnj ti->ti_len -= i; 8404924Swnj ti->ti_seq += i; 8414601Swnj } 8425065Swnj q = (struct tcpiphdr *)(q->ti_next); 8435065Swnj } 8444601Swnj 8455065Swnj /* 8465065Swnj * While we overlap succeeding segments trim them or, 8475065Swnj * if they are completely covered, dequeue them. 8485065Swnj */ 8495690Swnj while (q != (struct tcpiphdr *)tp) { 8505065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8515690Swnj if (i <= 0) 8525690Swnj break; 8535065Swnj if (i < q->ti_len) { 8545690Swnj q->ti_seq += i; 8555065Swnj q->ti_len -= i; 8565065Swnj m_adj(dtom(q), i); 8575065Swnj break; 8584601Swnj } 8595065Swnj q = (struct tcpiphdr *)q->ti_next; 8605623Swnj m = dtom(q->ti_prev); 8615065Swnj remque(q->ti_prev); 8625623Swnj m_freem(m); 8635065Swnj } 8644601Swnj 8655065Swnj /* 8665065Swnj * Stick new segment in its place. 8675065Swnj */ 8685065Swnj insque(ti, q->ti_prev); 8694601Swnj 8705065Swnj present: 8715065Swnj /* 8725244Sroot * Present data to user, advancing rcv_nxt through 8735244Sroot * completed sequence space. 8745065Swnj */ 8755263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 8765244Sroot return (0); 8774924Swnj ti = tp->seg_next; 8785263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 8795263Swnj return (0); 8805263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 8815263Swnj return (0); 8825263Swnj do { 8835244Sroot tp->rcv_nxt += ti->ti_len; 8845244Sroot flags = ti->ti_flags & TH_FIN; 8854924Swnj remque(ti); 8865263Swnj m = dtom(ti); 8874924Swnj ti = (struct tcpiphdr *)ti->ti_next; 8885263Swnj if (so->so_state & SS_CANTRCVMORE) 8896161Ssam m_freem(m); 89010145Ssam else 8915263Swnj sbappend(&so->so_rcv, m); 8925263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 8935263Swnj sorwakeup(so); 8945065Swnj return (flags); 8955065Swnj drop: 8965065Swnj m_freem(dtom(ti)); 8975263Swnj return (0); 8984601Swnj } 899