1*17264Skarels /* tcp_input.c 6.5 84/10/18 */ 24601Swnj 317062Sbloom #include "param.h" 417062Sbloom #include "systm.h" 517062Sbloom #include "mbuf.h" 617062Sbloom #include "protosw.h" 717062Sbloom #include "socket.h" 817062Sbloom #include "socketvar.h" 917062Sbloom #include "errno.h" 1010894Ssam 1110894Ssam #include "../net/if.h" 1210894Ssam #include "../net/route.h" 1310894Ssam 1417062Sbloom #include "in.h" 1517062Sbloom #include "in_pcb.h" 1617062Sbloom #include "in_systm.h" 1717062Sbloom #include "ip.h" 1817062Sbloom #include "ip_var.h" 1917062Sbloom #include "tcp.h" 2017062Sbloom #include "tcp_fsm.h" 2117062Sbloom #include "tcp_seq.h" 2217062Sbloom #include "tcp_timer.h" 2317062Sbloom #include "tcp_var.h" 2417062Sbloom #include "tcpip.h" 2517062Sbloom #include "tcp_debug.h" 264601Swnj 275300Sroot int tcpprintfs = 0; 284679Swnj int tcpcksum = 1; 295267Sroot struct tcpiphdr tcp_saveti; 305440Swnj extern tcpnodelack; 314601Swnj 325267Sroot struct tcpcb *tcp_newtcpcb(); 335065Swnj /* 345065Swnj * TCP input routine, follows pages 65-76 of the 355065Swnj * protocol specification dated September, 1981 very closely. 365065Swnj */ 374924Swnj tcp_input(m0) 384924Swnj struct mbuf *m0; 394601Swnj { 404924Swnj register struct tcpiphdr *ti; 414924Swnj struct inpcb *inp; 424924Swnj register struct mbuf *m; 435440Swnj struct mbuf *om = 0; 444924Swnj int len, tlen, off; 455391Swnj register struct tcpcb *tp = 0; 464924Swnj register int tiflags; 474803Swnj struct socket *so; 485109Swnj int todrop, acked; 495267Sroot short ostate; 506028Sroot struct in_addr laddr; 5110769Ssam int dropsocket = 0; 524924Swnj 534924Swnj /* 545244Sroot * Get IP and TCP header together in first mbuf. 555244Sroot * Note: IP leaves IP header in first mbuf. 564924Swnj */ 574924Swnj m = m0; 585020Sroot ti = mtod(m, struct tcpiphdr *); 595244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 605208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 615307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 625307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 635085Swnj tcpstat.tcps_hdrops++; 645307Sroot return; 655085Swnj } 665085Swnj ti = mtod(m, struct tcpiphdr *); 675085Swnj } 684601Swnj 694601Swnj /* 705244Sroot * Checksum extended TCP header and data. 714601Swnj */ 724924Swnj tlen = ((struct ip *)ti)->ip_len; 734924Swnj len = sizeof (struct ip) + tlen; 744679Swnj if (tcpcksum) { 754924Swnj ti->ti_next = ti->ti_prev = 0; 764924Swnj ti->ti_x1 = 0; 775223Swnj ti->ti_len = (u_short)tlen; 786161Ssam ti->ti_len = htons((u_short)ti->ti_len); 795231Swnj if (ti->ti_sum = in_cksum(m, len)) { 8011830Ssam if (tcpprintfs) 8111830Ssam printf("tcp sum: src %x\n", ti->ti_src); 824924Swnj tcpstat.tcps_badsum++; 835085Swnj goto drop; 844601Swnj } 854601Swnj } 864601Swnj 874601Swnj /* 885244Sroot * Check that TCP offset makes sense, 895440Swnj * pull out TCP options and adjust length. 904601Swnj */ 914924Swnj off = ti->ti_off << 2; 925231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 9311830Ssam if (tcpprintfs) 9411830Ssam printf("tcp off: src %x off %d\n", ti->ti_src, off); 954924Swnj tcpstat.tcps_badoff++; 965085Swnj goto drop; 974924Swnj } 986211Swnj tlen -= off; 996211Swnj ti->ti_len = tlen; 1005440Swnj if (off > sizeof (struct tcphdr)) { 1015440Swnj if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 1025440Swnj tcpstat.tcps_hdrops++; 10311730Ssam return; 1045440Swnj } 1055440Swnj ti = mtod(m, struct tcpiphdr *); 1069642Ssam om = m_get(M_DONTWAIT, MT_DATA); 1075440Swnj if (om == 0) 1085440Swnj goto drop; 1095440Swnj om->m_len = off - sizeof (struct tcphdr); 1105440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 1116161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 1125440Swnj m->m_len -= om->m_len; 1136161Ssam bcopy(op+om->m_len, op, 1146161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 1155440Swnj } 1165440Swnj } 1175065Swnj tiflags = ti->ti_flags; 1184924Swnj 1196093Sroot /* 1206211Swnj * Drop TCP and IP headers. 1216093Sroot */ 1226093Sroot off += sizeof (struct ip); 1236093Sroot m->m_off += off; 1246093Sroot m->m_len -= off; 1256093Sroot 1264924Swnj /* 1275244Sroot * Convert TCP protocol specific fields to host format. 1285085Swnj */ 1295085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1305085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1315085Swnj ti->ti_win = ntohs(ti->ti_win); 1325085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1335085Swnj 1345085Swnj /* 1358271Sroot * Locate pcb for segment. 1364924Swnj */ 1375065Swnj inp = in_pcblookup 1386028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 1396028Sroot INPLOOKUP_WILDCARD); 1405065Swnj 1415065Swnj /* 1425065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1435244Sroot * all data in the incoming segment is discarded. 1445065Swnj */ 1455300Sroot if (inp == 0) 1465085Swnj goto dropwithreset; 1475065Swnj tp = intotcpcb(inp); 1485300Sroot if (tp == 0) 1495085Swnj goto dropwithreset; 1505109Swnj so = inp->inp_socket; 1515267Sroot if (so->so_options & SO_DEBUG) { 1525267Sroot ostate = tp->t_state; 1535267Sroot tcp_saveti = *ti; 1545267Sroot } 1557510Sroot if (so->so_options & SO_ACCEPTCONN) { 1567510Sroot so = sonewconn(so); 1577510Sroot if (so == 0) 1587510Sroot goto drop; 15910769Ssam /* 16010769Ssam * This is ugly, but .... 16110769Ssam * 16210769Ssam * Mark socket as temporary until we're 16310769Ssam * committed to keeping it. The code at 16410769Ssam * ``drop'' and ``dropwithreset'' check the 16510769Ssam * flag dropsocket to see if the temporary 16610769Ssam * socket created here should be discarded. 16710769Ssam * We mark the socket as discardable until 16810769Ssam * we're committed to it below in TCPS_LISTEN. 16910769Ssam */ 17010769Ssam dropsocket++; 1717510Sroot inp = (struct inpcb *)so->so_pcb; 1727510Sroot inp->inp_laddr = ti->ti_dst; 1737510Sroot inp->inp_lport = ti->ti_dport; 1747510Sroot tp = intotcpcb(inp); 1757510Sroot tp->t_state = TCPS_LISTEN; 1767510Sroot } 1774601Swnj 1784601Swnj /* 1795162Swnj * Segment received on connection. 1805162Swnj * Reset idle time and keep-alive timer. 1815162Swnj */ 1825162Swnj tp->t_idle = 0; 1835162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1845162Swnj 1855162Swnj /* 1865440Swnj * Process options. 1875440Swnj */ 1885440Swnj if (om) { 1895440Swnj tcp_dooptions(tp, om); 1905440Swnj om = 0; 1915440Swnj } 1925440Swnj 1935440Swnj /* 1945085Swnj * Calculate amount of space in receive window, 1955085Swnj * and then do TCP input processing. 1964601Swnj */ 1975085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1985231Swnj if (tp->rcv_wnd < 0) 1995231Swnj tp->rcv_wnd = 0; 2004601Swnj 2014601Swnj switch (tp->t_state) { 2024601Swnj 2035065Swnj /* 2045065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 2055065Swnj * If the segment contains an ACK then it is bad and send a RST. 2065065Swnj * If it does not contain a SYN then it is not interesting; drop it. 2075085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 2085065Swnj * tp->iss, and send a segment: 2095085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 2105065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 2115065Swnj * Fill in remote peer address fields if not previously specified. 2125065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 2135244Sroot * segment in this state. 2145065Swnj */ 2158271Sroot case TCPS_LISTEN: { 21610145Ssam struct mbuf *am; 2178271Sroot register struct sockaddr_in *sin; 2188271Sroot 2195065Swnj if (tiflags & TH_RST) 2205065Swnj goto drop; 2215300Sroot if (tiflags & TH_ACK) 2225085Swnj goto dropwithreset; 2235300Sroot if ((tiflags & TH_SYN) == 0) 2245065Swnj goto drop; 22510145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 22610145Ssam if (am == NULL) 22710145Ssam goto drop; 22810145Ssam am->m_len = sizeof (struct sockaddr_in); 2298599Sroot sin = mtod(am, struct sockaddr_in *); 2308271Sroot sin->sin_family = AF_INET; 2318271Sroot sin->sin_addr = ti->ti_src; 2328271Sroot sin->sin_port = ti->ti_sport; 2336028Sroot laddr = inp->inp_laddr; 23410145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 2356028Sroot inp->inp_laddr = ti->ti_dst; 2368599Sroot if (in_pcbconnect(inp, am)) { 2376028Sroot inp->inp_laddr = laddr; 2388716Sroot (void) m_free(am); 2395244Sroot goto drop; 2406028Sroot } 2418716Sroot (void) m_free(am); 2425244Sroot tp->t_template = tcp_template(tp); 2435244Sroot if (tp->t_template == 0) { 2445244Sroot in_pcbdisconnect(inp); 245*17264Skarels dropsocket = 0; /* socket is already gone */ 2466028Sroot inp->inp_laddr = laddr; 2476320Swnj tp = 0; 2485244Sroot goto drop; 2495244Sroot } 2505085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 2515065Swnj tp->irs = ti->ti_seq; 2525085Swnj tcp_sendseqinit(tp); 2535085Swnj tcp_rcvseqinit(tp); 2545065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2555244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 25610769Ssam dropsocket = 0; /* committed to socket */ 2575085Swnj goto trimthenstep6; 2588271Sroot } 2594601Swnj 2605065Swnj /* 2615065Swnj * If the state is SYN_SENT: 2625065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2635065Swnj * if seg contains a RST, then drop the connection. 2645065Swnj * if seg does not contain SYN, then drop it. 2655065Swnj * Otherwise this is an acceptable SYN segment 2665065Swnj * initialize tp->rcv_nxt and tp->irs 2675065Swnj * if seg contains ack then advance tp->snd_una 2685065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2695065Swnj * arrange for segment to be acked (eventually) 2705065Swnj * continue processing rest of data/controls, beginning with URG 2715065Swnj */ 2725065Swnj case TCPS_SYN_SENT: 2735065Swnj if ((tiflags & TH_ACK) && 2745300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2755300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2765231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2775085Swnj goto dropwithreset; 2785065Swnj if (tiflags & TH_RST) { 27910394Ssam if (tiflags & TH_ACK) 28010394Ssam tp = tcp_drop(tp, ECONNREFUSED); 2815065Swnj goto drop; 2824601Swnj } 2835065Swnj if ((tiflags & TH_SYN) == 0) 2845065Swnj goto drop; 2855231Swnj tp->snd_una = ti->ti_ack; 2865357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2875357Sroot tp->snd_nxt = tp->snd_una; 2885244Sroot tp->t_timer[TCPT_REXMT] = 0; 2895065Swnj tp->irs = ti->ti_seq; 2905085Swnj tcp_rcvseqinit(tp); 2915085Swnj tp->t_flags |= TF_ACKNOW; 2925162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2935244Sroot soisconnected(so); 2945065Swnj tp->t_state = TCPS_ESTABLISHED; 2955162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2965162Swnj } else 2975085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2985085Swnj goto trimthenstep6; 2995085Swnj 3005085Swnj trimthenstep6: 3015085Swnj /* 3025231Swnj * Advance ti->ti_seq to correspond to first data byte. 3035085Swnj * If data, trim to stay within window, 3045085Swnj * dropping FIN if necessary. 3055085Swnj */ 3065231Swnj ti->ti_seq++; 3075085Swnj if (ti->ti_len > tp->rcv_wnd) { 3085085Swnj todrop = ti->ti_len - tp->rcv_wnd; 3095085Swnj m_adj(m, -todrop); 3105085Swnj ti->ti_len = tp->rcv_wnd; 3115085Swnj ti->ti_flags &= ~TH_FIN; 3125065Swnj } 3135263Swnj tp->snd_wl1 = ti->ti_seq - 1; 3145085Swnj goto step6; 3155065Swnj } 3164601Swnj 3175065Swnj /* 31816222Skarels * If data is received on a connection after the 31916222Skarels * user processes are gone, then RST the other end. 32016222Skarels */ 32116222Skarels if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT && 32216222Skarels ti->ti_len) { 32316222Skarels tp = tcp_close(tp); 32416222Skarels goto dropwithreset; 32516222Skarels } 32616222Skarels 32716222Skarels /* 3285065Swnj * States other than LISTEN or SYN_SENT. 3295065Swnj * First check that at least some bytes of segment are within 3305065Swnj * receive window. 3315065Swnj */ 3325065Swnj if (tp->rcv_wnd == 0) { 3335065Swnj /* 3345065Swnj * If window is closed can only take segments at 3355231Swnj * window edge, and have to drop data and PUSH from 3365065Swnj * incoming segments. 3375065Swnj */ 3385300Sroot if (tp->rcv_nxt != ti->ti_seq) 3395065Swnj goto dropafterack; 3405085Swnj if (ti->ti_len > 0) { 3415690Swnj m_adj(m, ti->ti_len); 3425085Swnj ti->ti_len = 0; 3435085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3445065Swnj } 3455065Swnj } else { 3465065Swnj /* 3475231Swnj * If segment begins before rcv_nxt, drop leading 3485065Swnj * data (and SYN); if nothing left, just ack. 3495065Swnj */ 3505690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 3515690Swnj if (todrop > 0) { 3525085Swnj if (tiflags & TH_SYN) { 3535300Sroot tiflags &= ~TH_SYN; 3545690Swnj ti->ti_flags &= ~TH_SYN; 3555085Swnj ti->ti_seq++; 3565085Swnj if (ti->ti_urp > 1) 3575085Swnj ti->ti_urp--; 3585085Swnj else 3595085Swnj tiflags &= ~TH_URG; 3605085Swnj todrop--; 3615085Swnj } 3626211Swnj if (todrop > ti->ti_len || 3636211Swnj todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 3645065Swnj goto dropafterack; 3655065Swnj m_adj(m, todrop); 3665065Swnj ti->ti_seq += todrop; 3675065Swnj ti->ti_len -= todrop; 3685085Swnj if (ti->ti_urp > todrop) 3695085Swnj ti->ti_urp -= todrop; 3705085Swnj else { 3715085Swnj tiflags &= ~TH_URG; 3725690Swnj ti->ti_flags &= ~TH_URG; 3735690Swnj ti->ti_urp = 0; 3745085Swnj } 3755065Swnj } 3765065Swnj /* 3775065Swnj * If segment ends after window, drop trailing data 3785085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3795065Swnj */ 3805690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3815690Swnj if (todrop > 0) { 3826211Swnj if (todrop >= ti->ti_len) 3835065Swnj goto dropafterack; 3845065Swnj m_adj(m, -todrop); 3855065Swnj ti->ti_len -= todrop; 3865085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3875065Swnj } 3885065Swnj } 3894601Swnj 3905065Swnj /* 3915065Swnj * If the RST bit is set examine the state: 3925065Swnj * SYN_RECEIVED STATE: 3935065Swnj * If passive open, return to LISTEN state. 3945065Swnj * If active open, inform user that connection was refused. 3955065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3965065Swnj * Inform user that connection was reset, and close tcb. 3975065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3985065Swnj * Close the tcb. 3995065Swnj */ 4005065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 4015267Sroot 4025065Swnj case TCPS_SYN_RECEIVED: 40310394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4045065Swnj goto drop; 4054601Swnj 4065065Swnj case TCPS_ESTABLISHED: 4075065Swnj case TCPS_FIN_WAIT_1: 4085065Swnj case TCPS_FIN_WAIT_2: 4095065Swnj case TCPS_CLOSE_WAIT: 41010394Ssam tp = tcp_drop(tp, ECONNRESET); 4115065Swnj goto drop; 4125065Swnj 4135065Swnj case TCPS_CLOSING: 4145065Swnj case TCPS_LAST_ACK: 4155065Swnj case TCPS_TIME_WAIT: 41610394Ssam tp = tcp_close(tp); 4175065Swnj goto drop; 4184601Swnj } 4194601Swnj 4204601Swnj /* 4215065Swnj * If a SYN is in the window, then this is an 4225065Swnj * error and we send an RST and drop the connection. 4234601Swnj */ 4245065Swnj if (tiflags & TH_SYN) { 42510394Ssam tp = tcp_drop(tp, ECONNRESET); 4265085Swnj goto dropwithreset; 4274601Swnj } 4284601Swnj 4294601Swnj /* 4305065Swnj * If the ACK bit is off we drop the segment and return. 4314601Swnj */ 4325085Swnj if ((tiflags & TH_ACK) == 0) 4335065Swnj goto drop; 4345065Swnj 4355065Swnj /* 4365065Swnj * Ack processing. 4375065Swnj */ 4384601Swnj switch (tp->t_state) { 4394601Swnj 4405065Swnj /* 4415065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4425065Swnj * ESTABLISHED state and continue processing, othewise 4435065Swnj * send an RST. 4445065Swnj */ 4455065Swnj case TCPS_SYN_RECEIVED: 4465085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4475231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4485085Swnj goto dropwithreset; 4495244Sroot tp->snd_una++; /* SYN acked */ 4505357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4515357Sroot tp->snd_nxt = tp->snd_una; 4525244Sroot tp->t_timer[TCPT_REXMT] = 0; 4535085Swnj soisconnected(so); 4545085Swnj tp->t_state = TCPS_ESTABLISHED; 4555162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4565244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4575085Swnj /* fall into ... */ 4584601Swnj 4595065Swnj /* 4605065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4615065Swnj * ACKs. If the ack is in the range 4625231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4635065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4645065Swnj * data from the retransmission queue. If this ACK reflects 4655065Swnj * more up to date window information we update our window information. 4665065Swnj */ 4675065Swnj case TCPS_ESTABLISHED: 4685065Swnj case TCPS_FIN_WAIT_1: 4695065Swnj case TCPS_FIN_WAIT_2: 4705065Swnj case TCPS_CLOSE_WAIT: 4715065Swnj case TCPS_CLOSING: 4725244Sroot case TCPS_LAST_ACK: 4735244Sroot case TCPS_TIME_WAIT: 4745085Swnj #define ourfinisacked (acked > 0) 4755085Swnj 4765244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4775065Swnj break; 4785300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4795065Swnj goto dropafterack; 4805085Swnj acked = ti->ti_ack - tp->snd_una; 4815951Swnj 4825951Swnj /* 4835951Swnj * If transmit timer is running and timed sequence 4845951Swnj * number was acked, update smoothed round trip time. 4855951Swnj */ 4865951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4875951Swnj if (tp->t_srtt == 0) 4885951Swnj tp->t_srtt = tp->t_rtt; 4895951Swnj else 4905951Swnj tp->t_srtt = 4915951Swnj tcp_alpha * tp->t_srtt + 4925951Swnj (1 - tcp_alpha) * tp->t_rtt; 4935951Swnj tp->t_rtt = 0; 4945951Swnj } 4955951Swnj 4965307Sroot if (ti->ti_ack == tp->snd_max) 4975244Sroot tp->t_timer[TCPT_REXMT] = 0; 4985307Sroot else { 4995244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 5005244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 5015300Sroot tp->t_rxtshift = 0; 5025085Swnj } 5035307Sroot if (acked > so->so_snd.sb_cc) { 50415386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 5055307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 5065307Sroot } else { 5076161Ssam sbdrop(&so->so_snd, acked); 5085307Sroot tp->snd_wnd -= acked; 5095307Sroot acked = 0; 5105307Sroot } 5116434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 5125300Sroot sowwakeup(so); 5135231Swnj tp->snd_una = ti->ti_ack; 5145357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 5155357Sroot tp->snd_nxt = tp->snd_una; 5165162Swnj 5174601Swnj switch (tp->t_state) { 5184601Swnj 5195065Swnj /* 5205065Swnj * In FIN_WAIT_1 STATE in addition to the processing 5215065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 5225085Swnj * then enter FIN_WAIT_2. 5235065Swnj */ 5245065Swnj case TCPS_FIN_WAIT_1: 5255896Swnj if (ourfinisacked) { 5265896Swnj /* 5275896Swnj * If we can't receive any more 5285896Swnj * data, then closing user can proceed. 5295896Swnj */ 5305896Swnj if (so->so_state & SS_CANTRCVMORE) 5315896Swnj soisdisconnected(so); 5325085Swnj tp->t_state = TCPS_FIN_WAIT_2; 533*17264Skarels /* 534*17264Skarels * This is contrary to the specification, 535*17264Skarels * but if we haven't gotten our FIN in 536*17264Skarels * 5 minutes, it's not forthcoming. 537*17264Skarels */ 5385896Swnj } 5394601Swnj break; 5404601Swnj 5415065Swnj /* 5425065Swnj * In CLOSING STATE in addition to the processing for 5435065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5445065Swnj * then enter the TIME-WAIT state, otherwise ignore 5455065Swnj * the segment. 5465065Swnj */ 5475065Swnj case TCPS_CLOSING: 5485244Sroot if (ourfinisacked) { 5495065Swnj tp->t_state = TCPS_TIME_WAIT; 5505244Sroot tcp_canceltimers(tp); 5515244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5525244Sroot soisdisconnected(so); 5535244Sroot } 5545244Sroot break; 5554601Swnj 5565065Swnj /* 5575085Swnj * The only thing that can arrive in LAST_ACK state 5585085Swnj * is an acknowledgment of our FIN. If our FIN is now 5595085Swnj * acknowledged, delete the TCB, enter the closed state 5605085Swnj * and return. 5615065Swnj */ 5625065Swnj case TCPS_LAST_ACK: 56310394Ssam if (ourfinisacked) 56410394Ssam tp = tcp_close(tp); 5655065Swnj goto drop; 5664601Swnj 5675065Swnj /* 5685065Swnj * In TIME_WAIT state the only thing that should arrive 5695065Swnj * is a retransmission of the remote FIN. Acknowledge 5705065Swnj * it and restart the finack timer. 5715065Swnj */ 5725065Swnj case TCPS_TIME_WAIT: 5735162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5745065Swnj goto dropafterack; 5754601Swnj } 5765085Swnj #undef ourfinisacked 5775085Swnj } 5784601Swnj 5795065Swnj step6: 5805065Swnj /* 5815244Sroot * Update window information. 5825244Sroot */ 5835300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5845391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5855300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5865244Sroot tp->snd_wnd = ti->ti_win; 5875244Sroot tp->snd_wl1 = ti->ti_seq; 5885244Sroot tp->snd_wl2 = ti->ti_ack; 5898599Sroot if (tp->snd_wnd != 0) 5905244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5915244Sroot } 5925244Sroot 5935244Sroot /* 5945547Swnj * Process segments with URG. 5955065Swnj */ 5967267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 5977267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5985547Swnj /* 59913121Ssam * This is a kludge, but if we receive accept 60013121Ssam * random urgent pointers, we'll crash in 60113121Ssam * soreceive. It's hard to imagine someone 60213121Ssam * actually wanting to send this much urgent data. 60312441Ssam */ 604*17264Skarels if (ti->ti_urp > tp->rcv_wnd + 1) { /* XXX */ 60512441Ssam ti->ti_urp = 0; /* XXX */ 60612441Ssam tiflags &= ~TH_URG; /* XXX */ 60712441Ssam ti->ti_flags &= ~TH_URG; /* XXX */ 60813121Ssam goto badurp; /* XXX */ 60912441Ssam } 61012441Ssam /* 6115547Swnj * If this segment advances the known urgent pointer, 6125547Swnj * then mark the data stream. This should not happen 6135547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 6145547Swnj * a FIN has been received from the remote side. 6155547Swnj * In these states we ignore the URG. 6165547Swnj */ 6175547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 6185547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 6195547Swnj so->so_oobmark = so->so_rcv.sb_cc + 6205547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 6215547Swnj if (so->so_oobmark == 0) 6225547Swnj so->so_state |= SS_RCVATMARK; 6238313Sroot sohasoutofband(so); 6245547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 6255440Swnj } 6265547Swnj /* 6275547Swnj * Remove out of band data so doesn't get presented to user. 6285547Swnj * This can happen independent of advancing the URG pointer, 6295547Swnj * but if two URG's are pending at once, some out-of-band 6305547Swnj * data may creep in... ick. 6315547Swnj */ 6327510Sroot if (ti->ti_urp <= ti->ti_len) 6335547Swnj tcp_pulloutofband(so, ti); 6345419Swnj } 63513121Ssam badurp: /* XXX */ 6364601Swnj 6374601Swnj /* 6385065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6395065Swnj * and arranging for acknowledgment of receipt if necessary. 6405065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6415065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6425065Swnj * case PRU_RCVD). If a FIN has already been received on this 6435065Swnj * connection then we just ignore the text. 6444601Swnj */ 6455263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6465263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6475065Swnj tiflags = tcp_reass(tp, ti); 6485440Swnj if (tcpnodelack == 0) 6495440Swnj tp->t_flags |= TF_DELACK; 6505440Swnj else 6515440Swnj tp->t_flags |= TF_ACKNOW; 6525244Sroot } else { 6534924Swnj m_freem(m); 6545263Swnj tiflags &= ~TH_FIN; 6555244Sroot } 6564601Swnj 6574601Swnj /* 6585263Swnj * If FIN is received ACK the FIN and let the user know 6595263Swnj * that the connection is closing. 6604601Swnj */ 6615263Swnj if (tiflags & TH_FIN) { 6625244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6635244Sroot socantrcvmore(so); 6645244Sroot tp->t_flags |= TF_ACKNOW; 6655244Sroot tp->rcv_nxt++; 6665244Sroot } 6675065Swnj switch (tp->t_state) { 6684601Swnj 6695065Swnj /* 6705065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6715065Swnj * enter the CLOSE_WAIT state. 6724884Swnj */ 6735065Swnj case TCPS_SYN_RECEIVED: 6745065Swnj case TCPS_ESTABLISHED: 6755065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6765065Swnj break; 6774884Swnj 6785065Swnj /* 6795085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6805085Swnj * enter the CLOSING state. 6814884Swnj */ 6825065Swnj case TCPS_FIN_WAIT_1: 6835085Swnj tp->t_state = TCPS_CLOSING; 6845065Swnj break; 6854601Swnj 6865065Swnj /* 6875065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6885065Swnj * starting the time-wait timer, turning off the other 6895065Swnj * standard timers. 6905065Swnj */ 6915065Swnj case TCPS_FIN_WAIT_2: 6925244Sroot tp->t_state = TCPS_TIME_WAIT; 6935074Swnj tcp_canceltimers(tp); 6945162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6955244Sroot soisdisconnected(so); 6965065Swnj break; 6975065Swnj 6984884Swnj /* 6995065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 7004884Swnj */ 7015065Swnj case TCPS_TIME_WAIT: 7025162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 7035065Swnj break; 7045085Swnj } 7054601Swnj } 7065267Sroot if (so->so_options & SO_DEBUG) 7075267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 7085085Swnj 7095085Swnj /* 7105085Swnj * Return any desired output. 7115085Swnj */ 7126161Ssam (void) tcp_output(tp); 7135065Swnj return; 7145085Swnj 7155065Swnj dropafterack: 7165085Swnj /* 7176211Swnj * Generate an ACK dropping incoming segment if it occupies 7186211Swnj * sequence space, where the ACK reflects our state. 7195085Swnj */ 7206211Swnj if ((tiflags&TH_RST) || 7216211Swnj tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 7225085Swnj goto drop; 7236303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 7246303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 7255391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 7265231Swnj return; 7275085Swnj 7285085Swnj dropwithreset: 72911731Ssam if (om) { 7306161Ssam (void) m_free(om); 73111731Ssam om = 0; 73211731Ssam } 7335085Swnj /* 7345244Sroot * Generate a RST, dropping incoming segment. 7355085Swnj * Make ACK acceptable to originator of segment. 7365085Swnj */ 7375085Swnj if (tiflags & TH_RST) 7385085Swnj goto drop; 7395085Swnj if (tiflags & TH_ACK) 7405391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7415085Swnj else { 7425085Swnj if (tiflags & TH_SYN) 7435085Swnj ti->ti_len++; 7446211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 7456211Swnj TH_RST|TH_ACK); 7465085Swnj } 74710769Ssam /* destroy temporarily created socket */ 74810769Ssam if (dropsocket) 74910769Ssam (void) soabort(so); 7505231Swnj return; 7515085Swnj 7525065Swnj drop: 75311730Ssam if (om) 75411730Ssam (void) m_free(om); 7555085Swnj /* 7565085Swnj * Drop space held by incoming segment and return. 7575085Swnj */ 7586303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 7596303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 7605065Swnj m_freem(m); 76110769Ssam /* destroy temporarily created socket */ 76210769Ssam if (dropsocket) 76310769Ssam (void) soabort(so); 7645267Sroot return; 7655065Swnj } 7665065Swnj 7675440Swnj tcp_dooptions(tp, om) 7685440Swnj struct tcpcb *tp; 7695440Swnj struct mbuf *om; 7705419Swnj { 7715440Swnj register u_char *cp; 7725440Swnj int opt, optlen, cnt; 7735419Swnj 7745440Swnj cp = mtod(om, u_char *); 7755440Swnj cnt = om->m_len; 7765440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7775440Swnj opt = cp[0]; 7785440Swnj if (opt == TCPOPT_EOL) 7795440Swnj break; 7805440Swnj if (opt == TCPOPT_NOP) 7815440Swnj optlen = 1; 78212169Ssam else { 7835440Swnj optlen = cp[1]; 78412169Ssam if (optlen <= 0) 78512169Ssam break; 78612169Ssam } 7875440Swnj switch (opt) { 7885440Swnj 7895440Swnj default: 7905440Swnj break; 7915440Swnj 7925440Swnj case TCPOPT_MAXSEG: 7935440Swnj if (optlen != 4) 7945440Swnj continue; 7955440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7966161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7975440Swnj break; 7985419Swnj } 7995419Swnj } 8006161Ssam (void) m_free(om); 8015419Swnj } 8025419Swnj 8035419Swnj /* 8045547Swnj * Pull out of band byte out of a segment so 8055547Swnj * it doesn't appear in the user's data queue. 8065547Swnj * It is still reflected in the segment length for 8075547Swnj * sequencing purposes. 8085547Swnj */ 8095547Swnj tcp_pulloutofband(so, ti) 8105547Swnj struct socket *so; 8115547Swnj struct tcpiphdr *ti; 8125547Swnj { 8135547Swnj register struct mbuf *m; 8146116Swnj int cnt = ti->ti_urp - 1; 8155547Swnj 8165547Swnj m = dtom(ti); 8175547Swnj while (cnt >= 0) { 8185547Swnj if (m->m_len > cnt) { 8195547Swnj char *cp = mtod(m, caddr_t) + cnt; 8205547Swnj struct tcpcb *tp = sototcpcb(so); 8215547Swnj 8225547Swnj tp->t_iobc = *cp; 8235547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 8246161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 8255547Swnj m->m_len--; 8265547Swnj return; 8275547Swnj } 8285547Swnj cnt -= m->m_len; 8295547Swnj m = m->m_next; 8305547Swnj if (m == 0) 8315547Swnj break; 8325547Swnj } 8335547Swnj panic("tcp_pulloutofband"); 8345547Swnj } 8355547Swnj 8365547Swnj /* 8375065Swnj * Insert segment ti into reassembly queue of tcp with 8385065Swnj * control block tp. Return TH_FIN if reassembly now includes 8395065Swnj * a segment with FIN. 8405065Swnj */ 8415109Swnj tcp_reass(tp, ti) 8425065Swnj register struct tcpcb *tp; 8435065Swnj register struct tcpiphdr *ti; 8445065Swnj { 8455065Swnj register struct tcpiphdr *q; 8465085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8475263Swnj struct mbuf *m; 8485263Swnj int flags; 8495065Swnj 8505065Swnj /* 8515162Swnj * Call with ti==0 after become established to 8525162Swnj * force pre-ESTABLISHED data up to user socket. 8535065Swnj */ 8545162Swnj if (ti == 0) 8555065Swnj goto present; 8564601Swnj 8575065Swnj /* 8585065Swnj * Find a segment which begins after this one does. 8595065Swnj */ 8605065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8615065Swnj q = (struct tcpiphdr *)q->ti_next) 8625065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8635065Swnj break; 8644601Swnj 8655065Swnj /* 8665065Swnj * If there is a preceding segment, it may provide some of 8675065Swnj * our data already. If so, drop the data from the incoming 8685065Swnj * segment. If it provides all of our data, drop us. 8695065Swnj */ 8705065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8715065Swnj register int i; 8725690Swnj q = (struct tcpiphdr *)q->ti_prev; 8735065Swnj /* conversion to int (in i) handles seq wraparound */ 8745065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8755065Swnj if (i > 0) { 8764924Swnj if (i >= ti->ti_len) 8775065Swnj goto drop; 8787338Swnj m_adj(dtom(ti), i); 8795065Swnj ti->ti_len -= i; 8804924Swnj ti->ti_seq += i; 8814601Swnj } 8825065Swnj q = (struct tcpiphdr *)(q->ti_next); 8835065Swnj } 8844601Swnj 8855065Swnj /* 8865065Swnj * While we overlap succeeding segments trim them or, 8875065Swnj * if they are completely covered, dequeue them. 8885065Swnj */ 8895690Swnj while (q != (struct tcpiphdr *)tp) { 8905065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8915690Swnj if (i <= 0) 8925690Swnj break; 8935065Swnj if (i < q->ti_len) { 8945690Swnj q->ti_seq += i; 8955065Swnj q->ti_len -= i; 8965065Swnj m_adj(dtom(q), i); 8975065Swnj break; 8984601Swnj } 8995065Swnj q = (struct tcpiphdr *)q->ti_next; 9005623Swnj m = dtom(q->ti_prev); 9015065Swnj remque(q->ti_prev); 9025623Swnj m_freem(m); 9035065Swnj } 9044601Swnj 9055065Swnj /* 9065065Swnj * Stick new segment in its place. 9075065Swnj */ 9085065Swnj insque(ti, q->ti_prev); 9094601Swnj 9105065Swnj present: 9115065Swnj /* 9125244Sroot * Present data to user, advancing rcv_nxt through 9135244Sroot * completed sequence space. 9145065Swnj */ 9155263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 9165244Sroot return (0); 9174924Swnj ti = tp->seg_next; 9185263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 9195263Swnj return (0); 9205263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9215263Swnj return (0); 9225263Swnj do { 9235244Sroot tp->rcv_nxt += ti->ti_len; 9245244Sroot flags = ti->ti_flags & TH_FIN; 9254924Swnj remque(ti); 9265263Swnj m = dtom(ti); 9274924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9285263Swnj if (so->so_state & SS_CANTRCVMORE) 9296161Ssam m_freem(m); 93010145Ssam else 9315263Swnj sbappend(&so->so_rcv, m); 9325263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9335263Swnj sorwakeup(so); 9345065Swnj return (flags); 9355065Swnj drop: 9365065Swnj m_freem(dtom(ti)); 9375263Swnj return (0); 9384601Swnj } 939