1*6211Swnj /* tcp_input.c 1.61 82/03/15 */ 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++; 81*6211Swnj if (tcpprintfs) 82*6211Swnj 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 } 96*6211Swnj tlen -= off; 97*6211Swnj 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 /* 119*6211Swnj * 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 } 320*6211Swnj if (todrop > ti->ti_len || 321*6211Swnj 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) { 340*6211Swnj 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); 3545951Swnj goto dropwithreset; 3555951Swnj } 3565951Swnj 3575951Swnj /* 3585065Swnj * If the RST bit is set examine the state: 3595065Swnj * SYN_RECEIVED STATE: 3605065Swnj * If passive open, return to LISTEN state. 3615065Swnj * If active open, inform user that connection was refused. 3625065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3635065Swnj * Inform user that connection was reset, and close tcb. 3645065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3655065Swnj * Close the tcb. 3665065Swnj */ 3675065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3685267Sroot 3695065Swnj case TCPS_SYN_RECEIVED: 3705065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3715267Sroot /* a miniature tcp_close, but invisible to user */ 3725267Sroot (void) m_free(dtom(tp->t_template)); 3735267Sroot (void) m_free(dtom(tp)); 3745267Sroot inp->inp_ppcb = 0; 3755267Sroot tp = tcp_newtcpcb(inp); 3765085Swnj tp->t_state = TCPS_LISTEN; 3776028Sroot inp->inp_faddr.s_addr = 0; 3786028Sroot inp->inp_fport = 0; 3796028Sroot inp->inp_laddr.s_addr = 0; /* not quite right */ 3805065Swnj goto drop; 3814601Swnj } 3825085Swnj tcp_drop(tp, ECONNREFUSED); 3835065Swnj goto drop; 3844601Swnj 3855065Swnj case TCPS_ESTABLISHED: 3865065Swnj case TCPS_FIN_WAIT_1: 3875065Swnj case TCPS_FIN_WAIT_2: 3885065Swnj case TCPS_CLOSE_WAIT: 3895065Swnj tcp_drop(tp, ECONNRESET); 3905065Swnj goto drop; 3915065Swnj 3925065Swnj case TCPS_CLOSING: 3935065Swnj case TCPS_LAST_ACK: 3945065Swnj case TCPS_TIME_WAIT: 3955065Swnj tcp_close(tp); 3965065Swnj goto drop; 3974601Swnj } 3984601Swnj 3994601Swnj /* 4005065Swnj * If a SYN is in the window, then this is an 4015065Swnj * error and we send an RST and drop the connection. 4024601Swnj */ 4035065Swnj if (tiflags & TH_SYN) { 4045231Swnj tcp_drop(tp, ECONNRESET); 4055085Swnj goto dropwithreset; 4064601Swnj } 4074601Swnj 4084601Swnj /* 4095065Swnj * If the ACK bit is off we drop the segment and return. 4104601Swnj */ 4115085Swnj if ((tiflags & TH_ACK) == 0) 4125065Swnj goto drop; 4135065Swnj 4145065Swnj /* 4155065Swnj * Ack processing. 4165065Swnj */ 4174601Swnj switch (tp->t_state) { 4184601Swnj 4195065Swnj /* 4205065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4215065Swnj * ESTABLISHED state and continue processing, othewise 4225065Swnj * send an RST. 4235065Swnj */ 4245065Swnj case TCPS_SYN_RECEIVED: 4255085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4265231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4275085Swnj goto dropwithreset; 4285244Sroot tp->snd_una++; /* SYN acked */ 4295357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4305357Sroot tp->snd_nxt = tp->snd_una; 4315244Sroot tp->t_timer[TCPT_REXMT] = 0; 4325391Swnj if (so->so_options & SO_ACCEPTCONN) 4335391Swnj so->so_state |= SS_CONNAWAITING; 4345085Swnj soisconnected(so); 4355085Swnj tp->t_state = TCPS_ESTABLISHED; 4365162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4375244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4385085Swnj /* fall into ... */ 4394601Swnj 4405065Swnj /* 4415065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4425065Swnj * ACKs. If the ack is in the range 4435231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4445065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4455065Swnj * data from the retransmission queue. If this ACK reflects 4465065Swnj * more up to date window information we update our window information. 4475065Swnj */ 4485065Swnj case TCPS_ESTABLISHED: 4495065Swnj case TCPS_FIN_WAIT_1: 4505065Swnj case TCPS_FIN_WAIT_2: 4515065Swnj case TCPS_CLOSE_WAIT: 4525065Swnj case TCPS_CLOSING: 4535244Sroot case TCPS_LAST_ACK: 4545244Sroot case TCPS_TIME_WAIT: 4555085Swnj #define ourfinisacked (acked > 0) 4565085Swnj 4575244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4585065Swnj break; 4595300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4605065Swnj goto dropafterack; 4615085Swnj acked = ti->ti_ack - tp->snd_una; 4625951Swnj 4635951Swnj /* 4645951Swnj * If transmit timer is running and timed sequence 4655951Swnj * number was acked, update smoothed round trip time. 4665951Swnj */ 4675951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4685951Swnj if (tp->t_srtt == 0) 4695951Swnj tp->t_srtt = tp->t_rtt; 4705951Swnj else 4715951Swnj tp->t_srtt = 4725951Swnj tcp_alpha * tp->t_srtt + 4735951Swnj (1 - tcp_alpha) * tp->t_rtt; 4745951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */ 4755951Swnj tp->t_rtt = 0; 4765951Swnj } 4775951Swnj 4785307Sroot if (ti->ti_ack == tp->snd_max) 4795244Sroot tp->t_timer[TCPT_REXMT] = 0; 4805307Sroot else { 4815244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4825244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4835951Swnj tp->t_rtt = 1; 4845300Sroot tp->t_rxtshift = 0; 4855085Swnj } 4865307Sroot if (acked > so->so_snd.sb_cc) { 4875307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4885307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4895307Sroot } else { 4906161Ssam sbdrop(&so->so_snd, acked); 4915307Sroot tp->snd_wnd -= acked; 4925307Sroot acked = 0; 4935307Sroot } 4945300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4955300Sroot sowwakeup(so); 4965231Swnj tp->snd_una = ti->ti_ack; 4975357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4985357Sroot tp->snd_nxt = tp->snd_una; 4995162Swnj 5004601Swnj switch (tp->t_state) { 5014601Swnj 5025065Swnj /* 5035065Swnj * In FIN_WAIT_1 STATE in addition to the processing 5045065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 5055085Swnj * then enter FIN_WAIT_2. 5065065Swnj */ 5075065Swnj case TCPS_FIN_WAIT_1: 5085896Swnj if (ourfinisacked) { 5095896Swnj /* 5105896Swnj * If we can't receive any more 5115896Swnj * data, then closing user can proceed. 5125896Swnj */ 5135896Swnj if (so->so_state & SS_CANTRCVMORE) 5145896Swnj soisdisconnected(so); 5155085Swnj tp->t_state = TCPS_FIN_WAIT_2; 5165896Swnj } 5174601Swnj break; 5184601Swnj 5195065Swnj /* 5205065Swnj * In CLOSING STATE in addition to the processing for 5215065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5225065Swnj * then enter the TIME-WAIT state, otherwise ignore 5235065Swnj * the segment. 5245065Swnj */ 5255065Swnj case TCPS_CLOSING: 5265244Sroot if (ourfinisacked) { 5275065Swnj tp->t_state = TCPS_TIME_WAIT; 5285244Sroot tcp_canceltimers(tp); 5295244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5305244Sroot soisdisconnected(so); 5315244Sroot } 5325244Sroot break; 5334601Swnj 5345065Swnj /* 5355085Swnj * The only thing that can arrive in LAST_ACK state 5365085Swnj * is an acknowledgment of our FIN. If our FIN is now 5375085Swnj * acknowledged, delete the TCB, enter the closed state 5385085Swnj * and return. 5395065Swnj */ 5405065Swnj case TCPS_LAST_ACK: 5415251Sroot if (ourfinisacked) 5425065Swnj tcp_close(tp); 5435065Swnj goto drop; 5444601Swnj 5455065Swnj /* 5465065Swnj * In TIME_WAIT state the only thing that should arrive 5475065Swnj * is a retransmission of the remote FIN. Acknowledge 5485065Swnj * it and restart the finack timer. 5495065Swnj */ 5505065Swnj case TCPS_TIME_WAIT: 5515162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5525065Swnj goto dropafterack; 5534601Swnj } 5545085Swnj #undef ourfinisacked 5555085Swnj } 5564601Swnj 5575065Swnj step6: 5585065Swnj /* 5595244Sroot * Update window information. 5605244Sroot */ 5615300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5625391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5635300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5645244Sroot tp->snd_wnd = ti->ti_win; 5655244Sroot tp->snd_wl1 = ti->ti_seq; 5665244Sroot tp->snd_wl2 = ti->ti_ack; 5675244Sroot if (tp->snd_wnd > 0) 5685244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5695244Sroot } 5705244Sroot 5715244Sroot /* 5725547Swnj * Process segments with URG. 5735065Swnj */ 5745547Swnj if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5755547Swnj /* 5765547Swnj * If this segment advances the known urgent pointer, 5775547Swnj * then mark the data stream. This should not happen 5785547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5795547Swnj * a FIN has been received from the remote side. 5805547Swnj * In these states we ignore the URG. 5815547Swnj */ 5825547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5835547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 5845547Swnj so->so_oobmark = so->so_rcv.sb_cc + 5855547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 5865547Swnj if (so->so_oobmark == 0) 5875547Swnj so->so_state |= SS_RCVATMARK; 5885440Swnj #ifdef TCPTRUEOOB 5895547Swnj if ((tp->t_flags & TF_DOOOB) == 0) 5905440Swnj #endif 5915547Swnj sohasoutofband(so); 5925547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 5935440Swnj } 5945547Swnj /* 5955547Swnj * Remove out of band data so doesn't get presented to user. 5965547Swnj * This can happen independent of advancing the URG pointer, 5975547Swnj * but if two URG's are pending at once, some out-of-band 5985547Swnj * data may creep in... ick. 5995547Swnj */ 6005547Swnj if (ti->ti_urp <= ti->ti_len) { 6015547Swnj tcp_pulloutofband(so, ti); 6025547Swnj } 6035419Swnj } 6044601Swnj 6054601Swnj /* 6065065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6075065Swnj * and arranging for acknowledgment of receipt if necessary. 6085065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6095065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6105065Swnj * case PRU_RCVD). If a FIN has already been received on this 6115065Swnj * connection then we just ignore the text. 6124601Swnj */ 6135263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6145263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6155065Swnj tiflags = tcp_reass(tp, ti); 6165440Swnj if (tcpnodelack == 0) 6175440Swnj tp->t_flags |= TF_DELACK; 6185440Swnj else 6195440Swnj tp->t_flags |= TF_ACKNOW; 6205244Sroot } else { 6214924Swnj m_freem(m); 6225263Swnj tiflags &= ~TH_FIN; 6235244Sroot } 6244601Swnj 6254601Swnj /* 6265263Swnj * If FIN is received ACK the FIN and let the user know 6275263Swnj * that the connection is closing. 6284601Swnj */ 6295263Swnj if (tiflags & TH_FIN) { 6305244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6315244Sroot socantrcvmore(so); 6325244Sroot tp->t_flags |= TF_ACKNOW; 6335244Sroot tp->rcv_nxt++; 6345244Sroot } 6355065Swnj switch (tp->t_state) { 6364601Swnj 6375065Swnj /* 6385065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6395065Swnj * enter the CLOSE_WAIT state. 6404884Swnj */ 6415065Swnj case TCPS_SYN_RECEIVED: 6425065Swnj case TCPS_ESTABLISHED: 6435065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6445065Swnj break; 6454884Swnj 6465065Swnj /* 6475085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6485085Swnj * enter the CLOSING state. 6494884Swnj */ 6505065Swnj case TCPS_FIN_WAIT_1: 6515085Swnj tp->t_state = TCPS_CLOSING; 6525065Swnj break; 6534601Swnj 6545065Swnj /* 6555065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6565065Swnj * starting the time-wait timer, turning off the other 6575065Swnj * standard timers. 6585065Swnj */ 6595065Swnj case TCPS_FIN_WAIT_2: 6605244Sroot tp->t_state = TCPS_TIME_WAIT; 6615074Swnj tcp_canceltimers(tp); 6625162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6635244Sroot soisdisconnected(so); 6645065Swnj break; 6655065Swnj 6664884Swnj /* 6675065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6684884Swnj */ 6695065Swnj case TCPS_TIME_WAIT: 6705162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6715065Swnj break; 6725085Swnj } 6734601Swnj } 6745267Sroot if (so->so_options & SO_DEBUG) 6755267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6765085Swnj 6775085Swnj /* 6785085Swnj * Return any desired output. 6795085Swnj */ 6806161Ssam (void) tcp_output(tp); 6815065Swnj return; 6825085Swnj 6835065Swnj dropafterack: 6845085Swnj /* 685*6211Swnj * Generate an ACK dropping incoming segment if it occupies 686*6211Swnj * sequence space, where the ACK reflects our state. 6875085Swnj */ 688*6211Swnj if ((tiflags&TH_RST) || 689*6211Swnj tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 6905085Swnj goto drop; 6915391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6925231Swnj return; 6935085Swnj 6945085Swnj dropwithreset: 6955440Swnj if (om) 6966161Ssam (void) m_free(om); 6975085Swnj /* 6985244Sroot * Generate a RST, dropping incoming segment. 6995085Swnj * Make ACK acceptable to originator of segment. 7005085Swnj */ 7015085Swnj if (tiflags & TH_RST) 7025085Swnj goto drop; 7035085Swnj if (tiflags & TH_ACK) 7045391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7055085Swnj else { 7065085Swnj if (tiflags & TH_SYN) 7075085Swnj ti->ti_len++; 708*6211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 709*6211Swnj TH_RST|TH_ACK); 7105085Swnj } 7115231Swnj return; 7125085Swnj 7135065Swnj drop: 7145085Swnj /* 7155085Swnj * Drop space held by incoming segment and return. 7165085Swnj */ 7175065Swnj m_freem(m); 7185267Sroot return; 7195065Swnj } 7205065Swnj 7215440Swnj tcp_dooptions(tp, om) 7225440Swnj struct tcpcb *tp; 7235440Swnj struct mbuf *om; 7245419Swnj { 7255440Swnj register u_char *cp; 7265440Swnj int opt, optlen, cnt; 7275419Swnj 7285440Swnj cp = mtod(om, u_char *); 7295440Swnj cnt = om->m_len; 7305440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7315440Swnj opt = cp[0]; 7325440Swnj if (opt == TCPOPT_EOL) 7335440Swnj break; 7345440Swnj if (opt == TCPOPT_NOP) 7355440Swnj optlen = 1; 7365440Swnj else 7375440Swnj optlen = cp[1]; 7385440Swnj switch (opt) { 7395440Swnj 7405440Swnj default: 7415440Swnj break; 7425440Swnj 7435440Swnj case TCPOPT_MAXSEG: 7445440Swnj if (optlen != 4) 7455440Swnj continue; 7465440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7475440Swnj #if vax 7486161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7495440Swnj #endif 7505440Swnj break; 7515440Swnj 7525440Swnj #ifdef TCPTRUEOOB 7535440Swnj case TCPOPT_WILLOOB: 7545440Swnj tp->t_flags |= TF_DOOOB; 7555440Swnj printf("tp %x dooob\n", tp); 7565440Swnj break; 7575440Swnj 7585440Swnj case TCPOPT_OOBDATA: { 7595440Swnj int seq; 7605547Swnj register struct socket *so = tp->t_inpcb->inp_socket; 7615547Swnj tcp_seq mark; 7625440Swnj 7635547Swnj if (optlen != 8) 7645440Swnj continue; 7655440Swnj seq = cp[2]; 7665440Swnj if (seq < tp->t_iobseq) 7675440Swnj seq += 256; 7685440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 7695440Swnj if (seq - tp->t_iobseq > 128) { 7705440Swnj printf("bad seq\n"); 7715440Swnj tp->t_oobflags |= TCPOOB_OWEACK; 7725440Swnj break; 7735440Swnj } 7745440Swnj tp->t_iobseq = cp[2]; 7755440Swnj tp->t_iobc = cp[3]; 7765547Swnj mark = *(tcp_seq *)(cp + 4); 7775547Swnj #if vax 7785547Swnj mark = ntohl(mark); 7795547Swnj #endif 7805547Swnj so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 7815547Swnj if (so->so_oobmark == 0) 7825547Swnj so->so_state |= SS_RCVATMARK; 7835440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 7845547Swnj sohasoutofband(so); 7855440Swnj break; 7865419Swnj } 7875440Swnj 7885440Swnj case TCPOPT_OOBACK: { 7895440Swnj int seq; 7905440Swnj 7915440Swnj if (optlen != 4) 7925440Swnj continue; 7935440Swnj if (tp->t_oobseq != cp[2]) { 7945440Swnj printf("wrong ack\n"); 7955440Swnj break; 7965440Swnj } 7975440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]); 7985440Swnj tp->t_oobflags &= ~TCPOOB_NEEDACK; 7995440Swnj tp->t_timer[TCPT_OOBREXMT] = 0; 8005419Swnj break; 8015440Swnj } 8025440Swnj #endif TCPTRUEOOB 8035440Swnj } 8045419Swnj } 8056161Ssam (void) m_free(om); 8065419Swnj } 8075419Swnj 8085419Swnj /* 8095547Swnj * Pull out of band byte out of a segment so 8105547Swnj * it doesn't appear in the user's data queue. 8115547Swnj * It is still reflected in the segment length for 8125547Swnj * sequencing purposes. 8135547Swnj */ 8145547Swnj tcp_pulloutofband(so, ti) 8155547Swnj struct socket *so; 8165547Swnj struct tcpiphdr *ti; 8175547Swnj { 8185547Swnj register struct mbuf *m; 8196116Swnj int cnt = ti->ti_urp - 1; 8205547Swnj 8215547Swnj m = dtom(ti); 8225547Swnj while (cnt >= 0) { 8235547Swnj if (m->m_len > cnt) { 8245547Swnj char *cp = mtod(m, caddr_t) + cnt; 8255547Swnj struct tcpcb *tp = sototcpcb(so); 8265547Swnj 8275547Swnj tp->t_iobc = *cp; 8285547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 8296161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 8305547Swnj m->m_len--; 8315547Swnj return; 8325547Swnj } 8335547Swnj cnt -= m->m_len; 8345547Swnj m = m->m_next; 8355547Swnj if (m == 0) 8365547Swnj break; 8375547Swnj } 8385547Swnj panic("tcp_pulloutofband"); 8395547Swnj } 8405547Swnj 8415547Swnj /* 8425065Swnj * Insert segment ti into reassembly queue of tcp with 8435065Swnj * control block tp. Return TH_FIN if reassembly now includes 8445065Swnj * a segment with FIN. 8455065Swnj */ 8465109Swnj tcp_reass(tp, ti) 8475065Swnj register struct tcpcb *tp; 8485065Swnj register struct tcpiphdr *ti; 8495065Swnj { 8505065Swnj register struct tcpiphdr *q; 8515085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8525263Swnj struct mbuf *m; 8535263Swnj int flags; 8545085Swnj COUNT(TCP_REASS); 8555065Swnj 8565065Swnj /* 8575162Swnj * Call with ti==0 after become established to 8585162Swnj * force pre-ESTABLISHED data up to user socket. 8595065Swnj */ 8605162Swnj if (ti == 0) 8615065Swnj goto present; 8624601Swnj 8635065Swnj /* 8645065Swnj * Find a segment which begins after this one does. 8655065Swnj */ 8665065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8675065Swnj q = (struct tcpiphdr *)q->ti_next) 8685065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8695065Swnj break; 8704601Swnj 8715065Swnj /* 8725065Swnj * If there is a preceding segment, it may provide some of 8735065Swnj * our data already. If so, drop the data from the incoming 8745065Swnj * segment. If it provides all of our data, drop us. 8755065Swnj */ 8765065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8775065Swnj register int i; 8785690Swnj q = (struct tcpiphdr *)q->ti_prev; 8795065Swnj /* conversion to int (in i) handles seq wraparound */ 8805065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8815065Swnj if (i > 0) { 8824924Swnj if (i >= ti->ti_len) 8835065Swnj goto drop; 8845065Swnj m_adj(dtom(tp), i); 8855065Swnj ti->ti_len -= i; 8864924Swnj ti->ti_seq += i; 8874601Swnj } 8885065Swnj q = (struct tcpiphdr *)(q->ti_next); 8895065Swnj } 8904601Swnj 8915065Swnj /* 8925065Swnj * While we overlap succeeding segments trim them or, 8935065Swnj * if they are completely covered, dequeue them. 8945065Swnj */ 8955690Swnj while (q != (struct tcpiphdr *)tp) { 8965065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8975690Swnj if (i <= 0) 8985690Swnj break; 8995065Swnj if (i < q->ti_len) { 9005690Swnj q->ti_seq += i; 9015065Swnj q->ti_len -= i; 9025065Swnj m_adj(dtom(q), i); 9035065Swnj break; 9044601Swnj } 9055065Swnj q = (struct tcpiphdr *)q->ti_next; 9065623Swnj m = dtom(q->ti_prev); 9075065Swnj remque(q->ti_prev); 9085623Swnj m_freem(m); 9095065Swnj } 9104601Swnj 9115065Swnj /* 9125065Swnj * Stick new segment in its place. 9135065Swnj */ 9145065Swnj insque(ti, q->ti_prev); 9154601Swnj 9165065Swnj present: 9175065Swnj /* 9185244Sroot * Present data to user, advancing rcv_nxt through 9195244Sroot * completed sequence space. 9205065Swnj */ 9215263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 9225244Sroot return (0); 9234924Swnj ti = tp->seg_next; 9245263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 9255263Swnj return (0); 9265263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9275263Swnj return (0); 9285263Swnj do { 9295244Sroot tp->rcv_nxt += ti->ti_len; 9305244Sroot flags = ti->ti_flags & TH_FIN; 9314924Swnj remque(ti); 9325263Swnj m = dtom(ti); 9334924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9345263Swnj if (so->so_state & SS_CANTRCVMORE) 9356161Ssam m_freem(m); 9365263Swnj else 9375263Swnj sbappend(&so->so_rcv, m); 9385263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9395263Swnj sorwakeup(so); 9405065Swnj return (flags); 9415065Swnj drop: 9425065Swnj m_freem(dtom(ti)); 9435263Swnj return (0); 9444601Swnj } 945