1*17316Skarels /* tcp_input.c 6.7 84/11/01 */ 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 /* 18617272Skarels * Process options if not in LISTEN state, 18717272Skarels * else do it below (after getting remote address). 1885440Swnj */ 18917272Skarels if (om && tp->t_state != TCPS_LISTEN) { 19017272Skarels tcp_dooptions(tp, om, ti); 1915440Swnj om = 0; 1925440Swnj } 1935440Swnj 1945440Swnj /* 1955085Swnj * Calculate amount of space in receive window, 1965085Swnj * and then do TCP input processing. 1974601Swnj */ 1985085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1995231Swnj if (tp->rcv_wnd < 0) 2005231Swnj tp->rcv_wnd = 0; 2014601Swnj 2024601Swnj switch (tp->t_state) { 2034601Swnj 2045065Swnj /* 2055065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 2065065Swnj * If the segment contains an ACK then it is bad and send a RST. 2075065Swnj * If it does not contain a SYN then it is not interesting; drop it. 2085085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 2095065Swnj * tp->iss, and send a segment: 2105085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 2115065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 2125065Swnj * Fill in remote peer address fields if not previously specified. 2135065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 2145244Sroot * segment in this state. 2155065Swnj */ 2168271Sroot case TCPS_LISTEN: { 21710145Ssam struct mbuf *am; 2188271Sroot register struct sockaddr_in *sin; 2198271Sroot 2205065Swnj if (tiflags & TH_RST) 2215065Swnj goto drop; 2225300Sroot if (tiflags & TH_ACK) 2235085Swnj goto dropwithreset; 2245300Sroot if ((tiflags & TH_SYN) == 0) 2255065Swnj goto drop; 22610145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 22710145Ssam if (am == NULL) 22810145Ssam goto drop; 22910145Ssam am->m_len = sizeof (struct sockaddr_in); 2308599Sroot sin = mtod(am, struct sockaddr_in *); 2318271Sroot sin->sin_family = AF_INET; 2328271Sroot sin->sin_addr = ti->ti_src; 2338271Sroot sin->sin_port = ti->ti_sport; 2346028Sroot laddr = inp->inp_laddr; 23510145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 2366028Sroot inp->inp_laddr = ti->ti_dst; 2378599Sroot if (in_pcbconnect(inp, am)) { 2386028Sroot inp->inp_laddr = laddr; 2398716Sroot (void) m_free(am); 2405244Sroot goto drop; 2416028Sroot } 2428716Sroot (void) m_free(am); 2435244Sroot tp->t_template = tcp_template(tp); 2445244Sroot if (tp->t_template == 0) { 2455244Sroot in_pcbdisconnect(inp); 24617264Skarels dropsocket = 0; /* socket is already gone */ 2476028Sroot inp->inp_laddr = laddr; 2486320Swnj tp = 0; 2495244Sroot goto drop; 2505244Sroot } 25117272Skarels if (om) { 25217272Skarels tcp_dooptions(tp, om, ti); 25317272Skarels om = 0; 25417272Skarels } 2555085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 2565065Swnj tp->irs = ti->ti_seq; 2575085Swnj tcp_sendseqinit(tp); 2585085Swnj tcp_rcvseqinit(tp); 2595065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2605244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 26110769Ssam dropsocket = 0; /* committed to socket */ 2625085Swnj goto trimthenstep6; 2638271Sroot } 2644601Swnj 2655065Swnj /* 2665065Swnj * If the state is SYN_SENT: 2675065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2685065Swnj * if seg contains a RST, then drop the connection. 2695065Swnj * if seg does not contain SYN, then drop it. 2705065Swnj * Otherwise this is an acceptable SYN segment 2715065Swnj * initialize tp->rcv_nxt and tp->irs 2725065Swnj * if seg contains ack then advance tp->snd_una 2735065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2745065Swnj * arrange for segment to be acked (eventually) 2755065Swnj * continue processing rest of data/controls, beginning with URG 2765065Swnj */ 2775065Swnj case TCPS_SYN_SENT: 2785065Swnj if ((tiflags & TH_ACK) && 2795300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2805300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2815231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2825085Swnj goto dropwithreset; 2835065Swnj if (tiflags & TH_RST) { 28410394Ssam if (tiflags & TH_ACK) 28510394Ssam tp = tcp_drop(tp, ECONNREFUSED); 2865065Swnj goto drop; 2874601Swnj } 2885065Swnj if ((tiflags & TH_SYN) == 0) 2895065Swnj goto drop; 2905231Swnj tp->snd_una = ti->ti_ack; 2915357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2925357Sroot tp->snd_nxt = tp->snd_una; 2935244Sroot tp->t_timer[TCPT_REXMT] = 0; 2945065Swnj tp->irs = ti->ti_seq; 2955085Swnj tcp_rcvseqinit(tp); 2965085Swnj tp->t_flags |= TF_ACKNOW; 2975162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2985244Sroot soisconnected(so); 2995065Swnj tp->t_state = TCPS_ESTABLISHED; 30017272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 3015162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 3025162Swnj } else 3035085Swnj tp->t_state = TCPS_SYN_RECEIVED; 3045085Swnj goto trimthenstep6; 3055085Swnj 3065085Swnj trimthenstep6: 3075085Swnj /* 3085231Swnj * Advance ti->ti_seq to correspond to first data byte. 3095085Swnj * If data, trim to stay within window, 3105085Swnj * dropping FIN if necessary. 3115085Swnj */ 3125231Swnj ti->ti_seq++; 3135085Swnj if (ti->ti_len > tp->rcv_wnd) { 3145085Swnj todrop = ti->ti_len - tp->rcv_wnd; 3155085Swnj m_adj(m, -todrop); 3165085Swnj ti->ti_len = tp->rcv_wnd; 3175085Swnj ti->ti_flags &= ~TH_FIN; 3185065Swnj } 3195263Swnj tp->snd_wl1 = ti->ti_seq - 1; 3205085Swnj goto step6; 3215065Swnj } 3224601Swnj 3235065Swnj /* 32416222Skarels * If data is received on a connection after the 32516222Skarels * user processes are gone, then RST the other end. 32616222Skarels */ 32716222Skarels if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT && 32816222Skarels ti->ti_len) { 32916222Skarels tp = tcp_close(tp); 33016222Skarels goto dropwithreset; 33116222Skarels } 33216222Skarels 33316222Skarels /* 3345065Swnj * States other than LISTEN or SYN_SENT. 3355065Swnj * First check that at least some bytes of segment are within 3365065Swnj * receive window. 3375065Swnj */ 3385065Swnj if (tp->rcv_wnd == 0) { 3395065Swnj /* 3405065Swnj * If window is closed can only take segments at 3415231Swnj * window edge, and have to drop data and PUSH from 3425065Swnj * incoming segments. 3435065Swnj */ 3445300Sroot if (tp->rcv_nxt != ti->ti_seq) 3455065Swnj goto dropafterack; 3465085Swnj if (ti->ti_len > 0) { 3475690Swnj m_adj(m, ti->ti_len); 3485085Swnj ti->ti_len = 0; 3495085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3505065Swnj } 3515065Swnj } else { 3525065Swnj /* 3535231Swnj * If segment begins before rcv_nxt, drop leading 3545065Swnj * data (and SYN); if nothing left, just ack. 3555065Swnj */ 3565690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 3575690Swnj if (todrop > 0) { 3585085Swnj if (tiflags & TH_SYN) { 3595300Sroot tiflags &= ~TH_SYN; 3605690Swnj ti->ti_flags &= ~TH_SYN; 3615085Swnj ti->ti_seq++; 3625085Swnj if (ti->ti_urp > 1) 3635085Swnj ti->ti_urp--; 3645085Swnj else 3655085Swnj tiflags &= ~TH_URG; 3665085Swnj todrop--; 3675085Swnj } 3686211Swnj if (todrop > ti->ti_len || 3696211Swnj todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 3705065Swnj goto dropafterack; 3715065Swnj m_adj(m, todrop); 3725065Swnj ti->ti_seq += todrop; 3735065Swnj ti->ti_len -= todrop; 3745085Swnj if (ti->ti_urp > todrop) 3755085Swnj ti->ti_urp -= todrop; 3765085Swnj else { 3775085Swnj tiflags &= ~TH_URG; 3785690Swnj ti->ti_flags &= ~TH_URG; 3795690Swnj ti->ti_urp = 0; 3805085Swnj } 3815065Swnj } 3825065Swnj /* 3835065Swnj * If segment ends after window, drop trailing data 3845085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3855065Swnj */ 3865690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3875690Swnj if (todrop > 0) { 3886211Swnj if (todrop >= ti->ti_len) 3895065Swnj goto dropafterack; 3905065Swnj m_adj(m, -todrop); 3915065Swnj ti->ti_len -= todrop; 3925085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3935065Swnj } 3945065Swnj } 3954601Swnj 3965065Swnj /* 3975065Swnj * If the RST bit is set examine the state: 3985065Swnj * SYN_RECEIVED STATE: 3995065Swnj * If passive open, return to LISTEN state. 4005065Swnj * If active open, inform user that connection was refused. 4015065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 4025065Swnj * Inform user that connection was reset, and close tcb. 4035065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 4045065Swnj * Close the tcb. 4055065Swnj */ 4065065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 4075267Sroot 4085065Swnj case TCPS_SYN_RECEIVED: 40910394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4105065Swnj goto drop; 4114601Swnj 4125065Swnj case TCPS_ESTABLISHED: 4135065Swnj case TCPS_FIN_WAIT_1: 4145065Swnj case TCPS_FIN_WAIT_2: 4155065Swnj case TCPS_CLOSE_WAIT: 41610394Ssam tp = tcp_drop(tp, ECONNRESET); 4175065Swnj goto drop; 4185065Swnj 4195065Swnj case TCPS_CLOSING: 4205065Swnj case TCPS_LAST_ACK: 4215065Swnj case TCPS_TIME_WAIT: 42210394Ssam tp = tcp_close(tp); 4235065Swnj goto drop; 4244601Swnj } 4254601Swnj 4264601Swnj /* 4275065Swnj * If a SYN is in the window, then this is an 4285065Swnj * error and we send an RST and drop the connection. 4294601Swnj */ 4305065Swnj if (tiflags & TH_SYN) { 43110394Ssam tp = tcp_drop(tp, ECONNRESET); 4325085Swnj goto dropwithreset; 4334601Swnj } 4344601Swnj 4354601Swnj /* 4365065Swnj * If the ACK bit is off we drop the segment and return. 4374601Swnj */ 4385085Swnj if ((tiflags & TH_ACK) == 0) 4395065Swnj goto drop; 4405065Swnj 4415065Swnj /* 4425065Swnj * Ack processing. 4435065Swnj */ 4444601Swnj switch (tp->t_state) { 4454601Swnj 4465065Swnj /* 4475065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4485065Swnj * ESTABLISHED state and continue processing, othewise 4495065Swnj * send an RST. 4505065Swnj */ 4515065Swnj case TCPS_SYN_RECEIVED: 4525085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4535231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4545085Swnj goto dropwithreset; 4555244Sroot tp->snd_una++; /* SYN acked */ 4565357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4575357Sroot tp->snd_nxt = tp->snd_una; 4585244Sroot tp->t_timer[TCPT_REXMT] = 0; 4595085Swnj soisconnected(so); 4605085Swnj tp->t_state = TCPS_ESTABLISHED; 46117272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 4625162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4635244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4645085Swnj /* fall into ... */ 4654601Swnj 4665065Swnj /* 4675065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4685065Swnj * ACKs. If the ack is in the range 4695231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4705065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4715065Swnj * data from the retransmission queue. If this ACK reflects 4725065Swnj * more up to date window information we update our window information. 4735065Swnj */ 4745065Swnj case TCPS_ESTABLISHED: 4755065Swnj case TCPS_FIN_WAIT_1: 4765065Swnj case TCPS_FIN_WAIT_2: 4775065Swnj case TCPS_CLOSE_WAIT: 4785065Swnj case TCPS_CLOSING: 4795244Sroot case TCPS_LAST_ACK: 4805244Sroot case TCPS_TIME_WAIT: 4815085Swnj #define ourfinisacked (acked > 0) 4825085Swnj 4835244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4845065Swnj break; 4855300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4865065Swnj goto dropafterack; 4875085Swnj acked = ti->ti_ack - tp->snd_una; 4885951Swnj 4895951Swnj /* 4905951Swnj * If transmit timer is running and timed sequence 4915951Swnj * number was acked, update smoothed round trip time. 4925951Swnj */ 4935951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4945951Swnj if (tp->t_srtt == 0) 4955951Swnj tp->t_srtt = tp->t_rtt; 4965951Swnj else 4975951Swnj tp->t_srtt = 4985951Swnj tcp_alpha * tp->t_srtt + 4995951Swnj (1 - tcp_alpha) * tp->t_rtt; 5005951Swnj tp->t_rtt = 0; 5015951Swnj } 5025951Swnj 5035307Sroot if (ti->ti_ack == tp->snd_max) 5045244Sroot tp->t_timer[TCPT_REXMT] = 0; 5055307Sroot else { 5065244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 5075244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 5085300Sroot tp->t_rxtshift = 0; 5095085Swnj } 5105307Sroot if (acked > so->so_snd.sb_cc) { 51115386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 5125307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 5135307Sroot } else { 5146161Ssam sbdrop(&so->so_snd, acked); 5155307Sroot tp->snd_wnd -= acked; 5165307Sroot acked = 0; 5175307Sroot } 5186434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 5195300Sroot sowwakeup(so); 5205231Swnj tp->snd_una = ti->ti_ack; 5215357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 5225357Sroot tp->snd_nxt = tp->snd_una; 5235162Swnj 5244601Swnj switch (tp->t_state) { 5254601Swnj 5265065Swnj /* 5275065Swnj * In FIN_WAIT_1 STATE in addition to the processing 5285065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 5295085Swnj * then enter FIN_WAIT_2. 5305065Swnj */ 5315065Swnj case TCPS_FIN_WAIT_1: 5325896Swnj if (ourfinisacked) { 5335896Swnj /* 5345896Swnj * If we can't receive any more 5355896Swnj * data, then closing user can proceed. 5365896Swnj */ 5375896Swnj if (so->so_state & SS_CANTRCVMORE) 5385896Swnj soisdisconnected(so); 5395085Swnj tp->t_state = TCPS_FIN_WAIT_2; 54017264Skarels /* 54117264Skarels * This is contrary to the specification, 54217264Skarels * but if we haven't gotten our FIN in 54317264Skarels * 5 minutes, it's not forthcoming. 54417264Skarels */ 545*17316Skarels tp->t_timer[TCPT_2MSL] = 5 * 60 * PR_SLOWHZ; 5465896Swnj } 5474601Swnj break; 5484601Swnj 5495065Swnj /* 5505065Swnj * In CLOSING STATE in addition to the processing for 5515065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5525065Swnj * then enter the TIME-WAIT state, otherwise ignore 5535065Swnj * the segment. 5545065Swnj */ 5555065Swnj case TCPS_CLOSING: 5565244Sroot if (ourfinisacked) { 5575065Swnj tp->t_state = TCPS_TIME_WAIT; 5585244Sroot tcp_canceltimers(tp); 5595244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5605244Sroot soisdisconnected(so); 5615244Sroot } 5625244Sroot break; 5634601Swnj 5645065Swnj /* 5655085Swnj * The only thing that can arrive in LAST_ACK state 5665085Swnj * is an acknowledgment of our FIN. If our FIN is now 5675085Swnj * acknowledged, delete the TCB, enter the closed state 5685085Swnj * and return. 5695065Swnj */ 5705065Swnj case TCPS_LAST_ACK: 57110394Ssam if (ourfinisacked) 57210394Ssam tp = tcp_close(tp); 5735065Swnj goto drop; 5744601Swnj 5755065Swnj /* 5765065Swnj * In TIME_WAIT state the only thing that should arrive 5775065Swnj * is a retransmission of the remote FIN. Acknowledge 5785065Swnj * it and restart the finack timer. 5795065Swnj */ 5805065Swnj case TCPS_TIME_WAIT: 5815162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5825065Swnj goto dropafterack; 5834601Swnj } 5845085Swnj #undef ourfinisacked 5855085Swnj } 5864601Swnj 5875065Swnj step6: 5885065Swnj /* 5895244Sroot * Update window information. 5905244Sroot */ 5915300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5925391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5935300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5945244Sroot tp->snd_wnd = ti->ti_win; 5955244Sroot tp->snd_wl1 = ti->ti_seq; 5965244Sroot tp->snd_wl2 = ti->ti_ack; 5978599Sroot if (tp->snd_wnd != 0) 5985244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5995244Sroot } 6005244Sroot 6015244Sroot /* 6025547Swnj * Process segments with URG. 6035065Swnj */ 6047267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 6057267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6065547Swnj /* 60713121Ssam * This is a kludge, but if we receive accept 60813121Ssam * random urgent pointers, we'll crash in 60913121Ssam * soreceive. It's hard to imagine someone 61013121Ssam * actually wanting to send this much urgent data. 61112441Ssam */ 61217264Skarels if (ti->ti_urp > tp->rcv_wnd + 1) { /* XXX */ 61312441Ssam ti->ti_urp = 0; /* XXX */ 61412441Ssam tiflags &= ~TH_URG; /* XXX */ 61512441Ssam ti->ti_flags &= ~TH_URG; /* XXX */ 61613121Ssam goto badurp; /* XXX */ 61712441Ssam } 61812441Ssam /* 6195547Swnj * If this segment advances the known urgent pointer, 6205547Swnj * then mark the data stream. This should not happen 6215547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 6225547Swnj * a FIN has been received from the remote side. 6235547Swnj * In these states we ignore the URG. 6245547Swnj */ 6255547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 6265547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 6275547Swnj so->so_oobmark = so->so_rcv.sb_cc + 6285547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 6295547Swnj if (so->so_oobmark == 0) 6305547Swnj so->so_state |= SS_RCVATMARK; 6318313Sroot sohasoutofband(so); 6325547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 6335440Swnj } 6345547Swnj /* 6355547Swnj * Remove out of band data so doesn't get presented to user. 6365547Swnj * This can happen independent of advancing the URG pointer, 6375547Swnj * but if two URG's are pending at once, some out-of-band 6385547Swnj * data may creep in... ick. 6395547Swnj */ 6407510Sroot if (ti->ti_urp <= ti->ti_len) 6415547Swnj tcp_pulloutofband(so, ti); 6425419Swnj } 64313121Ssam badurp: /* XXX */ 6444601Swnj 6454601Swnj /* 6465065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6475065Swnj * and arranging for acknowledgment of receipt if necessary. 6485065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6495065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6505065Swnj * case PRU_RCVD). If a FIN has already been received on this 6515065Swnj * connection then we just ignore the text. 6524601Swnj */ 6535263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6545263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6555065Swnj tiflags = tcp_reass(tp, ti); 6565440Swnj if (tcpnodelack == 0) 6575440Swnj tp->t_flags |= TF_DELACK; 6585440Swnj else 6595440Swnj tp->t_flags |= TF_ACKNOW; 6605244Sroot } else { 6614924Swnj m_freem(m); 6625263Swnj tiflags &= ~TH_FIN; 6635244Sroot } 6644601Swnj 6654601Swnj /* 6665263Swnj * If FIN is received ACK the FIN and let the user know 6675263Swnj * that the connection is closing. 6684601Swnj */ 6695263Swnj if (tiflags & TH_FIN) { 6705244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6715244Sroot socantrcvmore(so); 6725244Sroot tp->t_flags |= TF_ACKNOW; 6735244Sroot tp->rcv_nxt++; 6745244Sroot } 6755065Swnj switch (tp->t_state) { 6764601Swnj 6775065Swnj /* 6785065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6795065Swnj * enter the CLOSE_WAIT state. 6804884Swnj */ 6815065Swnj case TCPS_SYN_RECEIVED: 6825065Swnj case TCPS_ESTABLISHED: 6835065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6845065Swnj break; 6854884Swnj 6865065Swnj /* 6875085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6885085Swnj * enter the CLOSING state. 6894884Swnj */ 6905065Swnj case TCPS_FIN_WAIT_1: 6915085Swnj tp->t_state = TCPS_CLOSING; 6925065Swnj break; 6934601Swnj 6945065Swnj /* 6955065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6965065Swnj * starting the time-wait timer, turning off the other 6975065Swnj * standard timers. 6985065Swnj */ 6995065Swnj case TCPS_FIN_WAIT_2: 7005244Sroot tp->t_state = TCPS_TIME_WAIT; 7015074Swnj tcp_canceltimers(tp); 7025162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 7035244Sroot soisdisconnected(so); 7045065Swnj break; 7055065Swnj 7064884Swnj /* 7075065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 7084884Swnj */ 7095065Swnj case TCPS_TIME_WAIT: 7105162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 7115065Swnj break; 7125085Swnj } 7134601Swnj } 7145267Sroot if (so->so_options & SO_DEBUG) 7155267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 7165085Swnj 7175085Swnj /* 7185085Swnj * Return any desired output. 7195085Swnj */ 7206161Ssam (void) tcp_output(tp); 7215065Swnj return; 7225085Swnj 7235065Swnj dropafterack: 7245085Swnj /* 7256211Swnj * Generate an ACK dropping incoming segment if it occupies 7266211Swnj * sequence space, where the ACK reflects our state. 7275085Swnj */ 7286211Swnj if ((tiflags&TH_RST) || 7296211Swnj tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 7305085Swnj goto drop; 7316303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 7326303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 7335391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 7345231Swnj return; 7355085Swnj 7365085Swnj dropwithreset: 73711731Ssam if (om) { 7386161Ssam (void) m_free(om); 73911731Ssam om = 0; 74011731Ssam } 7415085Swnj /* 7425244Sroot * Generate a RST, dropping incoming segment. 7435085Swnj * Make ACK acceptable to originator of segment. 7445085Swnj */ 7455085Swnj if (tiflags & TH_RST) 7465085Swnj goto drop; 7475085Swnj if (tiflags & TH_ACK) 7485391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7495085Swnj else { 7505085Swnj if (tiflags & TH_SYN) 7515085Swnj ti->ti_len++; 7526211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 7536211Swnj TH_RST|TH_ACK); 7545085Swnj } 75510769Ssam /* destroy temporarily created socket */ 75610769Ssam if (dropsocket) 75710769Ssam (void) soabort(so); 7585231Swnj return; 7595085Swnj 7605065Swnj drop: 76111730Ssam if (om) 76211730Ssam (void) m_free(om); 7635085Swnj /* 7645085Swnj * Drop space held by incoming segment and return. 7655085Swnj */ 7666303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 7676303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 7685065Swnj m_freem(m); 76910769Ssam /* destroy temporarily created socket */ 77010769Ssam if (dropsocket) 77110769Ssam (void) soabort(so); 7725267Sroot return; 7735065Swnj } 7745065Swnj 77517272Skarels tcp_dooptions(tp, om, ti) 7765440Swnj struct tcpcb *tp; 7775440Swnj struct mbuf *om; 77817272Skarels struct tcpiphdr *ti; 7795419Swnj { 7805440Swnj register u_char *cp; 7815440Swnj int opt, optlen, cnt; 7825419Swnj 7835440Swnj cp = mtod(om, u_char *); 7845440Swnj cnt = om->m_len; 7855440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7865440Swnj opt = cp[0]; 7875440Swnj if (opt == TCPOPT_EOL) 7885440Swnj break; 7895440Swnj if (opt == TCPOPT_NOP) 7905440Swnj optlen = 1; 79112169Ssam else { 7925440Swnj optlen = cp[1]; 79312169Ssam if (optlen <= 0) 79412169Ssam break; 79512169Ssam } 7965440Swnj switch (opt) { 7975440Swnj 7985440Swnj default: 7995440Swnj break; 8005440Swnj 8015440Swnj case TCPOPT_MAXSEG: 8025440Swnj if (optlen != 4) 8035440Swnj continue; 80417272Skarels if (!(ti->ti_flags & TH_SYN)) 80517272Skarels continue; 8065440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 8076161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 80817272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 8095440Swnj break; 8105419Swnj } 8115419Swnj } 8126161Ssam (void) m_free(om); 8135419Swnj } 8145419Swnj 8155419Swnj /* 8165547Swnj * Pull out of band byte out of a segment so 8175547Swnj * it doesn't appear in the user's data queue. 8185547Swnj * It is still reflected in the segment length for 8195547Swnj * sequencing purposes. 8205547Swnj */ 8215547Swnj tcp_pulloutofband(so, ti) 8225547Swnj struct socket *so; 8235547Swnj struct tcpiphdr *ti; 8245547Swnj { 8255547Swnj register struct mbuf *m; 8266116Swnj int cnt = ti->ti_urp - 1; 8275547Swnj 8285547Swnj m = dtom(ti); 8295547Swnj while (cnt >= 0) { 8305547Swnj if (m->m_len > cnt) { 8315547Swnj char *cp = mtod(m, caddr_t) + cnt; 8325547Swnj struct tcpcb *tp = sototcpcb(so); 8335547Swnj 8345547Swnj tp->t_iobc = *cp; 8355547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 8366161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 8375547Swnj m->m_len--; 8385547Swnj return; 8395547Swnj } 8405547Swnj cnt -= m->m_len; 8415547Swnj m = m->m_next; 8425547Swnj if (m == 0) 8435547Swnj break; 8445547Swnj } 8455547Swnj panic("tcp_pulloutofband"); 8465547Swnj } 8475547Swnj 8485547Swnj /* 8495065Swnj * Insert segment ti into reassembly queue of tcp with 8505065Swnj * control block tp. Return TH_FIN if reassembly now includes 8515065Swnj * a segment with FIN. 8525065Swnj */ 8535109Swnj tcp_reass(tp, ti) 8545065Swnj register struct tcpcb *tp; 8555065Swnj register struct tcpiphdr *ti; 8565065Swnj { 8575065Swnj register struct tcpiphdr *q; 8585085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8595263Swnj struct mbuf *m; 8605263Swnj int flags; 8615065Swnj 8625065Swnj /* 8635162Swnj * Call with ti==0 after become established to 8645162Swnj * force pre-ESTABLISHED data up to user socket. 8655065Swnj */ 8665162Swnj if (ti == 0) 8675065Swnj goto present; 8684601Swnj 8695065Swnj /* 8705065Swnj * Find a segment which begins after this one does. 8715065Swnj */ 8725065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8735065Swnj q = (struct tcpiphdr *)q->ti_next) 8745065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8755065Swnj break; 8764601Swnj 8775065Swnj /* 8785065Swnj * If there is a preceding segment, it may provide some of 8795065Swnj * our data already. If so, drop the data from the incoming 8805065Swnj * segment. If it provides all of our data, drop us. 8815065Swnj */ 8825065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8835065Swnj register int i; 8845690Swnj q = (struct tcpiphdr *)q->ti_prev; 8855065Swnj /* conversion to int (in i) handles seq wraparound */ 8865065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8875065Swnj if (i > 0) { 8884924Swnj if (i >= ti->ti_len) 8895065Swnj goto drop; 8907338Swnj m_adj(dtom(ti), i); 8915065Swnj ti->ti_len -= i; 8924924Swnj ti->ti_seq += i; 8934601Swnj } 8945065Swnj q = (struct tcpiphdr *)(q->ti_next); 8955065Swnj } 8964601Swnj 8975065Swnj /* 8985065Swnj * While we overlap succeeding segments trim them or, 8995065Swnj * if they are completely covered, dequeue them. 9005065Swnj */ 9015690Swnj while (q != (struct tcpiphdr *)tp) { 9025065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 9035690Swnj if (i <= 0) 9045690Swnj break; 9055065Swnj if (i < q->ti_len) { 9065690Swnj q->ti_seq += i; 9075065Swnj q->ti_len -= i; 9085065Swnj m_adj(dtom(q), i); 9095065Swnj break; 9104601Swnj } 9115065Swnj q = (struct tcpiphdr *)q->ti_next; 9125623Swnj m = dtom(q->ti_prev); 9135065Swnj remque(q->ti_prev); 9145623Swnj m_freem(m); 9155065Swnj } 9164601Swnj 9175065Swnj /* 9185065Swnj * Stick new segment in its place. 9195065Swnj */ 9205065Swnj insque(ti, q->ti_prev); 9214601Swnj 9225065Swnj present: 9235065Swnj /* 9245244Sroot * Present data to user, advancing rcv_nxt through 9255244Sroot * completed sequence space. 9265065Swnj */ 9275263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 9285244Sroot return (0); 9294924Swnj ti = tp->seg_next; 9305263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 9315263Swnj return (0); 9325263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9335263Swnj return (0); 9345263Swnj do { 9355244Sroot tp->rcv_nxt += ti->ti_len; 9365244Sroot flags = ti->ti_flags & TH_FIN; 9374924Swnj remque(ti); 9385263Swnj m = dtom(ti); 9394924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9405263Swnj if (so->so_state & SS_CANTRCVMORE) 9416161Ssam m_freem(m); 94210145Ssam else 9435263Swnj sbappend(&so->so_rcv, m); 9445263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9455263Swnj sorwakeup(so); 9465065Swnj return (flags); 9475065Swnj drop: 9485065Swnj m_freem(dtom(ti)); 9495263Swnj return (0); 9504601Swnj } 95117272Skarels 95217272Skarels /* 95317272Skarels * Determine a reasonable value for maxseg size. 95417272Skarels * If the route is known, use one that can be handled 95517272Skarels * on the given interface without forcing IP to fragment. 95617272Skarels * If bigger than a page (CLSIZE), round down to nearest pagesize 95717272Skarels * to utilize pagesize mbufs. 95817272Skarels * If interface pointer is unavailable, or the destination isn't local, 95917272Skarels * use a conservative size (512 or the default IP max size), 96017272Skarels * as we can't discover anything about intervening gateways or networks. 96117272Skarels * 96217272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 96317272Skarels */ 96417272Skarels tcp_mss(tp) 96517272Skarels register struct tcpcb *tp; 96617272Skarels { 96717272Skarels struct route *ro; 96817272Skarels struct ifnet *ifp; 96917272Skarels int mss; 97017272Skarels struct inpcb *inp; 97117272Skarels 97217272Skarels inp = tp->t_inpcb; 97317272Skarels ro = &inp->inp_route; 97417272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 97517272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 97617272Skarels /* No route yet, so try to acquire one */ 97717272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 97817272Skarels ro->ro_dst.sa_family = AF_INET; 97917272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 98017272Skarels inp->inp_faddr; 98117272Skarels rtalloc(ro); 98217272Skarels } 98317272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 984*17316Skarels return (TCP_MSS); 98517272Skarels } 98617272Skarels 98717272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 98817272Skarels #if (CLBYTES & (CLBYTES - 1)) == 0 98917272Skarels if (mss > CLBYTES) 99017272Skarels mss &= ~(CLBYTES-1); 99117272Skarels #else 99217272Skarels if (mss > CLBYTES) 99317272Skarels mss = mss / CLBYTES * CLBYTES; 99417272Skarels #endif 99517272Skarels if (in_localaddr(tp->t_inpcb->inp_faddr)) 99617272Skarels return(mss); 997*17316Skarels return (MIN(mss, TCP_MSS)); 99817272Skarels } 999