1*10894Ssam /* tcp_input.c 1.89 83/02/10 */ 24601Swnj 34601Swnj #include "../h/param.h" 44601Swnj #include "../h/systm.h" 54663Swnj #include "../h/mbuf.h" 65085Swnj #include "../h/protosw.h" 74663Swnj #include "../h/socket.h" 84803Swnj #include "../h/socketvar.h" 9*10894Ssam #include "../h/errno.h" 10*10894Ssam 11*10894Ssam #include "../net/if.h" 12*10894Ssam #include "../net/route.h" 13*10894Ssam 148401Swnj #include "../netinet/in.h" 158401Swnj #include "../netinet/in_pcb.h" 168401Swnj #include "../netinet/in_systm.h" 178401Swnj #include "../netinet/ip.h" 188401Swnj #include "../netinet/ip_var.h" 198401Swnj #include "../netinet/tcp.h" 208401Swnj #include "../netinet/tcp_fsm.h" 218401Swnj #include "../netinet/tcp_seq.h" 228401Swnj #include "../netinet/tcp_timer.h" 238401Swnj #include "../netinet/tcp_var.h" 248401Swnj #include "../netinet/tcpip.h" 258401Swnj #include "../netinet/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)) { 804924Swnj tcpstat.tcps_badsum++; 816211Swnj if (tcpprintfs) 826211Swnj printf("tcp cksum %x\n", ti->ti_sum); 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) { 934924Swnj tcpstat.tcps_badoff++; 945085Swnj goto drop; 954924Swnj } 966211Swnj tlen -= off; 976211Swnj ti->ti_len = tlen; 985440Swnj if (off > sizeof (struct tcphdr)) { 995440Swnj if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 1005440Swnj tcpstat.tcps_hdrops++; 1015440Swnj goto drop; 1025440Swnj } 1035440Swnj ti = mtod(m, struct tcpiphdr *); 1049642Ssam om = m_get(M_DONTWAIT, MT_DATA); 1055440Swnj if (om == 0) 1065440Swnj goto drop; 1075440Swnj om->m_len = off - sizeof (struct tcphdr); 1085440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 1096161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 1105440Swnj m->m_len -= om->m_len; 1116161Ssam bcopy(op+om->m_len, op, 1126161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 1135440Swnj } 1145440Swnj } 1155065Swnj tiflags = ti->ti_flags; 1164924Swnj 1176093Sroot /* 1186211Swnj * Drop TCP and IP headers. 1196093Sroot */ 1206093Sroot off += sizeof (struct ip); 1216093Sroot m->m_off += off; 1226093Sroot m->m_len -= off; 1236093Sroot 1244924Swnj /* 1255244Sroot * Convert TCP protocol specific fields to host format. 1265085Swnj */ 1275085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1285085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1295085Swnj ti->ti_win = ntohs(ti->ti_win); 1305085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1315085Swnj 1325085Swnj /* 1338271Sroot * Locate pcb for segment. 1344924Swnj */ 1355065Swnj inp = in_pcblookup 1366028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 1376028Sroot INPLOOKUP_WILDCARD); 1385065Swnj 1395065Swnj /* 1405065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1415244Sroot * all data in the incoming segment is discarded. 1425065Swnj */ 1435300Sroot if (inp == 0) 1445085Swnj goto dropwithreset; 1455065Swnj tp = intotcpcb(inp); 1465300Sroot if (tp == 0) 1475085Swnj goto dropwithreset; 1485109Swnj so = inp->inp_socket; 1495267Sroot if (so->so_options & SO_DEBUG) { 1505267Sroot ostate = tp->t_state; 1515267Sroot tcp_saveti = *ti; 1525267Sroot } 1537510Sroot if (so->so_options & SO_ACCEPTCONN) { 1547510Sroot so = sonewconn(so); 1557510Sroot if (so == 0) 1567510Sroot goto drop; 15710769Ssam /* 15810769Ssam * This is ugly, but .... 15910769Ssam * 16010769Ssam * Mark socket as temporary until we're 16110769Ssam * committed to keeping it. The code at 16210769Ssam * ``drop'' and ``dropwithreset'' check the 16310769Ssam * flag dropsocket to see if the temporary 16410769Ssam * socket created here should be discarded. 16510769Ssam * We mark the socket as discardable until 16610769Ssam * we're committed to it below in TCPS_LISTEN. 16710769Ssam */ 16810769Ssam dropsocket++; 1697510Sroot inp = (struct inpcb *)so->so_pcb; 1707510Sroot inp->inp_laddr = ti->ti_dst; 1717510Sroot inp->inp_lport = ti->ti_dport; 1727510Sroot tp = intotcpcb(inp); 1737510Sroot tp->t_state = TCPS_LISTEN; 1747510Sroot } 1754601Swnj 1764601Swnj /* 1775162Swnj * Segment received on connection. 1785162Swnj * Reset idle time and keep-alive timer. 1795162Swnj */ 1805162Swnj tp->t_idle = 0; 1815162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1825162Swnj 1835162Swnj /* 1845440Swnj * Process options. 1855440Swnj */ 1865440Swnj if (om) { 1875440Swnj tcp_dooptions(tp, om); 1885440Swnj om = 0; 1895440Swnj } 1905440Swnj 1915440Swnj /* 1925085Swnj * Calculate amount of space in receive window, 1935085Swnj * and then do TCP input processing. 1944601Swnj */ 1955085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1965231Swnj if (tp->rcv_wnd < 0) 1975231Swnj tp->rcv_wnd = 0; 1984601Swnj 1994601Swnj switch (tp->t_state) { 2004601Swnj 2015065Swnj /* 2025065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 2035065Swnj * If the segment contains an ACK then it is bad and send a RST. 2045065Swnj * If it does not contain a SYN then it is not interesting; drop it. 2055085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 2065065Swnj * tp->iss, and send a segment: 2075085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 2085065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 2095065Swnj * Fill in remote peer address fields if not previously specified. 2105065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 2115244Sroot * segment in this state. 2125065Swnj */ 2138271Sroot case TCPS_LISTEN: { 21410145Ssam struct mbuf *am; 2158271Sroot register struct sockaddr_in *sin; 2168271Sroot 2175065Swnj if (tiflags & TH_RST) 2185065Swnj goto drop; 2195300Sroot if (tiflags & TH_ACK) 2205085Swnj goto dropwithreset; 2215300Sroot if ((tiflags & TH_SYN) == 0) 2225065Swnj goto drop; 22310145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 22410145Ssam if (am == NULL) 22510145Ssam goto drop; 22610145Ssam am->m_len = sizeof (struct sockaddr_in); 2278599Sroot sin = mtod(am, struct sockaddr_in *); 2288271Sroot sin->sin_family = AF_INET; 2298271Sroot sin->sin_addr = ti->ti_src; 2308271Sroot sin->sin_port = ti->ti_sport; 2316028Sroot laddr = inp->inp_laddr; 23210145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 2336028Sroot inp->inp_laddr = ti->ti_dst; 2348599Sroot if (in_pcbconnect(inp, am)) { 2356028Sroot inp->inp_laddr = laddr; 2368716Sroot (void) m_free(am); 2375244Sroot goto drop; 2386028Sroot } 2398716Sroot (void) m_free(am); 2405244Sroot tp->t_template = tcp_template(tp); 2415244Sroot if (tp->t_template == 0) { 2425244Sroot in_pcbdisconnect(inp); 2436028Sroot inp->inp_laddr = laddr; 2446320Swnj tp = 0; 2455244Sroot goto drop; 2465244Sroot } 2475085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 2485065Swnj tp->irs = ti->ti_seq; 2495085Swnj tcp_sendseqinit(tp); 2505085Swnj tcp_rcvseqinit(tp); 2515065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2525244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 25310769Ssam dropsocket = 0; /* committed to socket */ 2545085Swnj goto trimthenstep6; 2558271Sroot } 2564601Swnj 2575065Swnj /* 2585065Swnj * If the state is SYN_SENT: 2595065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2605065Swnj * if seg contains a RST, then drop the connection. 2615065Swnj * if seg does not contain SYN, then drop it. 2625065Swnj * Otherwise this is an acceptable SYN segment 2635065Swnj * initialize tp->rcv_nxt and tp->irs 2645065Swnj * if seg contains ack then advance tp->snd_una 2655065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2665065Swnj * arrange for segment to be acked (eventually) 2675065Swnj * continue processing rest of data/controls, beginning with URG 2685065Swnj */ 2695065Swnj case TCPS_SYN_SENT: 2705065Swnj if ((tiflags & TH_ACK) && 2715300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2725300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2735231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2745085Swnj goto dropwithreset; 2755065Swnj if (tiflags & TH_RST) { 27610394Ssam if (tiflags & TH_ACK) 27710394Ssam tp = tcp_drop(tp, ECONNREFUSED); 2785065Swnj goto drop; 2794601Swnj } 2805065Swnj if ((tiflags & TH_SYN) == 0) 2815065Swnj goto drop; 2825231Swnj tp->snd_una = ti->ti_ack; 2835357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2845357Sroot tp->snd_nxt = tp->snd_una; 2855244Sroot tp->t_timer[TCPT_REXMT] = 0; 2865065Swnj tp->irs = ti->ti_seq; 2875085Swnj tcp_rcvseqinit(tp); 2885085Swnj tp->t_flags |= TF_ACKNOW; 2895162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2905244Sroot soisconnected(so); 2915065Swnj tp->t_state = TCPS_ESTABLISHED; 2925162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2935162Swnj } else 2945085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2955085Swnj goto trimthenstep6; 2965085Swnj 2975085Swnj trimthenstep6: 2985085Swnj /* 2995231Swnj * Advance ti->ti_seq to correspond to first data byte. 3005085Swnj * If data, trim to stay within window, 3015085Swnj * dropping FIN if necessary. 3025085Swnj */ 3035231Swnj ti->ti_seq++; 3045085Swnj if (ti->ti_len > tp->rcv_wnd) { 3055085Swnj todrop = ti->ti_len - tp->rcv_wnd; 3065085Swnj m_adj(m, -todrop); 3075085Swnj ti->ti_len = tp->rcv_wnd; 3085085Swnj ti->ti_flags &= ~TH_FIN; 3095065Swnj } 3105263Swnj tp->snd_wl1 = ti->ti_seq - 1; 3115085Swnj goto step6; 3125065Swnj } 3134601Swnj 3145065Swnj /* 3155065Swnj * States other than LISTEN or SYN_SENT. 3165065Swnj * First check that at least some bytes of segment are within 3175065Swnj * receive window. 3185065Swnj */ 3195065Swnj if (tp->rcv_wnd == 0) { 3205065Swnj /* 3215065Swnj * If window is closed can only take segments at 3225231Swnj * window edge, and have to drop data and PUSH from 3235065Swnj * incoming segments. 3245065Swnj */ 3255300Sroot if (tp->rcv_nxt != ti->ti_seq) 3265065Swnj goto dropafterack; 3275085Swnj if (ti->ti_len > 0) { 3285690Swnj m_adj(m, ti->ti_len); 3295085Swnj ti->ti_len = 0; 3305085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3315065Swnj } 3325065Swnj } else { 3335065Swnj /* 3345231Swnj * If segment begins before rcv_nxt, drop leading 3355065Swnj * data (and SYN); if nothing left, just ack. 3365065Swnj */ 3375690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 3385690Swnj if (todrop > 0) { 3395085Swnj if (tiflags & TH_SYN) { 3405300Sroot tiflags &= ~TH_SYN; 3415690Swnj ti->ti_flags &= ~TH_SYN; 3425085Swnj ti->ti_seq++; 3435085Swnj if (ti->ti_urp > 1) 3445085Swnj ti->ti_urp--; 3455085Swnj else 3465085Swnj tiflags &= ~TH_URG; 3475085Swnj todrop--; 3485085Swnj } 3496211Swnj if (todrop > ti->ti_len || 3506211Swnj todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 3515065Swnj goto dropafterack; 3525065Swnj m_adj(m, todrop); 3535065Swnj ti->ti_seq += todrop; 3545065Swnj ti->ti_len -= todrop; 3555085Swnj if (ti->ti_urp > todrop) 3565085Swnj ti->ti_urp -= todrop; 3575085Swnj else { 3585085Swnj tiflags &= ~TH_URG; 3595690Swnj ti->ti_flags &= ~TH_URG; 3605690Swnj ti->ti_urp = 0; 3615085Swnj } 3625065Swnj } 3635065Swnj /* 3645065Swnj * If segment ends after window, drop trailing data 3655085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3665065Swnj */ 3675690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3685690Swnj if (todrop > 0) { 3696211Swnj if (todrop >= ti->ti_len) 3705065Swnj goto dropafterack; 3715065Swnj m_adj(m, -todrop); 3725065Swnj ti->ti_len -= todrop; 3735085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3745065Swnj } 3755065Swnj } 3764601Swnj 3775065Swnj /* 37810013Ssam * If data is received on a connection after the 3795951Swnj * user processes are gone, then RST the other end. 3805951Swnj */ 38110394Ssam if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT && 38210203Ssam ti->ti_len) { 38310394Ssam tp = tcp_close(tp); 3845951Swnj goto dropwithreset; 3855951Swnj } 3865951Swnj 3875951Swnj /* 3885065Swnj * If the RST bit is set examine the state: 3895065Swnj * SYN_RECEIVED STATE: 3905065Swnj * If passive open, return to LISTEN state. 3915065Swnj * If active open, inform user that connection was refused. 3925065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3935065Swnj * Inform user that connection was reset, and close tcb. 3945065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3955065Swnj * Close the tcb. 3965065Swnj */ 3975065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3985267Sroot 3995065Swnj case TCPS_SYN_RECEIVED: 40010394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4015065Swnj goto drop; 4024601Swnj 4035065Swnj case TCPS_ESTABLISHED: 4045065Swnj case TCPS_FIN_WAIT_1: 4055065Swnj case TCPS_FIN_WAIT_2: 4065065Swnj case TCPS_CLOSE_WAIT: 40710394Ssam tp = tcp_drop(tp, ECONNRESET); 4085065Swnj goto drop; 4095065Swnj 4105065Swnj case TCPS_CLOSING: 4115065Swnj case TCPS_LAST_ACK: 4125065Swnj case TCPS_TIME_WAIT: 41310394Ssam tp = tcp_close(tp); 4145065Swnj goto drop; 4154601Swnj } 4164601Swnj 4174601Swnj /* 4185065Swnj * If a SYN is in the window, then this is an 4195065Swnj * error and we send an RST and drop the connection. 4204601Swnj */ 4215065Swnj if (tiflags & TH_SYN) { 42210394Ssam tp = tcp_drop(tp, ECONNRESET); 4235085Swnj goto dropwithreset; 4244601Swnj } 4254601Swnj 4264601Swnj /* 4275065Swnj * If the ACK bit is off we drop the segment and return. 4284601Swnj */ 4295085Swnj if ((tiflags & TH_ACK) == 0) 4305065Swnj goto drop; 4315065Swnj 4325065Swnj /* 4335065Swnj * Ack processing. 4345065Swnj */ 4354601Swnj switch (tp->t_state) { 4364601Swnj 4375065Swnj /* 4385065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4395065Swnj * ESTABLISHED state and continue processing, othewise 4405065Swnj * send an RST. 4415065Swnj */ 4425065Swnj case TCPS_SYN_RECEIVED: 4435085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4445231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4455085Swnj goto dropwithreset; 4465244Sroot tp->snd_una++; /* SYN acked */ 4475357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4485357Sroot tp->snd_nxt = tp->snd_una; 4495244Sroot tp->t_timer[TCPT_REXMT] = 0; 4505085Swnj soisconnected(so); 4515085Swnj tp->t_state = TCPS_ESTABLISHED; 4525162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4535244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4545085Swnj /* fall into ... */ 4554601Swnj 4565065Swnj /* 4575065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4585065Swnj * ACKs. If the ack is in the range 4595231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4605065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4615065Swnj * data from the retransmission queue. If this ACK reflects 4625065Swnj * more up to date window information we update our window information. 4635065Swnj */ 4645065Swnj case TCPS_ESTABLISHED: 4655065Swnj case TCPS_FIN_WAIT_1: 4665065Swnj case TCPS_FIN_WAIT_2: 4675065Swnj case TCPS_CLOSE_WAIT: 4685065Swnj case TCPS_CLOSING: 4695244Sroot case TCPS_LAST_ACK: 4705244Sroot case TCPS_TIME_WAIT: 4715085Swnj #define ourfinisacked (acked > 0) 4725085Swnj 4735244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4745065Swnj break; 4755300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4765065Swnj goto dropafterack; 4775085Swnj acked = ti->ti_ack - tp->snd_una; 4785951Swnj 4795951Swnj /* 4805951Swnj * If transmit timer is running and timed sequence 4815951Swnj * number was acked, update smoothed round trip time. 4825951Swnj */ 4835951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4845951Swnj if (tp->t_srtt == 0) 4855951Swnj tp->t_srtt = tp->t_rtt; 4865951Swnj else 4875951Swnj tp->t_srtt = 4885951Swnj tcp_alpha * tp->t_srtt + 4895951Swnj (1 - tcp_alpha) * tp->t_rtt; 4905951Swnj tp->t_rtt = 0; 4915951Swnj } 4925951Swnj 4935307Sroot if (ti->ti_ack == tp->snd_max) 4945244Sroot tp->t_timer[TCPT_REXMT] = 0; 4955307Sroot else { 4965244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4975244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4985951Swnj tp->t_rtt = 1; 4995300Sroot tp->t_rxtshift = 0; 5005085Swnj } 5015307Sroot if (acked > so->so_snd.sb_cc) { 5025307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 5035307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 5045307Sroot } else { 5056161Ssam sbdrop(&so->so_snd, acked); 5065307Sroot tp->snd_wnd -= acked; 5075307Sroot acked = 0; 5085307Sroot } 5096434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 5105300Sroot sowwakeup(so); 5115231Swnj tp->snd_una = ti->ti_ack; 5125357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 5135357Sroot tp->snd_nxt = tp->snd_una; 5145162Swnj 5154601Swnj switch (tp->t_state) { 5164601Swnj 5175065Swnj /* 5185065Swnj * In FIN_WAIT_1 STATE in addition to the processing 5195065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 5205085Swnj * then enter FIN_WAIT_2. 5215065Swnj */ 5225065Swnj case TCPS_FIN_WAIT_1: 5235896Swnj if (ourfinisacked) { 5245896Swnj /* 5255896Swnj * If we can't receive any more 5265896Swnj * data, then closing user can proceed. 5275896Swnj */ 5285896Swnj if (so->so_state & SS_CANTRCVMORE) 5295896Swnj soisdisconnected(so); 5305085Swnj tp->t_state = TCPS_FIN_WAIT_2; 5315896Swnj } 5324601Swnj break; 5334601Swnj 5345065Swnj /* 5355065Swnj * In CLOSING STATE in addition to the processing for 5365065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5375065Swnj * then enter the TIME-WAIT state, otherwise ignore 5385065Swnj * the segment. 5395065Swnj */ 5405065Swnj case TCPS_CLOSING: 5415244Sroot if (ourfinisacked) { 5425065Swnj tp->t_state = TCPS_TIME_WAIT; 5435244Sroot tcp_canceltimers(tp); 5445244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5455244Sroot soisdisconnected(so); 5465244Sroot } 5475244Sroot break; 5484601Swnj 5495065Swnj /* 5505085Swnj * The only thing that can arrive in LAST_ACK state 5515085Swnj * is an acknowledgment of our FIN. If our FIN is now 5525085Swnj * acknowledged, delete the TCB, enter the closed state 5535085Swnj * and return. 5545065Swnj */ 5555065Swnj case TCPS_LAST_ACK: 55610394Ssam if (ourfinisacked) 55710394Ssam tp = tcp_close(tp); 5585065Swnj goto drop; 5594601Swnj 5605065Swnj /* 5615065Swnj * In TIME_WAIT state the only thing that should arrive 5625065Swnj * is a retransmission of the remote FIN. Acknowledge 5635065Swnj * it and restart the finack timer. 5645065Swnj */ 5655065Swnj case TCPS_TIME_WAIT: 5665162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5675065Swnj goto dropafterack; 5684601Swnj } 5695085Swnj #undef ourfinisacked 5705085Swnj } 5714601Swnj 5725065Swnj step6: 5735065Swnj /* 5745244Sroot * Update window information. 5755244Sroot */ 5765300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5775391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5785300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5795244Sroot tp->snd_wnd = ti->ti_win; 5805244Sroot tp->snd_wl1 = ti->ti_seq; 5815244Sroot tp->snd_wl2 = ti->ti_ack; 5828599Sroot if (tp->snd_wnd != 0) 5835244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5845244Sroot } 5855244Sroot 5865244Sroot /* 5875547Swnj * Process segments with URG. 5885065Swnj */ 5897267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 5907267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5915547Swnj /* 5925547Swnj * If this segment advances the known urgent pointer, 5935547Swnj * then mark the data stream. This should not happen 5945547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5955547Swnj * a FIN has been received from the remote side. 5965547Swnj * In these states we ignore the URG. 5975547Swnj */ 5985547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5995547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 6005547Swnj so->so_oobmark = so->so_rcv.sb_cc + 6015547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 6025547Swnj if (so->so_oobmark == 0) 6035547Swnj so->so_state |= SS_RCVATMARK; 6048313Sroot sohasoutofband(so); 6055547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 6065440Swnj } 6075547Swnj /* 6085547Swnj * Remove out of band data so doesn't get presented to user. 6095547Swnj * This can happen independent of advancing the URG pointer, 6105547Swnj * but if two URG's are pending at once, some out-of-band 6115547Swnj * data may creep in... ick. 6125547Swnj */ 6137510Sroot if (ti->ti_urp <= ti->ti_len) 6145547Swnj tcp_pulloutofband(so, ti); 6155419Swnj } 6164601Swnj 6174601Swnj /* 6185065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6195065Swnj * and arranging for acknowledgment of receipt if necessary. 6205065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6215065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6225065Swnj * case PRU_RCVD). If a FIN has already been received on this 6235065Swnj * connection then we just ignore the text. 6244601Swnj */ 6255263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6265263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6275065Swnj tiflags = tcp_reass(tp, ti); 6285440Swnj if (tcpnodelack == 0) 6295440Swnj tp->t_flags |= TF_DELACK; 6305440Swnj else 6315440Swnj tp->t_flags |= TF_ACKNOW; 6325244Sroot } else { 6334924Swnj m_freem(m); 6345263Swnj tiflags &= ~TH_FIN; 6355244Sroot } 6364601Swnj 6374601Swnj /* 6385263Swnj * If FIN is received ACK the FIN and let the user know 6395263Swnj * that the connection is closing. 6404601Swnj */ 6415263Swnj if (tiflags & TH_FIN) { 6425244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6435244Sroot socantrcvmore(so); 6445244Sroot tp->t_flags |= TF_ACKNOW; 6455244Sroot tp->rcv_nxt++; 6465244Sroot } 6475065Swnj switch (tp->t_state) { 6484601Swnj 6495065Swnj /* 6505065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6515065Swnj * enter the CLOSE_WAIT state. 6524884Swnj */ 6535065Swnj case TCPS_SYN_RECEIVED: 6545065Swnj case TCPS_ESTABLISHED: 6555065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6565065Swnj break; 6574884Swnj 6585065Swnj /* 6595085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6605085Swnj * enter the CLOSING state. 6614884Swnj */ 6625065Swnj case TCPS_FIN_WAIT_1: 6635085Swnj tp->t_state = TCPS_CLOSING; 6645065Swnj break; 6654601Swnj 6665065Swnj /* 6675065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6685065Swnj * starting the time-wait timer, turning off the other 6695065Swnj * standard timers. 6705065Swnj */ 6715065Swnj case TCPS_FIN_WAIT_2: 6725244Sroot tp->t_state = TCPS_TIME_WAIT; 6735074Swnj tcp_canceltimers(tp); 6745162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6755244Sroot soisdisconnected(so); 6765065Swnj break; 6775065Swnj 6784884Swnj /* 6795065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6804884Swnj */ 6815065Swnj case TCPS_TIME_WAIT: 6825162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6835065Swnj break; 6845085Swnj } 6854601Swnj } 6865267Sroot if (so->so_options & SO_DEBUG) 6875267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6885085Swnj 6895085Swnj /* 6905085Swnj * Return any desired output. 6915085Swnj */ 6926161Ssam (void) tcp_output(tp); 6935065Swnj return; 6945085Swnj 6955065Swnj dropafterack: 6965085Swnj /* 6976211Swnj * Generate an ACK dropping incoming segment if it occupies 6986211Swnj * sequence space, where the ACK reflects our state. 6995085Swnj */ 7006211Swnj if ((tiflags&TH_RST) || 7016211Swnj tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 7025085Swnj goto drop; 7036303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 7046303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 7055391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 7065231Swnj return; 7075085Swnj 7085085Swnj dropwithreset: 7095440Swnj if (om) 7106161Ssam (void) m_free(om); 7115085Swnj /* 7125244Sroot * Generate a RST, dropping incoming segment. 7135085Swnj * Make ACK acceptable to originator of segment. 7145085Swnj */ 7155085Swnj if (tiflags & TH_RST) 7165085Swnj goto drop; 7175085Swnj if (tiflags & TH_ACK) 7185391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7195085Swnj else { 7205085Swnj if (tiflags & TH_SYN) 7215085Swnj ti->ti_len++; 7226211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 7236211Swnj TH_RST|TH_ACK); 7245085Swnj } 72510769Ssam /* destroy temporarily created socket */ 72610769Ssam if (dropsocket) 72710769Ssam (void) soabort(so); 7285231Swnj return; 7295085Swnj 7305065Swnj drop: 7315085Swnj /* 7325085Swnj * Drop space held by incoming segment and return. 7335085Swnj */ 7346303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 7356303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 7365065Swnj m_freem(m); 73710769Ssam /* destroy temporarily created socket */ 73810769Ssam if (dropsocket) 73910769Ssam (void) soabort(so); 7405267Sroot return; 7415065Swnj } 7425065Swnj 7435440Swnj tcp_dooptions(tp, om) 7445440Swnj struct tcpcb *tp; 7455440Swnj struct mbuf *om; 7465419Swnj { 7475440Swnj register u_char *cp; 7485440Swnj int opt, optlen, cnt; 7495419Swnj 7505440Swnj cp = mtod(om, u_char *); 7515440Swnj cnt = om->m_len; 7525440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7535440Swnj opt = cp[0]; 7545440Swnj if (opt == TCPOPT_EOL) 7555440Swnj break; 7565440Swnj if (opt == TCPOPT_NOP) 7575440Swnj optlen = 1; 7585440Swnj else 7595440Swnj optlen = cp[1]; 7605440Swnj switch (opt) { 7615440Swnj 7625440Swnj default: 7635440Swnj break; 7645440Swnj 7655440Swnj case TCPOPT_MAXSEG: 7665440Swnj if (optlen != 4) 7675440Swnj continue; 7685440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7696161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7705440Swnj break; 7715419Swnj } 7725419Swnj } 7736161Ssam (void) m_free(om); 7745419Swnj } 7755419Swnj 7765419Swnj /* 7775547Swnj * Pull out of band byte out of a segment so 7785547Swnj * it doesn't appear in the user's data queue. 7795547Swnj * It is still reflected in the segment length for 7805547Swnj * sequencing purposes. 7815547Swnj */ 7825547Swnj tcp_pulloutofband(so, ti) 7835547Swnj struct socket *so; 7845547Swnj struct tcpiphdr *ti; 7855547Swnj { 7865547Swnj register struct mbuf *m; 7876116Swnj int cnt = ti->ti_urp - 1; 7885547Swnj 7895547Swnj m = dtom(ti); 7905547Swnj while (cnt >= 0) { 7915547Swnj if (m->m_len > cnt) { 7925547Swnj char *cp = mtod(m, caddr_t) + cnt; 7935547Swnj struct tcpcb *tp = sototcpcb(so); 7945547Swnj 7955547Swnj tp->t_iobc = *cp; 7965547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 7976161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 7985547Swnj m->m_len--; 7995547Swnj return; 8005547Swnj } 8015547Swnj cnt -= m->m_len; 8025547Swnj m = m->m_next; 8035547Swnj if (m == 0) 8045547Swnj break; 8055547Swnj } 8065547Swnj panic("tcp_pulloutofband"); 8075547Swnj } 8085547Swnj 8095547Swnj /* 8105065Swnj * Insert segment ti into reassembly queue of tcp with 8115065Swnj * control block tp. Return TH_FIN if reassembly now includes 8125065Swnj * a segment with FIN. 8135065Swnj */ 8145109Swnj tcp_reass(tp, ti) 8155065Swnj register struct tcpcb *tp; 8165065Swnj register struct tcpiphdr *ti; 8175065Swnj { 8185065Swnj register struct tcpiphdr *q; 8195085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8205263Swnj struct mbuf *m; 8215263Swnj int flags; 8225065Swnj 8235065Swnj /* 8245162Swnj * Call with ti==0 after become established to 8255162Swnj * force pre-ESTABLISHED data up to user socket. 8265065Swnj */ 8275162Swnj if (ti == 0) 8285065Swnj goto present; 8294601Swnj 8305065Swnj /* 8315065Swnj * Find a segment which begins after this one does. 8325065Swnj */ 8335065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8345065Swnj q = (struct tcpiphdr *)q->ti_next) 8355065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8365065Swnj break; 8374601Swnj 8385065Swnj /* 8395065Swnj * If there is a preceding segment, it may provide some of 8405065Swnj * our data already. If so, drop the data from the incoming 8415065Swnj * segment. If it provides all of our data, drop us. 8425065Swnj */ 8435065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8445065Swnj register int i; 8455690Swnj q = (struct tcpiphdr *)q->ti_prev; 8465065Swnj /* conversion to int (in i) handles seq wraparound */ 8475065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8485065Swnj if (i > 0) { 8494924Swnj if (i >= ti->ti_len) 8505065Swnj goto drop; 8517338Swnj m_adj(dtom(ti), i); 8525065Swnj ti->ti_len -= i; 8534924Swnj ti->ti_seq += i; 8544601Swnj } 8555065Swnj q = (struct tcpiphdr *)(q->ti_next); 8565065Swnj } 8574601Swnj 8585065Swnj /* 8595065Swnj * While we overlap succeeding segments trim them or, 8605065Swnj * if they are completely covered, dequeue them. 8615065Swnj */ 8625690Swnj while (q != (struct tcpiphdr *)tp) { 8635065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8645690Swnj if (i <= 0) 8655690Swnj break; 8665065Swnj if (i < q->ti_len) { 8675690Swnj q->ti_seq += i; 8685065Swnj q->ti_len -= i; 8695065Swnj m_adj(dtom(q), i); 8705065Swnj break; 8714601Swnj } 8725065Swnj q = (struct tcpiphdr *)q->ti_next; 8735623Swnj m = dtom(q->ti_prev); 8745065Swnj remque(q->ti_prev); 8755623Swnj m_freem(m); 8765065Swnj } 8774601Swnj 8785065Swnj /* 8795065Swnj * Stick new segment in its place. 8805065Swnj */ 8815065Swnj insque(ti, q->ti_prev); 8824601Swnj 8835065Swnj present: 8845065Swnj /* 8855244Sroot * Present data to user, advancing rcv_nxt through 8865244Sroot * completed sequence space. 8875065Swnj */ 8885263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 8895244Sroot return (0); 8904924Swnj ti = tp->seg_next; 8915263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 8925263Swnj return (0); 8935263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 8945263Swnj return (0); 8955263Swnj do { 8965244Sroot tp->rcv_nxt += ti->ti_len; 8975244Sroot flags = ti->ti_flags & TH_FIN; 8984924Swnj remque(ti); 8995263Swnj m = dtom(ti); 9004924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9015263Swnj if (so->so_state & SS_CANTRCVMORE) 9026161Ssam m_freem(m); 90310145Ssam else 9045263Swnj sbappend(&so->so_rcv, m); 9055263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9065263Swnj sorwakeup(so); 9075065Swnj return (flags); 9085065Swnj drop: 9095065Swnj m_freem(dtom(ti)); 9105263Swnj return (0); 9114601Swnj } 912