1*17062Sbloom /* tcp_input.c 6.4 84/08/29 */ 24601Swnj 3*17062Sbloom #include "param.h" 4*17062Sbloom #include "systm.h" 5*17062Sbloom #include "mbuf.h" 6*17062Sbloom #include "protosw.h" 7*17062Sbloom #include "socket.h" 8*17062Sbloom #include "socketvar.h" 9*17062Sbloom #include "errno.h" 1010894Ssam 1110894Ssam #include "../net/if.h" 1210894Ssam #include "../net/route.h" 1310894Ssam 14*17062Sbloom #include "in.h" 15*17062Sbloom #include "in_pcb.h" 16*17062Sbloom #include "in_systm.h" 17*17062Sbloom #include "ip.h" 18*17062Sbloom #include "ip_var.h" 19*17062Sbloom #include "tcp.h" 20*17062Sbloom #include "tcp_fsm.h" 21*17062Sbloom #include "tcp_seq.h" 22*17062Sbloom #include "tcp_timer.h" 23*17062Sbloom #include "tcp_var.h" 24*17062Sbloom #include "tcpip.h" 25*17062Sbloom #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); 2456028Sroot inp->inp_laddr = laddr; 2466320Swnj tp = 0; 2475244Sroot goto drop; 2485244Sroot } 2495085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 2505065Swnj tp->irs = ti->ti_seq; 2515085Swnj tcp_sendseqinit(tp); 2525085Swnj tcp_rcvseqinit(tp); 2535065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2545244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 25510769Ssam dropsocket = 0; /* committed to socket */ 2565085Swnj goto trimthenstep6; 2578271Sroot } 2584601Swnj 2595065Swnj /* 2605065Swnj * If the state is SYN_SENT: 2615065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2625065Swnj * if seg contains a RST, then drop the connection. 2635065Swnj * if seg does not contain SYN, then drop it. 2645065Swnj * Otherwise this is an acceptable SYN segment 2655065Swnj * initialize tp->rcv_nxt and tp->irs 2665065Swnj * if seg contains ack then advance tp->snd_una 2675065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2685065Swnj * arrange for segment to be acked (eventually) 2695065Swnj * continue processing rest of data/controls, beginning with URG 2705065Swnj */ 2715065Swnj case TCPS_SYN_SENT: 2725065Swnj if ((tiflags & TH_ACK) && 2735300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2745300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2755231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2765085Swnj goto dropwithreset; 2775065Swnj if (tiflags & TH_RST) { 27810394Ssam if (tiflags & TH_ACK) 27910394Ssam tp = tcp_drop(tp, ECONNREFUSED); 2805065Swnj goto drop; 2814601Swnj } 2825065Swnj if ((tiflags & TH_SYN) == 0) 2835065Swnj goto drop; 2845231Swnj tp->snd_una = ti->ti_ack; 2855357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2865357Sroot tp->snd_nxt = tp->snd_una; 2875244Sroot tp->t_timer[TCPT_REXMT] = 0; 2885065Swnj tp->irs = ti->ti_seq; 2895085Swnj tcp_rcvseqinit(tp); 2905085Swnj tp->t_flags |= TF_ACKNOW; 2915162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2925244Sroot soisconnected(so); 2935065Swnj tp->t_state = TCPS_ESTABLISHED; 2945162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2955162Swnj } else 2965085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2975085Swnj goto trimthenstep6; 2985085Swnj 2995085Swnj trimthenstep6: 3005085Swnj /* 3015231Swnj * Advance ti->ti_seq to correspond to first data byte. 3025085Swnj * If data, trim to stay within window, 3035085Swnj * dropping FIN if necessary. 3045085Swnj */ 3055231Swnj ti->ti_seq++; 3065085Swnj if (ti->ti_len > tp->rcv_wnd) { 3075085Swnj todrop = ti->ti_len - tp->rcv_wnd; 3085085Swnj m_adj(m, -todrop); 3095085Swnj ti->ti_len = tp->rcv_wnd; 3105085Swnj ti->ti_flags &= ~TH_FIN; 3115065Swnj } 3125263Swnj tp->snd_wl1 = ti->ti_seq - 1; 3135085Swnj goto step6; 3145065Swnj } 3154601Swnj 3165065Swnj /* 31716222Skarels * If data is received on a connection after the 31816222Skarels * user processes are gone, then RST the other end. 31916222Skarels */ 32016222Skarels if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT && 32116222Skarels ti->ti_len) { 32216222Skarels tp = tcp_close(tp); 32316222Skarels goto dropwithreset; 32416222Skarels } 32516222Skarels 32616222Skarels /* 3275065Swnj * States other than LISTEN or SYN_SENT. 3285065Swnj * First check that at least some bytes of segment are within 3295065Swnj * receive window. 3305065Swnj */ 3315065Swnj if (tp->rcv_wnd == 0) { 3325065Swnj /* 3335065Swnj * If window is closed can only take segments at 3345231Swnj * window edge, and have to drop data and PUSH from 3355065Swnj * incoming segments. 3365065Swnj */ 3375300Sroot if (tp->rcv_nxt != ti->ti_seq) 3385065Swnj goto dropafterack; 3395085Swnj if (ti->ti_len > 0) { 3405690Swnj m_adj(m, ti->ti_len); 3415085Swnj ti->ti_len = 0; 3425085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3435065Swnj } 3445065Swnj } else { 3455065Swnj /* 3465231Swnj * If segment begins before rcv_nxt, drop leading 3475065Swnj * data (and SYN); if nothing left, just ack. 3485065Swnj */ 3495690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 3505690Swnj if (todrop > 0) { 3515085Swnj if (tiflags & TH_SYN) { 3525300Sroot tiflags &= ~TH_SYN; 3535690Swnj ti->ti_flags &= ~TH_SYN; 3545085Swnj ti->ti_seq++; 3555085Swnj if (ti->ti_urp > 1) 3565085Swnj ti->ti_urp--; 3575085Swnj else 3585085Swnj tiflags &= ~TH_URG; 3595085Swnj todrop--; 3605085Swnj } 3616211Swnj if (todrop > ti->ti_len || 3626211Swnj todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 3635065Swnj goto dropafterack; 3645065Swnj m_adj(m, todrop); 3655065Swnj ti->ti_seq += todrop; 3665065Swnj ti->ti_len -= todrop; 3675085Swnj if (ti->ti_urp > todrop) 3685085Swnj ti->ti_urp -= todrop; 3695085Swnj else { 3705085Swnj tiflags &= ~TH_URG; 3715690Swnj ti->ti_flags &= ~TH_URG; 3725690Swnj ti->ti_urp = 0; 3735085Swnj } 3745065Swnj } 3755065Swnj /* 3765065Swnj * If segment ends after window, drop trailing data 3775085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3785065Swnj */ 3795690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3805690Swnj if (todrop > 0) { 3816211Swnj if (todrop >= ti->ti_len) 3825065Swnj goto dropafterack; 3835065Swnj m_adj(m, -todrop); 3845065Swnj ti->ti_len -= todrop; 3855085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3865065Swnj } 3875065Swnj } 3884601Swnj 3895065Swnj /* 3905065Swnj * If the RST bit is set examine the state: 3915065Swnj * SYN_RECEIVED STATE: 3925065Swnj * If passive open, return to LISTEN state. 3935065Swnj * If active open, inform user that connection was refused. 3945065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3955065Swnj * Inform user that connection was reset, and close tcb. 3965065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3975065Swnj * Close the tcb. 3985065Swnj */ 3995065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 4005267Sroot 4015065Swnj case TCPS_SYN_RECEIVED: 40210394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4035065Swnj goto drop; 4044601Swnj 4055065Swnj case TCPS_ESTABLISHED: 4065065Swnj case TCPS_FIN_WAIT_1: 4075065Swnj case TCPS_FIN_WAIT_2: 4085065Swnj case TCPS_CLOSE_WAIT: 40910394Ssam tp = tcp_drop(tp, ECONNRESET); 4105065Swnj goto drop; 4115065Swnj 4125065Swnj case TCPS_CLOSING: 4135065Swnj case TCPS_LAST_ACK: 4145065Swnj case TCPS_TIME_WAIT: 41510394Ssam tp = tcp_close(tp); 4165065Swnj goto drop; 4174601Swnj } 4184601Swnj 4194601Swnj /* 4205065Swnj * If a SYN is in the window, then this is an 4215065Swnj * error and we send an RST and drop the connection. 4224601Swnj */ 4235065Swnj if (tiflags & TH_SYN) { 42410394Ssam tp = tcp_drop(tp, ECONNRESET); 4255085Swnj goto dropwithreset; 4264601Swnj } 4274601Swnj 4284601Swnj /* 4295065Swnj * If the ACK bit is off we drop the segment and return. 4304601Swnj */ 4315085Swnj if ((tiflags & TH_ACK) == 0) 4325065Swnj goto drop; 4335065Swnj 4345065Swnj /* 4355065Swnj * Ack processing. 4365065Swnj */ 4374601Swnj switch (tp->t_state) { 4384601Swnj 4395065Swnj /* 4405065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4415065Swnj * ESTABLISHED state and continue processing, othewise 4425065Swnj * send an RST. 4435065Swnj */ 4445065Swnj case TCPS_SYN_RECEIVED: 4455085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4465231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4475085Swnj goto dropwithreset; 4485244Sroot tp->snd_una++; /* SYN acked */ 4495357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4505357Sroot tp->snd_nxt = tp->snd_una; 4515244Sroot tp->t_timer[TCPT_REXMT] = 0; 4525085Swnj soisconnected(so); 4535085Swnj tp->t_state = TCPS_ESTABLISHED; 4545162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4555244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4565085Swnj /* fall into ... */ 4574601Swnj 4585065Swnj /* 4595065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4605065Swnj * ACKs. If the ack is in the range 4615231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4625065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4635065Swnj * data from the retransmission queue. If this ACK reflects 4645065Swnj * more up to date window information we update our window information. 4655065Swnj */ 4665065Swnj case TCPS_ESTABLISHED: 4675065Swnj case TCPS_FIN_WAIT_1: 4685065Swnj case TCPS_FIN_WAIT_2: 4695065Swnj case TCPS_CLOSE_WAIT: 4705065Swnj case TCPS_CLOSING: 4715244Sroot case TCPS_LAST_ACK: 4725244Sroot case TCPS_TIME_WAIT: 4735085Swnj #define ourfinisacked (acked > 0) 4745085Swnj 4755244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4765065Swnj break; 4775300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4785065Swnj goto dropafterack; 4795085Swnj acked = ti->ti_ack - tp->snd_una; 4805951Swnj 4815951Swnj /* 4825951Swnj * If transmit timer is running and timed sequence 4835951Swnj * number was acked, update smoothed round trip time. 4845951Swnj */ 4855951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4865951Swnj if (tp->t_srtt == 0) 4875951Swnj tp->t_srtt = tp->t_rtt; 4885951Swnj else 4895951Swnj tp->t_srtt = 4905951Swnj tcp_alpha * tp->t_srtt + 4915951Swnj (1 - tcp_alpha) * tp->t_rtt; 4925951Swnj tp->t_rtt = 0; 4935951Swnj } 4945951Swnj 4955307Sroot if (ti->ti_ack == tp->snd_max) 4965244Sroot tp->t_timer[TCPT_REXMT] = 0; 4975307Sroot else { 4985244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4995244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 5005951Swnj tp->t_rtt = 1; 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; 5335896Swnj } 5344601Swnj break; 5354601Swnj 5365065Swnj /* 5375065Swnj * In CLOSING STATE in addition to the processing for 5385065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5395065Swnj * then enter the TIME-WAIT state, otherwise ignore 5405065Swnj * the segment. 5415065Swnj */ 5425065Swnj case TCPS_CLOSING: 5435244Sroot if (ourfinisacked) { 5445065Swnj tp->t_state = TCPS_TIME_WAIT; 5455244Sroot tcp_canceltimers(tp); 5465244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5475244Sroot soisdisconnected(so); 5485244Sroot } 5495244Sroot break; 5504601Swnj 5515065Swnj /* 5525085Swnj * The only thing that can arrive in LAST_ACK state 5535085Swnj * is an acknowledgment of our FIN. If our FIN is now 5545085Swnj * acknowledged, delete the TCB, enter the closed state 5555085Swnj * and return. 5565065Swnj */ 5575065Swnj case TCPS_LAST_ACK: 55810394Ssam if (ourfinisacked) 55910394Ssam tp = tcp_close(tp); 5605065Swnj goto drop; 5614601Swnj 5625065Swnj /* 5635065Swnj * In TIME_WAIT state the only thing that should arrive 5645065Swnj * is a retransmission of the remote FIN. Acknowledge 5655065Swnj * it and restart the finack timer. 5665065Swnj */ 5675065Swnj case TCPS_TIME_WAIT: 5685162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5695065Swnj goto dropafterack; 5704601Swnj } 5715085Swnj #undef ourfinisacked 5725085Swnj } 5734601Swnj 5745065Swnj step6: 5755065Swnj /* 5765244Sroot * Update window information. 5775244Sroot */ 5785300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5795391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5805300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5815244Sroot tp->snd_wnd = ti->ti_win; 5825244Sroot tp->snd_wl1 = ti->ti_seq; 5835244Sroot tp->snd_wl2 = ti->ti_ack; 5848599Sroot if (tp->snd_wnd != 0) 5855244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5865244Sroot } 5875244Sroot 5885244Sroot /* 5895547Swnj * Process segments with URG. 5905065Swnj */ 5917267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 5927267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5935547Swnj /* 59413121Ssam * This is a kludge, but if we receive accept 59513121Ssam * random urgent pointers, we'll crash in 59613121Ssam * soreceive. It's hard to imagine someone 59713121Ssam * actually wanting to send this much urgent data. 59812441Ssam */ 59912441Ssam if (ti->ti_urp > tp->t_maxseg) { /* XXX */ 60012441Ssam ti->ti_urp = 0; /* XXX */ 60112441Ssam tiflags &= ~TH_URG; /* XXX */ 60212441Ssam ti->ti_flags &= ~TH_URG; /* XXX */ 60313121Ssam goto badurp; /* XXX */ 60412441Ssam } 60512441Ssam /* 6065547Swnj * If this segment advances the known urgent pointer, 6075547Swnj * then mark the data stream. This should not happen 6085547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 6095547Swnj * a FIN has been received from the remote side. 6105547Swnj * In these states we ignore the URG. 6115547Swnj */ 6125547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 6135547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 6145547Swnj so->so_oobmark = so->so_rcv.sb_cc + 6155547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 6165547Swnj if (so->so_oobmark == 0) 6175547Swnj so->so_state |= SS_RCVATMARK; 6188313Sroot sohasoutofband(so); 6195547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 6205440Swnj } 6215547Swnj /* 6225547Swnj * Remove out of band data so doesn't get presented to user. 6235547Swnj * This can happen independent of advancing the URG pointer, 6245547Swnj * but if two URG's are pending at once, some out-of-band 6255547Swnj * data may creep in... ick. 6265547Swnj */ 6277510Sroot if (ti->ti_urp <= ti->ti_len) 6285547Swnj tcp_pulloutofband(so, ti); 6295419Swnj } 63013121Ssam badurp: /* XXX */ 6314601Swnj 6324601Swnj /* 6335065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6345065Swnj * and arranging for acknowledgment of receipt if necessary. 6355065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6365065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6375065Swnj * case PRU_RCVD). If a FIN has already been received on this 6385065Swnj * connection then we just ignore the text. 6394601Swnj */ 6405263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6415263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6425065Swnj tiflags = tcp_reass(tp, ti); 6435440Swnj if (tcpnodelack == 0) 6445440Swnj tp->t_flags |= TF_DELACK; 6455440Swnj else 6465440Swnj tp->t_flags |= TF_ACKNOW; 6475244Sroot } else { 6484924Swnj m_freem(m); 6495263Swnj tiflags &= ~TH_FIN; 6505244Sroot } 6514601Swnj 6524601Swnj /* 6535263Swnj * If FIN is received ACK the FIN and let the user know 6545263Swnj * that the connection is closing. 6554601Swnj */ 6565263Swnj if (tiflags & TH_FIN) { 6575244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6585244Sroot socantrcvmore(so); 6595244Sroot tp->t_flags |= TF_ACKNOW; 6605244Sroot tp->rcv_nxt++; 6615244Sroot } 6625065Swnj switch (tp->t_state) { 6634601Swnj 6645065Swnj /* 6655065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6665065Swnj * enter the CLOSE_WAIT state. 6674884Swnj */ 6685065Swnj case TCPS_SYN_RECEIVED: 6695065Swnj case TCPS_ESTABLISHED: 6705065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6715065Swnj break; 6724884Swnj 6735065Swnj /* 6745085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6755085Swnj * enter the CLOSING state. 6764884Swnj */ 6775065Swnj case TCPS_FIN_WAIT_1: 6785085Swnj tp->t_state = TCPS_CLOSING; 6795065Swnj break; 6804601Swnj 6815065Swnj /* 6825065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6835065Swnj * starting the time-wait timer, turning off the other 6845065Swnj * standard timers. 6855065Swnj */ 6865065Swnj case TCPS_FIN_WAIT_2: 6875244Sroot tp->t_state = TCPS_TIME_WAIT; 6885074Swnj tcp_canceltimers(tp); 6895162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6905244Sroot soisdisconnected(so); 6915065Swnj break; 6925065Swnj 6934884Swnj /* 6945065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6954884Swnj */ 6965065Swnj case TCPS_TIME_WAIT: 6975162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6985065Swnj break; 6995085Swnj } 7004601Swnj } 7015267Sroot if (so->so_options & SO_DEBUG) 7025267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 7035085Swnj 7045085Swnj /* 7055085Swnj * Return any desired output. 7065085Swnj */ 7076161Ssam (void) tcp_output(tp); 7085065Swnj return; 7095085Swnj 7105065Swnj dropafterack: 7115085Swnj /* 7126211Swnj * Generate an ACK dropping incoming segment if it occupies 7136211Swnj * sequence space, where the ACK reflects our state. 7145085Swnj */ 7156211Swnj if ((tiflags&TH_RST) || 7166211Swnj tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 7175085Swnj goto drop; 7186303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 7196303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 7205391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 7215231Swnj return; 7225085Swnj 7235085Swnj dropwithreset: 72411731Ssam if (om) { 7256161Ssam (void) m_free(om); 72611731Ssam om = 0; 72711731Ssam } 7285085Swnj /* 7295244Sroot * Generate a RST, dropping incoming segment. 7305085Swnj * Make ACK acceptable to originator of segment. 7315085Swnj */ 7325085Swnj if (tiflags & TH_RST) 7335085Swnj goto drop; 7345085Swnj if (tiflags & TH_ACK) 7355391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7365085Swnj else { 7375085Swnj if (tiflags & TH_SYN) 7385085Swnj ti->ti_len++; 7396211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 7406211Swnj TH_RST|TH_ACK); 7415085Swnj } 74210769Ssam /* destroy temporarily created socket */ 74310769Ssam if (dropsocket) 74410769Ssam (void) soabort(so); 7455231Swnj return; 7465085Swnj 7475065Swnj drop: 74811730Ssam if (om) 74911730Ssam (void) m_free(om); 7505085Swnj /* 7515085Swnj * Drop space held by incoming segment and return. 7525085Swnj */ 7536303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 7546303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 7555065Swnj m_freem(m); 75610769Ssam /* destroy temporarily created socket */ 75710769Ssam if (dropsocket) 75810769Ssam (void) soabort(so); 7595267Sroot return; 7605065Swnj } 7615065Swnj 7625440Swnj tcp_dooptions(tp, om) 7635440Swnj struct tcpcb *tp; 7645440Swnj struct mbuf *om; 7655419Swnj { 7665440Swnj register u_char *cp; 7675440Swnj int opt, optlen, cnt; 7685419Swnj 7695440Swnj cp = mtod(om, u_char *); 7705440Swnj cnt = om->m_len; 7715440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7725440Swnj opt = cp[0]; 7735440Swnj if (opt == TCPOPT_EOL) 7745440Swnj break; 7755440Swnj if (opt == TCPOPT_NOP) 7765440Swnj optlen = 1; 77712169Ssam else { 7785440Swnj optlen = cp[1]; 77912169Ssam if (optlen <= 0) 78012169Ssam break; 78112169Ssam } 7825440Swnj switch (opt) { 7835440Swnj 7845440Swnj default: 7855440Swnj break; 7865440Swnj 7875440Swnj case TCPOPT_MAXSEG: 7885440Swnj if (optlen != 4) 7895440Swnj continue; 7905440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7916161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7925440Swnj break; 7935419Swnj } 7945419Swnj } 7956161Ssam (void) m_free(om); 7965419Swnj } 7975419Swnj 7985419Swnj /* 7995547Swnj * Pull out of band byte out of a segment so 8005547Swnj * it doesn't appear in the user's data queue. 8015547Swnj * It is still reflected in the segment length for 8025547Swnj * sequencing purposes. 8035547Swnj */ 8045547Swnj tcp_pulloutofband(so, ti) 8055547Swnj struct socket *so; 8065547Swnj struct tcpiphdr *ti; 8075547Swnj { 8085547Swnj register struct mbuf *m; 8096116Swnj int cnt = ti->ti_urp - 1; 8105547Swnj 8115547Swnj m = dtom(ti); 8125547Swnj while (cnt >= 0) { 8135547Swnj if (m->m_len > cnt) { 8145547Swnj char *cp = mtod(m, caddr_t) + cnt; 8155547Swnj struct tcpcb *tp = sototcpcb(so); 8165547Swnj 8175547Swnj tp->t_iobc = *cp; 8185547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 8196161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 8205547Swnj m->m_len--; 8215547Swnj return; 8225547Swnj } 8235547Swnj cnt -= m->m_len; 8245547Swnj m = m->m_next; 8255547Swnj if (m == 0) 8265547Swnj break; 8275547Swnj } 8285547Swnj panic("tcp_pulloutofband"); 8295547Swnj } 8305547Swnj 8315547Swnj /* 8325065Swnj * Insert segment ti into reassembly queue of tcp with 8335065Swnj * control block tp. Return TH_FIN if reassembly now includes 8345065Swnj * a segment with FIN. 8355065Swnj */ 8365109Swnj tcp_reass(tp, ti) 8375065Swnj register struct tcpcb *tp; 8385065Swnj register struct tcpiphdr *ti; 8395065Swnj { 8405065Swnj register struct tcpiphdr *q; 8415085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8425263Swnj struct mbuf *m; 8435263Swnj int flags; 8445065Swnj 8455065Swnj /* 8465162Swnj * Call with ti==0 after become established to 8475162Swnj * force pre-ESTABLISHED data up to user socket. 8485065Swnj */ 8495162Swnj if (ti == 0) 8505065Swnj goto present; 8514601Swnj 8525065Swnj /* 8535065Swnj * Find a segment which begins after this one does. 8545065Swnj */ 8555065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8565065Swnj q = (struct tcpiphdr *)q->ti_next) 8575065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8585065Swnj break; 8594601Swnj 8605065Swnj /* 8615065Swnj * If there is a preceding segment, it may provide some of 8625065Swnj * our data already. If so, drop the data from the incoming 8635065Swnj * segment. If it provides all of our data, drop us. 8645065Swnj */ 8655065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8665065Swnj register int i; 8675690Swnj q = (struct tcpiphdr *)q->ti_prev; 8685065Swnj /* conversion to int (in i) handles seq wraparound */ 8695065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8705065Swnj if (i > 0) { 8714924Swnj if (i >= ti->ti_len) 8725065Swnj goto drop; 8737338Swnj m_adj(dtom(ti), i); 8745065Swnj ti->ti_len -= i; 8754924Swnj ti->ti_seq += i; 8764601Swnj } 8775065Swnj q = (struct tcpiphdr *)(q->ti_next); 8785065Swnj } 8794601Swnj 8805065Swnj /* 8815065Swnj * While we overlap succeeding segments trim them or, 8825065Swnj * if they are completely covered, dequeue them. 8835065Swnj */ 8845690Swnj while (q != (struct tcpiphdr *)tp) { 8855065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8865690Swnj if (i <= 0) 8875690Swnj break; 8885065Swnj if (i < q->ti_len) { 8895690Swnj q->ti_seq += i; 8905065Swnj q->ti_len -= i; 8915065Swnj m_adj(dtom(q), i); 8925065Swnj break; 8934601Swnj } 8945065Swnj q = (struct tcpiphdr *)q->ti_next; 8955623Swnj m = dtom(q->ti_prev); 8965065Swnj remque(q->ti_prev); 8975623Swnj m_freem(m); 8985065Swnj } 8994601Swnj 9005065Swnj /* 9015065Swnj * Stick new segment in its place. 9025065Swnj */ 9035065Swnj insque(ti, q->ti_prev); 9044601Swnj 9055065Swnj present: 9065065Swnj /* 9075244Sroot * Present data to user, advancing rcv_nxt through 9085244Sroot * completed sequence space. 9095065Swnj */ 9105263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 9115244Sroot return (0); 9124924Swnj ti = tp->seg_next; 9135263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 9145263Swnj return (0); 9155263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9165263Swnj return (0); 9175263Swnj do { 9185244Sroot tp->rcv_nxt += ti->ti_len; 9195244Sroot flags = ti->ti_flags & TH_FIN; 9204924Swnj remque(ti); 9215263Swnj m = dtom(ti); 9224924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9235263Swnj if (so->so_state & SS_CANTRCVMORE) 9246161Ssam m_freem(m); 92510145Ssam else 9265263Swnj sbappend(&so->so_rcv, m); 9275263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9285263Swnj sorwakeup(so); 9295065Swnj return (flags); 9305065Swnj drop: 9315065Swnj m_freem(dtom(ti)); 9325263Swnj return (0); 9334601Swnj } 934