1*6434Swnj /* tcp_input.c 1.66 82/04/01 */ 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" 106351Ssam #include "../net/route.h" 115085Swnj #include "../net/in_pcb.h" 125085Swnj #include "../net/in_systm.h" 135085Swnj #include "../net/if.h" 144803Swnj #include "../net/ip.h" 154899Swnj #include "../net/ip_var.h" 164803Swnj #include "../net/tcp.h" 174803Swnj #include "../net/tcp_fsm.h" 185085Swnj #include "../net/tcp_seq.h" 195085Swnj #include "../net/tcp_timer.h" 204803Swnj #include "../net/tcp_var.h" 215085Swnj #include "../net/tcpip.h" 225267Sroot #include "../net/tcp_debug.h" 235109Swnj #include "../errno.h" 244601Swnj 255300Sroot int tcpprintfs = 0; 264679Swnj int tcpcksum = 1; 275244Sroot struct sockaddr_in tcp_in = { AF_INET }; 285267Sroot struct tcpiphdr tcp_saveti; 295440Swnj extern tcpnodelack; 304601Swnj 315267Sroot struct tcpcb *tcp_newtcpcb(); 325065Swnj /* 335065Swnj * TCP input routine, follows pages 65-76 of the 345065Swnj * protocol specification dated September, 1981 very closely. 355065Swnj */ 364924Swnj tcp_input(m0) 374924Swnj struct mbuf *m0; 384601Swnj { 394924Swnj register struct tcpiphdr *ti; 404924Swnj struct inpcb *inp; 414924Swnj register struct mbuf *m; 425440Swnj struct mbuf *om = 0; 434924Swnj int len, tlen, off; 445391Swnj register struct tcpcb *tp = 0; 454924Swnj register int tiflags; 464803Swnj struct socket *so; 475109Swnj int todrop, acked; 485267Sroot short ostate; 496028Sroot struct in_addr laddr; 504924Swnj 514601Swnj COUNT(TCP_INPUT); 524924Swnj /* 535244Sroot * Get IP and TCP header together in first mbuf. 545244Sroot * Note: IP leaves IP header in first mbuf. 554924Swnj */ 564924Swnj m = m0; 575020Sroot ti = mtod(m, struct tcpiphdr *); 585244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 595208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 605307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 615307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 625085Swnj tcpstat.tcps_hdrops++; 635307Sroot return; 645085Swnj } 655085Swnj ti = mtod(m, struct tcpiphdr *); 665085Swnj } 674601Swnj 684601Swnj /* 695244Sroot * Checksum extended TCP header and data. 704601Swnj */ 714924Swnj tlen = ((struct ip *)ti)->ip_len; 724924Swnj len = sizeof (struct ip) + tlen; 734679Swnj if (tcpcksum) { 744924Swnj ti->ti_next = ti->ti_prev = 0; 754924Swnj ti->ti_x1 = 0; 765223Swnj ti->ti_len = (u_short)tlen; 776320Swnj #if vax || pdp11 786161Ssam ti->ti_len = htons((u_short)ti->ti_len); 795223Swnj #endif 805231Swnj if (ti->ti_sum = in_cksum(m, len)) { 814924Swnj tcpstat.tcps_badsum++; 826211Swnj if (tcpprintfs) 836211Swnj printf("tcp cksum %x\n", ti->ti_sum); 845085Swnj goto drop; 854601Swnj } 864601Swnj } 874601Swnj 884601Swnj /* 895244Sroot * Check that TCP offset makes sense, 905440Swnj * pull out TCP options and adjust length. 914601Swnj */ 924924Swnj off = ti->ti_off << 2; 935231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 944924Swnj tcpstat.tcps_badoff++; 955085Swnj goto drop; 964924Swnj } 976211Swnj tlen -= off; 986211Swnj ti->ti_len = tlen; 995440Swnj if (off > sizeof (struct tcphdr)) { 1005440Swnj if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 1015440Swnj tcpstat.tcps_hdrops++; 1025440Swnj goto drop; 1035440Swnj } 1045440Swnj ti = mtod(m, struct tcpiphdr *); 1055440Swnj om = m_get(M_DONTWAIT); 1065440Swnj if (om == 0) 1075440Swnj goto drop; 1085440Swnj om->m_off = MMINOFF; 1095440Swnj om->m_len = off - sizeof (struct tcphdr); 1105440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 1116161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 1125440Swnj m->m_len -= om->m_len; 1136161Ssam bcopy(op+om->m_len, op, 1146161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 1155440Swnj } 1165440Swnj } 1175065Swnj tiflags = ti->ti_flags; 1184924Swnj 1196093Sroot /* 1206211Swnj * Drop TCP and IP headers. 1216093Sroot */ 1226093Sroot off += sizeof (struct ip); 1236093Sroot m->m_off += off; 1246093Sroot m->m_len -= off; 1256093Sroot 1266320Swnj #if vax || pdp11 1274924Swnj /* 1285244Sroot * Convert TCP protocol specific fields to host format. 1295085Swnj */ 1305085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1315085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1325085Swnj ti->ti_win = ntohs(ti->ti_win); 1335085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1345231Swnj #endif 1355085Swnj 1365085Swnj /* 1375994Swnj * Locate pcb for segment. On match, update the local 1385994Swnj * address stored in the block to reflect anchoring. 1394924Swnj */ 1405065Swnj inp = in_pcblookup 1416028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 1426028Sroot INPLOOKUP_WILDCARD); 1435065Swnj 1445065Swnj /* 1455065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1465244Sroot * all data in the incoming segment is discarded. 1475065Swnj */ 1485300Sroot if (inp == 0) 1495085Swnj goto dropwithreset; 1505065Swnj tp = intotcpcb(inp); 1515300Sroot if (tp == 0) 1525085Swnj goto dropwithreset; 1535109Swnj so = inp->inp_socket; 1545267Sroot if (so->so_options & SO_DEBUG) { 1555267Sroot ostate = tp->t_state; 1565267Sroot tcp_saveti = *ti; 1575267Sroot } 1584601Swnj 1594601Swnj /* 1605162Swnj * Segment received on connection. 1615162Swnj * Reset idle time and keep-alive timer. 1625162Swnj */ 1635162Swnj tp->t_idle = 0; 1645162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1655162Swnj 1665162Swnj /* 1675440Swnj * Process options. 1685440Swnj */ 1695440Swnj if (om) { 1705440Swnj tcp_dooptions(tp, om); 1715440Swnj om = 0; 1725440Swnj } 1735440Swnj 1745440Swnj /* 1755085Swnj * Calculate amount of space in receive window, 1765085Swnj * and then do TCP input processing. 1774601Swnj */ 1785085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1795231Swnj if (tp->rcv_wnd < 0) 1805231Swnj tp->rcv_wnd = 0; 1814601Swnj 1824601Swnj switch (tp->t_state) { 1834601Swnj 1845065Swnj /* 1855065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1865065Swnj * If the segment contains an ACK then it is bad and send a RST. 1875065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1885085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1895065Swnj * tp->iss, and send a segment: 1905085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1915065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1925065Swnj * Fill in remote peer address fields if not previously specified. 1935065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1945244Sroot * segment in this state. 1955065Swnj */ 1965065Swnj case TCPS_LISTEN: 1975065Swnj if (tiflags & TH_RST) 1985065Swnj goto drop; 1995300Sroot if (tiflags & TH_ACK) 2005085Swnj goto dropwithreset; 2015300Sroot if ((tiflags & TH_SYN) == 0) 2025065Swnj goto drop; 2035244Sroot tcp_in.sin_addr = ti->ti_src; 2045244Sroot tcp_in.sin_port = ti->ti_sport; 2056028Sroot laddr = inp->inp_laddr; 2066028Sroot if (inp->inp_laddr.s_addr == 0) 2076028Sroot inp->inp_laddr = ti->ti_dst; 2086161Ssam if (in_pcbconnect(inp, (struct sockaddr_in *)&tcp_in)) { 2096028Sroot inp->inp_laddr = laddr; 2105244Sroot goto drop; 2116028Sroot } 2125244Sroot tp->t_template = tcp_template(tp); 2135244Sroot if (tp->t_template == 0) { 2145244Sroot in_pcbdisconnect(inp); 2156028Sroot inp->inp_laddr = laddr; 2166320Swnj tp = 0; 2175244Sroot goto drop; 2185244Sroot } 2196028Sroot in_setsockaddr(inp); 2205085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 2215065Swnj tp->irs = ti->ti_seq; 2225085Swnj tcp_sendseqinit(tp); 2235085Swnj tcp_rcvseqinit(tp); 2245065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2255244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 2265085Swnj goto trimthenstep6; 2274601Swnj 2285065Swnj /* 2295065Swnj * If the state is SYN_SENT: 2305065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2315065Swnj * if seg contains a RST, then drop the connection. 2325065Swnj * if seg does not contain SYN, then drop it. 2335065Swnj * Otherwise this is an acceptable SYN segment 2345065Swnj * initialize tp->rcv_nxt and tp->irs 2355065Swnj * if seg contains ack then advance tp->snd_una 2365065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2375065Swnj * arrange for segment to be acked (eventually) 2385065Swnj * continue processing rest of data/controls, beginning with URG 2395065Swnj */ 2405065Swnj case TCPS_SYN_SENT: 2415065Swnj if ((tiflags & TH_ACK) && 2425300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2435300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2445231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2455085Swnj goto dropwithreset; 2465065Swnj if (tiflags & TH_RST) { 2476320Swnj if (tiflags & TH_ACK) { 2485267Sroot tcp_drop(tp, ECONNREFUSED); 2496320Swnj tp = 0; 2506320Swnj } 2515065Swnj goto drop; 2524601Swnj } 2535065Swnj if ((tiflags & TH_SYN) == 0) 2545065Swnj goto drop; 2555231Swnj tp->snd_una = ti->ti_ack; 2565357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2575357Sroot tp->snd_nxt = tp->snd_una; 2585244Sroot tp->t_timer[TCPT_REXMT] = 0; 2595065Swnj tp->irs = ti->ti_seq; 2605085Swnj tcp_rcvseqinit(tp); 2615085Swnj tp->t_flags |= TF_ACKNOW; 2625162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2635391Swnj if (so->so_options & SO_ACCEPTCONN) 2645391Swnj so->so_state |= SS_CONNAWAITING; 2655244Sroot soisconnected(so); 2665065Swnj tp->t_state = TCPS_ESTABLISHED; 2675162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2685162Swnj } else 2695085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2705085Swnj goto trimthenstep6; 2715085Swnj 2725085Swnj trimthenstep6: 2735085Swnj /* 2745231Swnj * Advance ti->ti_seq to correspond to first data byte. 2755085Swnj * If data, trim to stay within window, 2765085Swnj * dropping FIN if necessary. 2775085Swnj */ 2785231Swnj ti->ti_seq++; 2795085Swnj if (ti->ti_len > tp->rcv_wnd) { 2805085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2815085Swnj m_adj(m, -todrop); 2825085Swnj ti->ti_len = tp->rcv_wnd; 2835085Swnj ti->ti_flags &= ~TH_FIN; 2845065Swnj } 2855263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2865085Swnj goto step6; 2875065Swnj } 2884601Swnj 2895065Swnj /* 2905065Swnj * States other than LISTEN or SYN_SENT. 2915065Swnj * First check that at least some bytes of segment are within 2925065Swnj * receive window. 2935065Swnj */ 2945065Swnj if (tp->rcv_wnd == 0) { 2955065Swnj /* 2965065Swnj * If window is closed can only take segments at 2975231Swnj * window edge, and have to drop data and PUSH from 2985065Swnj * incoming segments. 2995065Swnj */ 3005300Sroot if (tp->rcv_nxt != ti->ti_seq) 3015065Swnj goto dropafterack; 3025085Swnj if (ti->ti_len > 0) { 3035690Swnj m_adj(m, ti->ti_len); 3045085Swnj ti->ti_len = 0; 3055085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3065065Swnj } 3075065Swnj } else { 3085065Swnj /* 3095231Swnj * If segment begins before rcv_nxt, drop leading 3105065Swnj * data (and SYN); if nothing left, just ack. 3115065Swnj */ 3125690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 3135690Swnj if (todrop > 0) { 3145085Swnj if (tiflags & TH_SYN) { 3155300Sroot tiflags &= ~TH_SYN; 3165690Swnj ti->ti_flags &= ~TH_SYN; 3175085Swnj ti->ti_seq++; 3185085Swnj if (ti->ti_urp > 1) 3195085Swnj ti->ti_urp--; 3205085Swnj else 3215085Swnj tiflags &= ~TH_URG; 3225085Swnj todrop--; 3235085Swnj } 3246211Swnj if (todrop > ti->ti_len || 3256211Swnj todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 3265065Swnj goto dropafterack; 3275065Swnj m_adj(m, todrop); 3285065Swnj ti->ti_seq += todrop; 3295065Swnj ti->ti_len -= todrop; 3305085Swnj if (ti->ti_urp > todrop) 3315085Swnj ti->ti_urp -= todrop; 3325085Swnj else { 3335085Swnj tiflags &= ~TH_URG; 3345690Swnj ti->ti_flags &= ~TH_URG; 3355690Swnj ti->ti_urp = 0; 3365085Swnj } 3375065Swnj } 3385065Swnj /* 3395065Swnj * If segment ends after window, drop trailing data 3405085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3415065Swnj */ 3425690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3435690Swnj if (todrop > 0) { 3446211Swnj if (todrop >= ti->ti_len) 3455065Swnj goto dropafterack; 3465065Swnj m_adj(m, -todrop); 3475065Swnj ti->ti_len -= todrop; 3485085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3495065Swnj } 3505065Swnj } 3514601Swnj 3525065Swnj /* 3535951Swnj * If a segment is received on a connection after the 3545951Swnj * user processes are gone, then RST the other end. 3555951Swnj */ 3565951Swnj if (so->so_state & SS_USERGONE) { 3575951Swnj tcp_close(tp); 3586266Swnj tp = 0; 3595951Swnj goto dropwithreset; 3605951Swnj } 3615951Swnj 3625951Swnj /* 3635065Swnj * If the RST bit is set examine the state: 3645065Swnj * SYN_RECEIVED STATE: 3655065Swnj * If passive open, return to LISTEN state. 3665065Swnj * If active open, inform user that connection was refused. 3675065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3685065Swnj * Inform user that connection was reset, and close tcb. 3695065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3705065Swnj * Close the tcb. 3715065Swnj */ 3725065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3735267Sroot 3745065Swnj case TCPS_SYN_RECEIVED: 3755065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3765267Sroot /* a miniature tcp_close, but invisible to user */ 3775267Sroot (void) m_free(dtom(tp->t_template)); 3785267Sroot (void) m_free(dtom(tp)); 3795267Sroot inp->inp_ppcb = 0; 3805267Sroot tp = tcp_newtcpcb(inp); 3815085Swnj tp->t_state = TCPS_LISTEN; 3826028Sroot inp->inp_faddr.s_addr = 0; 3836028Sroot inp->inp_fport = 0; 3846028Sroot inp->inp_laddr.s_addr = 0; /* not quite right */ 3856320Swnj tp = 0; 3865065Swnj goto drop; 3874601Swnj } 3885085Swnj tcp_drop(tp, ECONNREFUSED); 3896320Swnj tp = 0; 3905065Swnj goto drop; 3914601Swnj 3925065Swnj case TCPS_ESTABLISHED: 3935065Swnj case TCPS_FIN_WAIT_1: 3945065Swnj case TCPS_FIN_WAIT_2: 3955065Swnj case TCPS_CLOSE_WAIT: 3965065Swnj tcp_drop(tp, ECONNRESET); 3976320Swnj tp = 0; 3985065Swnj goto drop; 3995065Swnj 4005065Swnj case TCPS_CLOSING: 4015065Swnj case TCPS_LAST_ACK: 4025065Swnj case TCPS_TIME_WAIT: 4035065Swnj tcp_close(tp); 4046320Swnj tp = 0; 4055065Swnj goto drop; 4064601Swnj } 4074601Swnj 4084601Swnj /* 4095065Swnj * If a SYN is in the window, then this is an 4105065Swnj * error and we send an RST and drop the connection. 4114601Swnj */ 4125065Swnj if (tiflags & TH_SYN) { 4135231Swnj tcp_drop(tp, ECONNRESET); 4146266Swnj tp = 0; 4155085Swnj goto dropwithreset; 4164601Swnj } 4174601Swnj 4184601Swnj /* 4195065Swnj * If the ACK bit is off we drop the segment and return. 4204601Swnj */ 4215085Swnj if ((tiflags & TH_ACK) == 0) 4225065Swnj goto drop; 4235065Swnj 4245065Swnj /* 4255065Swnj * Ack processing. 4265065Swnj */ 4274601Swnj switch (tp->t_state) { 4284601Swnj 4295065Swnj /* 4305065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4315065Swnj * ESTABLISHED state and continue processing, othewise 4325065Swnj * send an RST. 4335065Swnj */ 4345065Swnj case TCPS_SYN_RECEIVED: 4355085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4365231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4375085Swnj goto dropwithreset; 4385244Sroot tp->snd_una++; /* SYN acked */ 4395357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4405357Sroot tp->snd_nxt = tp->snd_una; 4415244Sroot tp->t_timer[TCPT_REXMT] = 0; 4425391Swnj if (so->so_options & SO_ACCEPTCONN) 4435391Swnj so->so_state |= SS_CONNAWAITING; 4445085Swnj soisconnected(so); 4455085Swnj tp->t_state = TCPS_ESTABLISHED; 4465162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4475244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4485085Swnj /* fall into ... */ 4494601Swnj 4505065Swnj /* 4515065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4525065Swnj * ACKs. If the ack is in the range 4535231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4545065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4555065Swnj * data from the retransmission queue. If this ACK reflects 4565065Swnj * more up to date window information we update our window information. 4575065Swnj */ 4585065Swnj case TCPS_ESTABLISHED: 4595065Swnj case TCPS_FIN_WAIT_1: 4605065Swnj case TCPS_FIN_WAIT_2: 4615065Swnj case TCPS_CLOSE_WAIT: 4625065Swnj case TCPS_CLOSING: 4635244Sroot case TCPS_LAST_ACK: 4645244Sroot case TCPS_TIME_WAIT: 4655085Swnj #define ourfinisacked (acked > 0) 4665085Swnj 4675244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4685065Swnj break; 4695300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4705065Swnj goto dropafterack; 4715085Swnj acked = ti->ti_ack - tp->snd_una; 4725951Swnj 4735951Swnj /* 4745951Swnj * If transmit timer is running and timed sequence 4755951Swnj * number was acked, update smoothed round trip time. 4765951Swnj */ 4775951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4785951Swnj if (tp->t_srtt == 0) 4795951Swnj tp->t_srtt = tp->t_rtt; 4805951Swnj else 4815951Swnj tp->t_srtt = 4825951Swnj tcp_alpha * tp->t_srtt + 4835951Swnj (1 - tcp_alpha) * tp->t_rtt; 4845951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */ 4855951Swnj tp->t_rtt = 0; 4865951Swnj } 4875951Swnj 4885307Sroot if (ti->ti_ack == tp->snd_max) 4895244Sroot tp->t_timer[TCPT_REXMT] = 0; 4905307Sroot else { 4915244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4925244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4935951Swnj tp->t_rtt = 1; 4945300Sroot tp->t_rxtshift = 0; 4955085Swnj } 4965307Sroot if (acked > so->so_snd.sb_cc) { 4975307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4985307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4995307Sroot } else { 5006161Ssam sbdrop(&so->so_snd, acked); 5015307Sroot tp->snd_wnd -= acked; 5025307Sroot acked = 0; 5035307Sroot } 504*6434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 5055300Sroot sowwakeup(so); 5065231Swnj tp->snd_una = ti->ti_ack; 5075357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 5085357Sroot tp->snd_nxt = tp->snd_una; 5095162Swnj 5104601Swnj switch (tp->t_state) { 5114601Swnj 5125065Swnj /* 5135065Swnj * In FIN_WAIT_1 STATE in addition to the processing 5145065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 5155085Swnj * then enter FIN_WAIT_2. 5165065Swnj */ 5175065Swnj case TCPS_FIN_WAIT_1: 5185896Swnj if (ourfinisacked) { 5195896Swnj /* 5205896Swnj * If we can't receive any more 5215896Swnj * data, then closing user can proceed. 5225896Swnj */ 5235896Swnj if (so->so_state & SS_CANTRCVMORE) 5245896Swnj soisdisconnected(so); 5255085Swnj tp->t_state = TCPS_FIN_WAIT_2; 5265896Swnj } 5274601Swnj break; 5284601Swnj 5295065Swnj /* 5305065Swnj * In CLOSING STATE in addition to the processing for 5315065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5325065Swnj * then enter the TIME-WAIT state, otherwise ignore 5335065Swnj * the segment. 5345065Swnj */ 5355065Swnj case TCPS_CLOSING: 5365244Sroot if (ourfinisacked) { 5375065Swnj tp->t_state = TCPS_TIME_WAIT; 5385244Sroot tcp_canceltimers(tp); 5395244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5405244Sroot soisdisconnected(so); 5415244Sroot } 5425244Sroot break; 5434601Swnj 5445065Swnj /* 5455085Swnj * The only thing that can arrive in LAST_ACK state 5465085Swnj * is an acknowledgment of our FIN. If our FIN is now 5475085Swnj * acknowledged, delete the TCB, enter the closed state 5485085Swnj * and return. 5495065Swnj */ 5505065Swnj case TCPS_LAST_ACK: 5516320Swnj if (ourfinisacked) { 5525065Swnj tcp_close(tp); 5536320Swnj tp = 0; 5546320Swnj } 5555065Swnj goto drop; 5564601Swnj 5575065Swnj /* 5585065Swnj * In TIME_WAIT state the only thing that should arrive 5595065Swnj * is a retransmission of the remote FIN. Acknowledge 5605065Swnj * it and restart the finack timer. 5615065Swnj */ 5625065Swnj case TCPS_TIME_WAIT: 5635162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5645065Swnj goto dropafterack; 5654601Swnj } 5665085Swnj #undef ourfinisacked 5675085Swnj } 5684601Swnj 5695065Swnj step6: 5705065Swnj /* 5715244Sroot * Update window information. 5725244Sroot */ 5735300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5745391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5755300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5765244Sroot tp->snd_wnd = ti->ti_win; 5775244Sroot tp->snd_wl1 = ti->ti_seq; 5785244Sroot tp->snd_wl2 = ti->ti_ack; 5795244Sroot if (tp->snd_wnd > 0) 5805244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5815244Sroot } 5825244Sroot 5835244Sroot /* 5845547Swnj * Process segments with URG. 5855065Swnj */ 5865547Swnj if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5875547Swnj /* 5885547Swnj * If this segment advances the known urgent pointer, 5895547Swnj * then mark the data stream. This should not happen 5905547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5915547Swnj * a FIN has been received from the remote side. 5925547Swnj * In these states we ignore the URG. 5935547Swnj */ 5945547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5955547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 5965547Swnj so->so_oobmark = so->so_rcv.sb_cc + 5975547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 5985547Swnj if (so->so_oobmark == 0) 5995547Swnj so->so_state |= SS_RCVATMARK; 6005440Swnj #ifdef TCPTRUEOOB 6015547Swnj if ((tp->t_flags & TF_DOOOB) == 0) 6025440Swnj #endif 6035547Swnj sohasoutofband(so); 6045547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 6055440Swnj } 6065547Swnj /* 6075547Swnj * Remove out of band data so doesn't get presented to user. 6085547Swnj * This can happen independent of advancing the URG pointer, 6095547Swnj * but if two URG's are pending at once, some out-of-band 6105547Swnj * data may creep in... ick. 6115547Swnj */ 6125547Swnj if (ti->ti_urp <= ti->ti_len) { 6135547Swnj tcp_pulloutofband(so, ti); 6145547Swnj } 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 } 7255231Swnj return; 7265085Swnj 7275065Swnj drop: 7285085Swnj /* 7295085Swnj * Drop space held by incoming segment and return. 7305085Swnj */ 7316303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 7326303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 7335065Swnj m_freem(m); 7345267Sroot return; 7355065Swnj } 7365065Swnj 7375440Swnj tcp_dooptions(tp, om) 7385440Swnj struct tcpcb *tp; 7395440Swnj struct mbuf *om; 7405419Swnj { 7415440Swnj register u_char *cp; 7425440Swnj int opt, optlen, cnt; 7435419Swnj 7445440Swnj cp = mtod(om, u_char *); 7455440Swnj cnt = om->m_len; 7465440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7475440Swnj opt = cp[0]; 7485440Swnj if (opt == TCPOPT_EOL) 7495440Swnj break; 7505440Swnj if (opt == TCPOPT_NOP) 7515440Swnj optlen = 1; 7525440Swnj else 7535440Swnj optlen = cp[1]; 7545440Swnj switch (opt) { 7555440Swnj 7565440Swnj default: 7575440Swnj break; 7585440Swnj 7595440Swnj case TCPOPT_MAXSEG: 7605440Swnj if (optlen != 4) 7615440Swnj continue; 7625440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7636320Swnj #if vax || pdp11 7646161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7655440Swnj #endif 7665440Swnj break; 7675440Swnj 7685440Swnj #ifdef TCPTRUEOOB 7695440Swnj case TCPOPT_WILLOOB: 7705440Swnj tp->t_flags |= TF_DOOOB; 7715440Swnj printf("tp %x dooob\n", tp); 7725440Swnj break; 7735440Swnj 7745440Swnj case TCPOPT_OOBDATA: { 7755440Swnj int seq; 7765547Swnj register struct socket *so = tp->t_inpcb->inp_socket; 7775547Swnj tcp_seq mark; 7785440Swnj 7795547Swnj if (optlen != 8) 7805440Swnj continue; 7815440Swnj seq = cp[2]; 7825440Swnj if (seq < tp->t_iobseq) 7835440Swnj seq += 256; 7845440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 7855440Swnj if (seq - tp->t_iobseq > 128) { 7865440Swnj printf("bad seq\n"); 7875440Swnj tp->t_oobflags |= TCPOOB_OWEACK; 7885440Swnj break; 7895440Swnj } 7905440Swnj tp->t_iobseq = cp[2]; 7915440Swnj tp->t_iobc = cp[3]; 7925547Swnj mark = *(tcp_seq *)(cp + 4); 7936320Swnj #if vax || pdp11 7945547Swnj mark = ntohl(mark); 7955547Swnj #endif 7965547Swnj so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 7975547Swnj if (so->so_oobmark == 0) 7985547Swnj so->so_state |= SS_RCVATMARK; 7995440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 8005547Swnj sohasoutofband(so); 8015440Swnj break; 8025419Swnj } 8035440Swnj 8045440Swnj case TCPOPT_OOBACK: { 8055440Swnj int seq; 8065440Swnj 8075440Swnj if (optlen != 4) 8085440Swnj continue; 8095440Swnj if (tp->t_oobseq != cp[2]) { 8105440Swnj printf("wrong ack\n"); 8115440Swnj break; 8125440Swnj } 8135440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]); 8145440Swnj tp->t_oobflags &= ~TCPOOB_NEEDACK; 8155440Swnj tp->t_timer[TCPT_OOBREXMT] = 0; 8165419Swnj break; 8175440Swnj } 8185440Swnj #endif TCPTRUEOOB 8195440Swnj } 8205419Swnj } 8216161Ssam (void) m_free(om); 8225419Swnj } 8235419Swnj 8245419Swnj /* 8255547Swnj * Pull out of band byte out of a segment so 8265547Swnj * it doesn't appear in the user's data queue. 8275547Swnj * It is still reflected in the segment length for 8285547Swnj * sequencing purposes. 8295547Swnj */ 8305547Swnj tcp_pulloutofband(so, ti) 8315547Swnj struct socket *so; 8325547Swnj struct tcpiphdr *ti; 8335547Swnj { 8345547Swnj register struct mbuf *m; 8356116Swnj int cnt = ti->ti_urp - 1; 8365547Swnj 8375547Swnj m = dtom(ti); 8385547Swnj while (cnt >= 0) { 8395547Swnj if (m->m_len > cnt) { 8405547Swnj char *cp = mtod(m, caddr_t) + cnt; 8415547Swnj struct tcpcb *tp = sototcpcb(so); 8425547Swnj 8435547Swnj tp->t_iobc = *cp; 8445547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 8456161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 8465547Swnj m->m_len--; 8475547Swnj return; 8485547Swnj } 8495547Swnj cnt -= m->m_len; 8505547Swnj m = m->m_next; 8515547Swnj if (m == 0) 8525547Swnj break; 8535547Swnj } 8545547Swnj panic("tcp_pulloutofband"); 8555547Swnj } 8565547Swnj 8575547Swnj /* 8585065Swnj * Insert segment ti into reassembly queue of tcp with 8595065Swnj * control block tp. Return TH_FIN if reassembly now includes 8605065Swnj * a segment with FIN. 8615065Swnj */ 8625109Swnj tcp_reass(tp, ti) 8635065Swnj register struct tcpcb *tp; 8645065Swnj register struct tcpiphdr *ti; 8655065Swnj { 8665065Swnj register struct tcpiphdr *q; 8675085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8685263Swnj struct mbuf *m; 8695263Swnj int flags; 8705085Swnj COUNT(TCP_REASS); 8715065Swnj 8725065Swnj /* 8735162Swnj * Call with ti==0 after become established to 8745162Swnj * force pre-ESTABLISHED data up to user socket. 8755065Swnj */ 8765162Swnj if (ti == 0) 8775065Swnj goto present; 8784601Swnj 8795065Swnj /* 8805065Swnj * Find a segment which begins after this one does. 8815065Swnj */ 8825065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8835065Swnj q = (struct tcpiphdr *)q->ti_next) 8845065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8855065Swnj break; 8864601Swnj 8875065Swnj /* 8885065Swnj * If there is a preceding segment, it may provide some of 8895065Swnj * our data already. If so, drop the data from the incoming 8905065Swnj * segment. If it provides all of our data, drop us. 8915065Swnj */ 8925065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8935065Swnj register int i; 8945690Swnj q = (struct tcpiphdr *)q->ti_prev; 8955065Swnj /* conversion to int (in i) handles seq wraparound */ 8965065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8975065Swnj if (i > 0) { 8984924Swnj if (i >= ti->ti_len) 8995065Swnj goto drop; 9005065Swnj m_adj(dtom(tp), i); 9015065Swnj ti->ti_len -= i; 9024924Swnj ti->ti_seq += i; 9034601Swnj } 9045065Swnj q = (struct tcpiphdr *)(q->ti_next); 9055065Swnj } 9064601Swnj 9075065Swnj /* 9085065Swnj * While we overlap succeeding segments trim them or, 9095065Swnj * if they are completely covered, dequeue them. 9105065Swnj */ 9115690Swnj while (q != (struct tcpiphdr *)tp) { 9125065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 9135690Swnj if (i <= 0) 9145690Swnj break; 9155065Swnj if (i < q->ti_len) { 9165690Swnj q->ti_seq += i; 9175065Swnj q->ti_len -= i; 9185065Swnj m_adj(dtom(q), i); 9195065Swnj break; 9204601Swnj } 9215065Swnj q = (struct tcpiphdr *)q->ti_next; 9225623Swnj m = dtom(q->ti_prev); 9235065Swnj remque(q->ti_prev); 9245623Swnj m_freem(m); 9255065Swnj } 9264601Swnj 9275065Swnj /* 9285065Swnj * Stick new segment in its place. 9295065Swnj */ 9305065Swnj insque(ti, q->ti_prev); 9314601Swnj 9325065Swnj present: 9335065Swnj /* 9345244Sroot * Present data to user, advancing rcv_nxt through 9355244Sroot * completed sequence space. 9365065Swnj */ 9375263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 9385244Sroot return (0); 9394924Swnj ti = tp->seg_next; 9405263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 9415263Swnj return (0); 9425263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9435263Swnj return (0); 9445263Swnj do { 9455244Sroot tp->rcv_nxt += ti->ti_len; 9465244Sroot flags = ti->ti_flags & TH_FIN; 9474924Swnj remque(ti); 9485263Swnj m = dtom(ti); 9494924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9505263Swnj if (so->so_state & SS_CANTRCVMORE) 9516161Ssam m_freem(m); 9525263Swnj else 9535263Swnj sbappend(&so->so_rcv, m); 9545263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9555263Swnj sorwakeup(so); 9565065Swnj return (flags); 9575065Swnj drop: 9585065Swnj m_freem(dtom(ti)); 9595263Swnj return (0); 9604601Swnj } 961