1*6266Swnj /* tcp_input.c 1.62 82/03/19 */ 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" 95085Swnj #include "../net/in.h" 105085Swnj #include "../net/in_pcb.h" 115085Swnj #include "../net/in_systm.h" 125085Swnj #include "../net/if.h" 134803Swnj #include "../net/ip.h" 144899Swnj #include "../net/ip_var.h" 154803Swnj #include "../net/tcp.h" 164803Swnj #include "../net/tcp_fsm.h" 175085Swnj #include "../net/tcp_seq.h" 185085Swnj #include "../net/tcp_timer.h" 194803Swnj #include "../net/tcp_var.h" 205085Swnj #include "../net/tcpip.h" 215267Sroot #include "../net/tcp_debug.h" 225109Swnj #include "../errno.h" 234601Swnj 245300Sroot int tcpprintfs = 0; 254679Swnj int tcpcksum = 1; 265244Sroot struct sockaddr_in tcp_in = { AF_INET }; 275267Sroot struct tcpiphdr tcp_saveti; 285440Swnj extern tcpnodelack; 294601Swnj 305267Sroot struct tcpcb *tcp_newtcpcb(); 315065Swnj /* 325065Swnj * TCP input routine, follows pages 65-76 of the 335065Swnj * protocol specification dated September, 1981 very closely. 345065Swnj */ 354924Swnj tcp_input(m0) 364924Swnj struct mbuf *m0; 374601Swnj { 384924Swnj register struct tcpiphdr *ti; 394924Swnj struct inpcb *inp; 404924Swnj register struct mbuf *m; 415440Swnj struct mbuf *om = 0; 424924Swnj int len, tlen, off; 435391Swnj register struct tcpcb *tp = 0; 444924Swnj register int tiflags; 454803Swnj struct socket *so; 465109Swnj int todrop, acked; 475267Sroot short ostate; 486028Sroot struct in_addr laddr; 494924Swnj 504601Swnj COUNT(TCP_INPUT); 514924Swnj /* 525244Sroot * Get IP and TCP header together in first mbuf. 535244Sroot * Note: IP leaves IP header in first mbuf. 544924Swnj */ 554924Swnj m = m0; 565020Sroot ti = mtod(m, struct tcpiphdr *); 575244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 585208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 595307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 605307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 615085Swnj tcpstat.tcps_hdrops++; 625307Sroot return; 635085Swnj } 645085Swnj ti = mtod(m, struct tcpiphdr *); 655085Swnj } 664601Swnj 674601Swnj /* 685244Sroot * Checksum extended TCP header and data. 694601Swnj */ 704924Swnj tlen = ((struct ip *)ti)->ip_len; 714924Swnj len = sizeof (struct ip) + tlen; 724679Swnj if (tcpcksum) { 734924Swnj ti->ti_next = ti->ti_prev = 0; 744924Swnj ti->ti_x1 = 0; 755223Swnj ti->ti_len = (u_short)tlen; 765223Swnj #if vax 776161Ssam ti->ti_len = htons((u_short)ti->ti_len); 785223Swnj #endif 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 *); 1045440Swnj om = m_get(M_DONTWAIT); 1055440Swnj if (om == 0) 1065440Swnj goto drop; 1075440Swnj om->m_off = MMINOFF; 1085440Swnj om->m_len = off - sizeof (struct tcphdr); 1095440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 1106161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 1115440Swnj m->m_len -= om->m_len; 1126161Ssam bcopy(op+om->m_len, op, 1136161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 1145440Swnj } 1155440Swnj } 1165065Swnj tiflags = ti->ti_flags; 1174924Swnj 1186093Sroot /* 1196211Swnj * Drop TCP and IP headers. 1206093Sroot */ 1216093Sroot off += sizeof (struct ip); 1226093Sroot m->m_off += off; 1236093Sroot m->m_len -= off; 1246093Sroot 1255231Swnj #if vax 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); 1335231Swnj #endif 1345085Swnj 1355085Swnj /* 1365994Swnj * Locate pcb for segment. On match, update the local 1375994Swnj * address stored in the block to reflect anchoring. 1384924Swnj */ 1395065Swnj inp = in_pcblookup 1406028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 1416028Sroot INPLOOKUP_WILDCARD); 1425065Swnj 1435065Swnj /* 1445065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1455244Sroot * all data in the incoming segment is discarded. 1465065Swnj */ 1475300Sroot if (inp == 0) 1485085Swnj goto dropwithreset; 1495065Swnj tp = intotcpcb(inp); 1505300Sroot if (tp == 0) 1515085Swnj goto dropwithreset; 1525109Swnj so = inp->inp_socket; 1535267Sroot if (so->so_options & SO_DEBUG) { 1545267Sroot ostate = tp->t_state; 1555267Sroot tcp_saveti = *ti; 1565267Sroot } 1574601Swnj 1584601Swnj /* 1595162Swnj * Segment received on connection. 1605162Swnj * Reset idle time and keep-alive timer. 1615162Swnj */ 1625162Swnj tp->t_idle = 0; 1635162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1645162Swnj 1655162Swnj /* 1665440Swnj * Process options. 1675440Swnj */ 1685440Swnj if (om) { 1695440Swnj tcp_dooptions(tp, om); 1705440Swnj om = 0; 1715440Swnj } 1725440Swnj 1735440Swnj /* 1745085Swnj * Calculate amount of space in receive window, 1755085Swnj * and then do TCP input processing. 1764601Swnj */ 1775085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1785231Swnj if (tp->rcv_wnd < 0) 1795231Swnj tp->rcv_wnd = 0; 1804601Swnj 1814601Swnj switch (tp->t_state) { 1824601Swnj 1835065Swnj /* 1845065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1855065Swnj * If the segment contains an ACK then it is bad and send a RST. 1865065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1875085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1885065Swnj * tp->iss, and send a segment: 1895085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1905065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1915065Swnj * Fill in remote peer address fields if not previously specified. 1925065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1935244Sroot * segment in this state. 1945065Swnj */ 1955065Swnj case TCPS_LISTEN: 1965065Swnj if (tiflags & TH_RST) 1975065Swnj goto drop; 1985300Sroot if (tiflags & TH_ACK) 1995085Swnj goto dropwithreset; 2005300Sroot if ((tiflags & TH_SYN) == 0) 2015065Swnj goto drop; 2025244Sroot tcp_in.sin_addr = ti->ti_src; 2035244Sroot tcp_in.sin_port = ti->ti_sport; 2046028Sroot laddr = inp->inp_laddr; 2056028Sroot if (inp->inp_laddr.s_addr == 0) 2066028Sroot inp->inp_laddr = ti->ti_dst; 2076161Ssam if (in_pcbconnect(inp, (struct sockaddr_in *)&tcp_in)) { 2086028Sroot inp->inp_laddr = laddr; 2095244Sroot goto drop; 2106028Sroot } 2115244Sroot tp->t_template = tcp_template(tp); 2125244Sroot if (tp->t_template == 0) { 2135244Sroot in_pcbdisconnect(inp); 2146028Sroot inp->inp_laddr = laddr; 2155244Sroot goto drop; 2165244Sroot } 2176028Sroot in_setsockaddr(inp); 2185085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 2195065Swnj tp->irs = ti->ti_seq; 2205085Swnj tcp_sendseqinit(tp); 2215085Swnj tcp_rcvseqinit(tp); 2225065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2235244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 2245085Swnj goto trimthenstep6; 2254601Swnj 2265065Swnj /* 2275065Swnj * If the state is SYN_SENT: 2285065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2295065Swnj * if seg contains a RST, then drop the connection. 2305065Swnj * if seg does not contain SYN, then drop it. 2315065Swnj * Otherwise this is an acceptable SYN segment 2325065Swnj * initialize tp->rcv_nxt and tp->irs 2335065Swnj * if seg contains ack then advance tp->snd_una 2345065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2355065Swnj * arrange for segment to be acked (eventually) 2365065Swnj * continue processing rest of data/controls, beginning with URG 2375065Swnj */ 2385065Swnj case TCPS_SYN_SENT: 2395065Swnj if ((tiflags & TH_ACK) && 2405300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2415300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2425231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2435085Swnj goto dropwithreset; 2445065Swnj if (tiflags & TH_RST) { 2455065Swnj if (tiflags & TH_ACK) 2465267Sroot tcp_drop(tp, ECONNREFUSED); 2475065Swnj goto drop; 2484601Swnj } 2495065Swnj if ((tiflags & TH_SYN) == 0) 2505065Swnj goto drop; 2515231Swnj tp->snd_una = ti->ti_ack; 2525357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2535357Sroot tp->snd_nxt = tp->snd_una; 2545244Sroot tp->t_timer[TCPT_REXMT] = 0; 2555065Swnj tp->irs = ti->ti_seq; 2565085Swnj tcp_rcvseqinit(tp); 2575085Swnj tp->t_flags |= TF_ACKNOW; 2585162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2595391Swnj if (so->so_options & SO_ACCEPTCONN) 2605391Swnj so->so_state |= SS_CONNAWAITING; 2615244Sroot soisconnected(so); 2625065Swnj tp->t_state = TCPS_ESTABLISHED; 2635162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2645162Swnj } else 2655085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2665085Swnj goto trimthenstep6; 2675085Swnj 2685085Swnj trimthenstep6: 2695085Swnj /* 2705231Swnj * Advance ti->ti_seq to correspond to first data byte. 2715085Swnj * If data, trim to stay within window, 2725085Swnj * dropping FIN if necessary. 2735085Swnj */ 2745231Swnj ti->ti_seq++; 2755085Swnj if (ti->ti_len > tp->rcv_wnd) { 2765085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2775085Swnj m_adj(m, -todrop); 2785085Swnj ti->ti_len = tp->rcv_wnd; 2795085Swnj ti->ti_flags &= ~TH_FIN; 2805065Swnj } 2815263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2825085Swnj goto step6; 2835065Swnj } 2844601Swnj 2855065Swnj /* 2865065Swnj * States other than LISTEN or SYN_SENT. 2875065Swnj * First check that at least some bytes of segment are within 2885065Swnj * receive window. 2895065Swnj */ 2905065Swnj if (tp->rcv_wnd == 0) { 2915065Swnj /* 2925065Swnj * If window is closed can only take segments at 2935231Swnj * window edge, and have to drop data and PUSH from 2945065Swnj * incoming segments. 2955065Swnj */ 2965300Sroot if (tp->rcv_nxt != ti->ti_seq) 2975065Swnj goto dropafterack; 2985085Swnj if (ti->ti_len > 0) { 2995690Swnj m_adj(m, ti->ti_len); 3005085Swnj ti->ti_len = 0; 3015085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3025065Swnj } 3035065Swnj } else { 3045065Swnj /* 3055231Swnj * If segment begins before rcv_nxt, drop leading 3065065Swnj * data (and SYN); if nothing left, just ack. 3075065Swnj */ 3085690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 3095690Swnj if (todrop > 0) { 3105085Swnj if (tiflags & TH_SYN) { 3115300Sroot tiflags &= ~TH_SYN; 3125690Swnj ti->ti_flags &= ~TH_SYN; 3135085Swnj ti->ti_seq++; 3145085Swnj if (ti->ti_urp > 1) 3155085Swnj ti->ti_urp--; 3165085Swnj else 3175085Swnj tiflags &= ~TH_URG; 3185085Swnj todrop--; 3195085Swnj } 3206211Swnj if (todrop > ti->ti_len || 3216211Swnj todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 3225065Swnj goto dropafterack; 3235065Swnj m_adj(m, todrop); 3245065Swnj ti->ti_seq += todrop; 3255065Swnj ti->ti_len -= todrop; 3265085Swnj if (ti->ti_urp > todrop) 3275085Swnj ti->ti_urp -= todrop; 3285085Swnj else { 3295085Swnj tiflags &= ~TH_URG; 3305690Swnj ti->ti_flags &= ~TH_URG; 3315690Swnj ti->ti_urp = 0; 3325085Swnj } 3335065Swnj } 3345065Swnj /* 3355065Swnj * If segment ends after window, drop trailing data 3365085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3375065Swnj */ 3385690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3395690Swnj if (todrop > 0) { 3406211Swnj if (todrop >= ti->ti_len) 3415065Swnj goto dropafterack; 3425065Swnj m_adj(m, -todrop); 3435065Swnj ti->ti_len -= todrop; 3445085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3455065Swnj } 3465065Swnj } 3474601Swnj 3485065Swnj /* 3495951Swnj * If a segment is received on a connection after the 3505951Swnj * user processes are gone, then RST the other end. 3515951Swnj */ 3525951Swnj if (so->so_state & SS_USERGONE) { 3535951Swnj tcp_close(tp); 354*6266Swnj tp = 0; 3555951Swnj goto dropwithreset; 3565951Swnj } 3575951Swnj 3585951Swnj /* 3595065Swnj * If the RST bit is set examine the state: 3605065Swnj * SYN_RECEIVED STATE: 3615065Swnj * If passive open, return to LISTEN state. 3625065Swnj * If active open, inform user that connection was refused. 3635065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3645065Swnj * Inform user that connection was reset, and close tcb. 3655065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3665065Swnj * Close the tcb. 3675065Swnj */ 3685065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3695267Sroot 3705065Swnj case TCPS_SYN_RECEIVED: 3715065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3725267Sroot /* a miniature tcp_close, but invisible to user */ 3735267Sroot (void) m_free(dtom(tp->t_template)); 3745267Sroot (void) m_free(dtom(tp)); 3755267Sroot inp->inp_ppcb = 0; 3765267Sroot tp = tcp_newtcpcb(inp); 3775085Swnj tp->t_state = TCPS_LISTEN; 3786028Sroot inp->inp_faddr.s_addr = 0; 3796028Sroot inp->inp_fport = 0; 3806028Sroot inp->inp_laddr.s_addr = 0; /* not quite right */ 3815065Swnj goto drop; 3824601Swnj } 3835085Swnj tcp_drop(tp, ECONNREFUSED); 3845065Swnj goto drop; 3854601Swnj 3865065Swnj case TCPS_ESTABLISHED: 3875065Swnj case TCPS_FIN_WAIT_1: 3885065Swnj case TCPS_FIN_WAIT_2: 3895065Swnj case TCPS_CLOSE_WAIT: 3905065Swnj tcp_drop(tp, ECONNRESET); 3915065Swnj goto drop; 3925065Swnj 3935065Swnj case TCPS_CLOSING: 3945065Swnj case TCPS_LAST_ACK: 3955065Swnj case TCPS_TIME_WAIT: 3965065Swnj tcp_close(tp); 3975065Swnj goto drop; 3984601Swnj } 3994601Swnj 4004601Swnj /* 4015065Swnj * If a SYN is in the window, then this is an 4025065Swnj * error and we send an RST and drop the connection. 4034601Swnj */ 4045065Swnj if (tiflags & TH_SYN) { 4055231Swnj tcp_drop(tp, ECONNRESET); 406*6266Swnj tp = 0; 4075085Swnj goto dropwithreset; 4084601Swnj } 4094601Swnj 4104601Swnj /* 4115065Swnj * If the ACK bit is off we drop the segment and return. 4124601Swnj */ 4135085Swnj if ((tiflags & TH_ACK) == 0) 4145065Swnj goto drop; 4155065Swnj 4165065Swnj /* 4175065Swnj * Ack processing. 4185065Swnj */ 4194601Swnj switch (tp->t_state) { 4204601Swnj 4215065Swnj /* 4225065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4235065Swnj * ESTABLISHED state and continue processing, othewise 4245065Swnj * send an RST. 4255065Swnj */ 4265065Swnj case TCPS_SYN_RECEIVED: 4275085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4285231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4295085Swnj goto dropwithreset; 4305244Sroot tp->snd_una++; /* SYN acked */ 4315357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4325357Sroot tp->snd_nxt = tp->snd_una; 4335244Sroot tp->t_timer[TCPT_REXMT] = 0; 4345391Swnj if (so->so_options & SO_ACCEPTCONN) 4355391Swnj so->so_state |= SS_CONNAWAITING; 4365085Swnj soisconnected(so); 4375085Swnj tp->t_state = TCPS_ESTABLISHED; 4385162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4395244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4405085Swnj /* fall into ... */ 4414601Swnj 4425065Swnj /* 4435065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4445065Swnj * ACKs. If the ack is in the range 4455231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4465065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4475065Swnj * data from the retransmission queue. If this ACK reflects 4485065Swnj * more up to date window information we update our window information. 4495065Swnj */ 4505065Swnj case TCPS_ESTABLISHED: 4515065Swnj case TCPS_FIN_WAIT_1: 4525065Swnj case TCPS_FIN_WAIT_2: 4535065Swnj case TCPS_CLOSE_WAIT: 4545065Swnj case TCPS_CLOSING: 4555244Sroot case TCPS_LAST_ACK: 4565244Sroot case TCPS_TIME_WAIT: 4575085Swnj #define ourfinisacked (acked > 0) 4585085Swnj 4595244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4605065Swnj break; 4615300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4625065Swnj goto dropafterack; 4635085Swnj acked = ti->ti_ack - tp->snd_una; 4645951Swnj 4655951Swnj /* 4665951Swnj * If transmit timer is running and timed sequence 4675951Swnj * number was acked, update smoothed round trip time. 4685951Swnj */ 4695951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4705951Swnj if (tp->t_srtt == 0) 4715951Swnj tp->t_srtt = tp->t_rtt; 4725951Swnj else 4735951Swnj tp->t_srtt = 4745951Swnj tcp_alpha * tp->t_srtt + 4755951Swnj (1 - tcp_alpha) * tp->t_rtt; 4765951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */ 4775951Swnj tp->t_rtt = 0; 4785951Swnj } 4795951Swnj 4805307Sroot if (ti->ti_ack == tp->snd_max) 4815244Sroot tp->t_timer[TCPT_REXMT] = 0; 4825307Sroot else { 4835244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4845244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4855951Swnj tp->t_rtt = 1; 4865300Sroot tp->t_rxtshift = 0; 4875085Swnj } 4885307Sroot if (acked > so->so_snd.sb_cc) { 4895307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4905307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4915307Sroot } else { 4926161Ssam sbdrop(&so->so_snd, acked); 4935307Sroot tp->snd_wnd -= acked; 4945307Sroot acked = 0; 4955307Sroot } 4965300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4975300Sroot sowwakeup(so); 4985231Swnj tp->snd_una = ti->ti_ack; 4995357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 5005357Sroot tp->snd_nxt = tp->snd_una; 5015162Swnj 5024601Swnj switch (tp->t_state) { 5034601Swnj 5045065Swnj /* 5055065Swnj * In FIN_WAIT_1 STATE in addition to the processing 5065065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 5075085Swnj * then enter FIN_WAIT_2. 5085065Swnj */ 5095065Swnj case TCPS_FIN_WAIT_1: 5105896Swnj if (ourfinisacked) { 5115896Swnj /* 5125896Swnj * If we can't receive any more 5135896Swnj * data, then closing user can proceed. 5145896Swnj */ 5155896Swnj if (so->so_state & SS_CANTRCVMORE) 5165896Swnj soisdisconnected(so); 5175085Swnj tp->t_state = TCPS_FIN_WAIT_2; 5185896Swnj } 5194601Swnj break; 5204601Swnj 5215065Swnj /* 5225065Swnj * In CLOSING STATE in addition to the processing for 5235065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5245065Swnj * then enter the TIME-WAIT state, otherwise ignore 5255065Swnj * the segment. 5265065Swnj */ 5275065Swnj case TCPS_CLOSING: 5285244Sroot if (ourfinisacked) { 5295065Swnj tp->t_state = TCPS_TIME_WAIT; 5305244Sroot tcp_canceltimers(tp); 5315244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5325244Sroot soisdisconnected(so); 5335244Sroot } 5345244Sroot break; 5354601Swnj 5365065Swnj /* 5375085Swnj * The only thing that can arrive in LAST_ACK state 5385085Swnj * is an acknowledgment of our FIN. If our FIN is now 5395085Swnj * acknowledged, delete the TCB, enter the closed state 5405085Swnj * and return. 5415065Swnj */ 5425065Swnj case TCPS_LAST_ACK: 5435251Sroot if (ourfinisacked) 5445065Swnj tcp_close(tp); 5455065Swnj goto drop; 5464601Swnj 5475065Swnj /* 5485065Swnj * In TIME_WAIT state the only thing that should arrive 5495065Swnj * is a retransmission of the remote FIN. Acknowledge 5505065Swnj * it and restart the finack timer. 5515065Swnj */ 5525065Swnj case TCPS_TIME_WAIT: 5535162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5545065Swnj goto dropafterack; 5554601Swnj } 5565085Swnj #undef ourfinisacked 5575085Swnj } 5584601Swnj 5595065Swnj step6: 5605065Swnj /* 5615244Sroot * Update window information. 5625244Sroot */ 5635300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5645391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5655300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5665244Sroot tp->snd_wnd = ti->ti_win; 5675244Sroot tp->snd_wl1 = ti->ti_seq; 5685244Sroot tp->snd_wl2 = ti->ti_ack; 5695244Sroot if (tp->snd_wnd > 0) 5705244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5715244Sroot } 5725244Sroot 5735244Sroot /* 5745547Swnj * Process segments with URG. 5755065Swnj */ 5765547Swnj if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5775547Swnj /* 5785547Swnj * If this segment advances the known urgent pointer, 5795547Swnj * then mark the data stream. This should not happen 5805547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5815547Swnj * a FIN has been received from the remote side. 5825547Swnj * In these states we ignore the URG. 5835547Swnj */ 5845547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5855547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 5865547Swnj so->so_oobmark = so->so_rcv.sb_cc + 5875547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 5885547Swnj if (so->so_oobmark == 0) 5895547Swnj so->so_state |= SS_RCVATMARK; 5905440Swnj #ifdef TCPTRUEOOB 5915547Swnj if ((tp->t_flags & TF_DOOOB) == 0) 5925440Swnj #endif 5935547Swnj sohasoutofband(so); 5945547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 5955440Swnj } 5965547Swnj /* 5975547Swnj * Remove out of band data so doesn't get presented to user. 5985547Swnj * This can happen independent of advancing the URG pointer, 5995547Swnj * but if two URG's are pending at once, some out-of-band 6005547Swnj * data may creep in... ick. 6015547Swnj */ 6025547Swnj if (ti->ti_urp <= ti->ti_len) { 6035547Swnj tcp_pulloutofband(so, ti); 6045547Swnj } 6055419Swnj } 6064601Swnj 6074601Swnj /* 6085065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6095065Swnj * and arranging for acknowledgment of receipt if necessary. 6105065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6115065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6125065Swnj * case PRU_RCVD). If a FIN has already been received on this 6135065Swnj * connection then we just ignore the text. 6144601Swnj */ 6155263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6165263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6175065Swnj tiflags = tcp_reass(tp, ti); 6185440Swnj if (tcpnodelack == 0) 6195440Swnj tp->t_flags |= TF_DELACK; 6205440Swnj else 6215440Swnj tp->t_flags |= TF_ACKNOW; 6225244Sroot } else { 6234924Swnj m_freem(m); 6245263Swnj tiflags &= ~TH_FIN; 6255244Sroot } 6264601Swnj 6274601Swnj /* 6285263Swnj * If FIN is received ACK the FIN and let the user know 6295263Swnj * that the connection is closing. 6304601Swnj */ 6315263Swnj if (tiflags & TH_FIN) { 6325244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6335244Sroot socantrcvmore(so); 6345244Sroot tp->t_flags |= TF_ACKNOW; 6355244Sroot tp->rcv_nxt++; 6365244Sroot } 6375065Swnj switch (tp->t_state) { 6384601Swnj 6395065Swnj /* 6405065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6415065Swnj * enter the CLOSE_WAIT state. 6424884Swnj */ 6435065Swnj case TCPS_SYN_RECEIVED: 6445065Swnj case TCPS_ESTABLISHED: 6455065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6465065Swnj break; 6474884Swnj 6485065Swnj /* 6495085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6505085Swnj * enter the CLOSING state. 6514884Swnj */ 6525065Swnj case TCPS_FIN_WAIT_1: 6535085Swnj tp->t_state = TCPS_CLOSING; 6545065Swnj break; 6554601Swnj 6565065Swnj /* 6575065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6585065Swnj * starting the time-wait timer, turning off the other 6595065Swnj * standard timers. 6605065Swnj */ 6615065Swnj case TCPS_FIN_WAIT_2: 6625244Sroot tp->t_state = TCPS_TIME_WAIT; 6635074Swnj tcp_canceltimers(tp); 6645162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6655244Sroot soisdisconnected(so); 6665065Swnj break; 6675065Swnj 6684884Swnj /* 6695065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6704884Swnj */ 6715065Swnj case TCPS_TIME_WAIT: 6725162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6735065Swnj break; 6745085Swnj } 6754601Swnj } 6765267Sroot if (so->so_options & SO_DEBUG) 6775267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6785085Swnj 6795085Swnj /* 6805085Swnj * Return any desired output. 6815085Swnj */ 6826161Ssam (void) tcp_output(tp); 6835065Swnj return; 6845085Swnj 6855065Swnj dropafterack: 6865085Swnj /* 6876211Swnj * Generate an ACK dropping incoming segment if it occupies 6886211Swnj * sequence space, where the ACK reflects our state. 6895085Swnj */ 6906211Swnj if ((tiflags&TH_RST) || 6916211Swnj tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 6925085Swnj goto drop; 6935391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6945231Swnj return; 6955085Swnj 6965085Swnj dropwithreset: 6975440Swnj if (om) 6986161Ssam (void) m_free(om); 6995085Swnj /* 7005244Sroot * Generate a RST, dropping incoming segment. 7015085Swnj * Make ACK acceptable to originator of segment. 7025085Swnj */ 7035085Swnj if (tiflags & TH_RST) 7045085Swnj goto drop; 7055085Swnj if (tiflags & TH_ACK) 7065391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7075085Swnj else { 7085085Swnj if (tiflags & TH_SYN) 7095085Swnj ti->ti_len++; 7106211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 7116211Swnj TH_RST|TH_ACK); 7125085Swnj } 7135231Swnj return; 7145085Swnj 7155065Swnj drop: 7165085Swnj /* 7175085Swnj * Drop space held by incoming segment and return. 7185085Swnj */ 7195065Swnj m_freem(m); 7205267Sroot return; 7215065Swnj } 7225065Swnj 7235440Swnj tcp_dooptions(tp, om) 7245440Swnj struct tcpcb *tp; 7255440Swnj struct mbuf *om; 7265419Swnj { 7275440Swnj register u_char *cp; 7285440Swnj int opt, optlen, cnt; 7295419Swnj 7305440Swnj cp = mtod(om, u_char *); 7315440Swnj cnt = om->m_len; 7325440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7335440Swnj opt = cp[0]; 7345440Swnj if (opt == TCPOPT_EOL) 7355440Swnj break; 7365440Swnj if (opt == TCPOPT_NOP) 7375440Swnj optlen = 1; 7385440Swnj else 7395440Swnj optlen = cp[1]; 7405440Swnj switch (opt) { 7415440Swnj 7425440Swnj default: 7435440Swnj break; 7445440Swnj 7455440Swnj case TCPOPT_MAXSEG: 7465440Swnj if (optlen != 4) 7475440Swnj continue; 7485440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7495440Swnj #if vax 7506161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7515440Swnj #endif 7525440Swnj break; 7535440Swnj 7545440Swnj #ifdef TCPTRUEOOB 7555440Swnj case TCPOPT_WILLOOB: 7565440Swnj tp->t_flags |= TF_DOOOB; 7575440Swnj printf("tp %x dooob\n", tp); 7585440Swnj break; 7595440Swnj 7605440Swnj case TCPOPT_OOBDATA: { 7615440Swnj int seq; 7625547Swnj register struct socket *so = tp->t_inpcb->inp_socket; 7635547Swnj tcp_seq mark; 7645440Swnj 7655547Swnj if (optlen != 8) 7665440Swnj continue; 7675440Swnj seq = cp[2]; 7685440Swnj if (seq < tp->t_iobseq) 7695440Swnj seq += 256; 7705440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 7715440Swnj if (seq - tp->t_iobseq > 128) { 7725440Swnj printf("bad seq\n"); 7735440Swnj tp->t_oobflags |= TCPOOB_OWEACK; 7745440Swnj break; 7755440Swnj } 7765440Swnj tp->t_iobseq = cp[2]; 7775440Swnj tp->t_iobc = cp[3]; 7785547Swnj mark = *(tcp_seq *)(cp + 4); 7795547Swnj #if vax 7805547Swnj mark = ntohl(mark); 7815547Swnj #endif 7825547Swnj so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 7835547Swnj if (so->so_oobmark == 0) 7845547Swnj so->so_state |= SS_RCVATMARK; 7855440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 7865547Swnj sohasoutofband(so); 7875440Swnj break; 7885419Swnj } 7895440Swnj 7905440Swnj case TCPOPT_OOBACK: { 7915440Swnj int seq; 7925440Swnj 7935440Swnj if (optlen != 4) 7945440Swnj continue; 7955440Swnj if (tp->t_oobseq != cp[2]) { 7965440Swnj printf("wrong ack\n"); 7975440Swnj break; 7985440Swnj } 7995440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]); 8005440Swnj tp->t_oobflags &= ~TCPOOB_NEEDACK; 8015440Swnj tp->t_timer[TCPT_OOBREXMT] = 0; 8025419Swnj break; 8035440Swnj } 8045440Swnj #endif TCPTRUEOOB 8055440Swnj } 8065419Swnj } 8076161Ssam (void) m_free(om); 8085419Swnj } 8095419Swnj 8105419Swnj /* 8115547Swnj * Pull out of band byte out of a segment so 8125547Swnj * it doesn't appear in the user's data queue. 8135547Swnj * It is still reflected in the segment length for 8145547Swnj * sequencing purposes. 8155547Swnj */ 8165547Swnj tcp_pulloutofband(so, ti) 8175547Swnj struct socket *so; 8185547Swnj struct tcpiphdr *ti; 8195547Swnj { 8205547Swnj register struct mbuf *m; 8216116Swnj int cnt = ti->ti_urp - 1; 8225547Swnj 8235547Swnj m = dtom(ti); 8245547Swnj while (cnt >= 0) { 8255547Swnj if (m->m_len > cnt) { 8265547Swnj char *cp = mtod(m, caddr_t) + cnt; 8275547Swnj struct tcpcb *tp = sototcpcb(so); 8285547Swnj 8295547Swnj tp->t_iobc = *cp; 8305547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 8316161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 8325547Swnj m->m_len--; 8335547Swnj return; 8345547Swnj } 8355547Swnj cnt -= m->m_len; 8365547Swnj m = m->m_next; 8375547Swnj if (m == 0) 8385547Swnj break; 8395547Swnj } 8405547Swnj panic("tcp_pulloutofband"); 8415547Swnj } 8425547Swnj 8435547Swnj /* 8445065Swnj * Insert segment ti into reassembly queue of tcp with 8455065Swnj * control block tp. Return TH_FIN if reassembly now includes 8465065Swnj * a segment with FIN. 8475065Swnj */ 8485109Swnj tcp_reass(tp, ti) 8495065Swnj register struct tcpcb *tp; 8505065Swnj register struct tcpiphdr *ti; 8515065Swnj { 8525065Swnj register struct tcpiphdr *q; 8535085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8545263Swnj struct mbuf *m; 8555263Swnj int flags; 8565085Swnj COUNT(TCP_REASS); 8575065Swnj 8585065Swnj /* 8595162Swnj * Call with ti==0 after become established to 8605162Swnj * force pre-ESTABLISHED data up to user socket. 8615065Swnj */ 8625162Swnj if (ti == 0) 8635065Swnj goto present; 8644601Swnj 8655065Swnj /* 8665065Swnj * Find a segment which begins after this one does. 8675065Swnj */ 8685065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8695065Swnj q = (struct tcpiphdr *)q->ti_next) 8705065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8715065Swnj break; 8724601Swnj 8735065Swnj /* 8745065Swnj * If there is a preceding segment, it may provide some of 8755065Swnj * our data already. If so, drop the data from the incoming 8765065Swnj * segment. If it provides all of our data, drop us. 8775065Swnj */ 8785065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8795065Swnj register int i; 8805690Swnj q = (struct tcpiphdr *)q->ti_prev; 8815065Swnj /* conversion to int (in i) handles seq wraparound */ 8825065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8835065Swnj if (i > 0) { 8844924Swnj if (i >= ti->ti_len) 8855065Swnj goto drop; 8865065Swnj m_adj(dtom(tp), i); 8875065Swnj ti->ti_len -= i; 8884924Swnj ti->ti_seq += i; 8894601Swnj } 8905065Swnj q = (struct tcpiphdr *)(q->ti_next); 8915065Swnj } 8924601Swnj 8935065Swnj /* 8945065Swnj * While we overlap succeeding segments trim them or, 8955065Swnj * if they are completely covered, dequeue them. 8965065Swnj */ 8975690Swnj while (q != (struct tcpiphdr *)tp) { 8985065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8995690Swnj if (i <= 0) 9005690Swnj break; 9015065Swnj if (i < q->ti_len) { 9025690Swnj q->ti_seq += i; 9035065Swnj q->ti_len -= i; 9045065Swnj m_adj(dtom(q), i); 9055065Swnj break; 9064601Swnj } 9075065Swnj q = (struct tcpiphdr *)q->ti_next; 9085623Swnj m = dtom(q->ti_prev); 9095065Swnj remque(q->ti_prev); 9105623Swnj m_freem(m); 9115065Swnj } 9124601Swnj 9135065Swnj /* 9145065Swnj * Stick new segment in its place. 9155065Swnj */ 9165065Swnj insque(ti, q->ti_prev); 9174601Swnj 9185065Swnj present: 9195065Swnj /* 9205244Sroot * Present data to user, advancing rcv_nxt through 9215244Sroot * completed sequence space. 9225065Swnj */ 9235263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 9245244Sroot return (0); 9254924Swnj ti = tp->seg_next; 9265263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 9275263Swnj return (0); 9285263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9295263Swnj return (0); 9305263Swnj do { 9315244Sroot tp->rcv_nxt += ti->ti_len; 9325244Sroot flags = ti->ti_flags & TH_FIN; 9334924Swnj remque(ti); 9345263Swnj m = dtom(ti); 9354924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9365263Swnj if (so->so_state & SS_CANTRCVMORE) 9376161Ssam m_freem(m); 9385263Swnj else 9395263Swnj sbappend(&so->so_rcv, m); 9405263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9415263Swnj sorwakeup(so); 9425065Swnj return (flags); 9435065Swnj drop: 9445065Swnj m_freem(dtom(ti)); 9455263Swnj return (0); 9464601Swnj } 947