1*6161Ssam /* tcp_input.c 1.60 82/03/13 */ 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 24*6161Ssam #ifdef notdef 255300Sroot int tcpprintfs = 0; 26*6161Ssam #endif 274679Swnj int tcpcksum = 1; 285244Sroot struct sockaddr_in tcp_in = { AF_INET }; 295267Sroot struct tcpiphdr tcp_saveti; 305440Swnj extern tcpnodelack; 314601Swnj 325267Sroot struct tcpcb *tcp_newtcpcb(); 335065Swnj /* 345065Swnj * TCP input routine, follows pages 65-76 of the 355065Swnj * protocol specification dated September, 1981 very closely. 365065Swnj */ 374924Swnj tcp_input(m0) 384924Swnj struct mbuf *m0; 394601Swnj { 404924Swnj register struct tcpiphdr *ti; 414924Swnj struct inpcb *inp; 424924Swnj register struct mbuf *m; 435440Swnj struct mbuf *om = 0; 444924Swnj int len, tlen, off; 455391Swnj register struct tcpcb *tp = 0; 464924Swnj register int tiflags; 474803Swnj struct socket *so; 485109Swnj int todrop, acked; 495267Sroot short ostate; 506028Sroot struct in_addr laddr; 514924Swnj 524601Swnj COUNT(TCP_INPUT); 534924Swnj /* 545244Sroot * Get IP and TCP header together in first mbuf. 555244Sroot * Note: IP leaves IP header in first mbuf. 564924Swnj */ 574924Swnj m = m0; 585020Sroot ti = mtod(m, struct tcpiphdr *); 595244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 605208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 615307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 625307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 635085Swnj tcpstat.tcps_hdrops++; 645307Sroot return; 655085Swnj } 665085Swnj ti = mtod(m, struct tcpiphdr *); 675085Swnj } 684601Swnj 694601Swnj /* 705244Sroot * Checksum extended TCP header and data. 714601Swnj */ 724924Swnj tlen = ((struct ip *)ti)->ip_len; 734924Swnj len = sizeof (struct ip) + tlen; 744679Swnj if (tcpcksum) { 754924Swnj ti->ti_next = ti->ti_prev = 0; 764924Swnj ti->ti_x1 = 0; 775223Swnj ti->ti_len = (u_short)tlen; 785223Swnj #if vax 79*6161Ssam ti->ti_len = htons((u_short)ti->ti_len); 805223Swnj #endif 815231Swnj if (ti->ti_sum = in_cksum(m, len)) { 824924Swnj tcpstat.tcps_badsum++; 835065Swnj 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 } 974924Swnj ti->ti_len = tlen - off; 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); 110*6161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 1115440Swnj m->m_len -= om->m_len; 112*6161Ssam bcopy(op+om->m_len, op, 113*6161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 1145440Swnj } 1155440Swnj } 1165065Swnj tiflags = ti->ti_flags; 1174924Swnj 1186093Sroot /* 1196093Sroot * drop IP header 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; 207*6161Ssam 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 } 3205300Sroot if (todrop > ti->ti_len) 3215065Swnj goto dropafterack; 3225065Swnj m_adj(m, todrop); 3235065Swnj ti->ti_seq += todrop; 3245065Swnj ti->ti_len -= todrop; 3255085Swnj if (ti->ti_urp > todrop) 3265085Swnj ti->ti_urp -= todrop; 3275085Swnj else { 3285085Swnj tiflags &= ~TH_URG; 3295690Swnj ti->ti_flags &= ~TH_URG; 3305690Swnj ti->ti_urp = 0; 3315085Swnj } 3325065Swnj } 3335065Swnj /* 3345065Swnj * If segment ends after window, drop trailing data 3355085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3365065Swnj */ 3375690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3385690Swnj if (todrop > 0) { 3395300Sroot if (todrop > ti->ti_len) 3405065Swnj goto dropafterack; 3415065Swnj m_adj(m, -todrop); 3425065Swnj ti->ti_len -= todrop; 3435085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3445065Swnj } 3455065Swnj } 3464601Swnj 3475065Swnj /* 3485951Swnj * If a segment is received on a connection after the 3495951Swnj * user processes are gone, then RST the other end. 3505951Swnj */ 3515951Swnj if (so->so_state & SS_USERGONE) { 3525951Swnj tcp_close(tp); 3535951Swnj goto dropwithreset; 3545951Swnj } 3555951Swnj 3565951Swnj /* 3575065Swnj * If the RST bit is set examine the state: 3585065Swnj * SYN_RECEIVED STATE: 3595065Swnj * If passive open, return to LISTEN state. 3605065Swnj * If active open, inform user that connection was refused. 3615065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3625065Swnj * Inform user that connection was reset, and close tcb. 3635065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3645065Swnj * Close the tcb. 3655065Swnj */ 3665065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3675267Sroot 3685065Swnj case TCPS_SYN_RECEIVED: 3695065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3705267Sroot /* a miniature tcp_close, but invisible to user */ 3715267Sroot (void) m_free(dtom(tp->t_template)); 3725267Sroot (void) m_free(dtom(tp)); 3735267Sroot inp->inp_ppcb = 0; 3745267Sroot tp = tcp_newtcpcb(inp); 3755085Swnj tp->t_state = TCPS_LISTEN; 3766028Sroot inp->inp_faddr.s_addr = 0; 3776028Sroot inp->inp_fport = 0; 3786028Sroot inp->inp_laddr.s_addr = 0; /* not quite right */ 3795065Swnj goto drop; 3804601Swnj } 3815085Swnj tcp_drop(tp, ECONNREFUSED); 3825065Swnj goto drop; 3834601Swnj 3845065Swnj case TCPS_ESTABLISHED: 3855065Swnj case TCPS_FIN_WAIT_1: 3865065Swnj case TCPS_FIN_WAIT_2: 3875065Swnj case TCPS_CLOSE_WAIT: 3885065Swnj tcp_drop(tp, ECONNRESET); 3895065Swnj goto drop; 3905065Swnj 3915065Swnj case TCPS_CLOSING: 3925065Swnj case TCPS_LAST_ACK: 3935065Swnj case TCPS_TIME_WAIT: 3945065Swnj tcp_close(tp); 3955065Swnj goto drop; 3964601Swnj } 3974601Swnj 3984601Swnj /* 3995065Swnj * If a SYN is in the window, then this is an 4005065Swnj * error and we send an RST and drop the connection. 4014601Swnj */ 4025065Swnj if (tiflags & TH_SYN) { 4035231Swnj tcp_drop(tp, ECONNRESET); 4045085Swnj goto dropwithreset; 4054601Swnj } 4064601Swnj 4074601Swnj /* 4085065Swnj * If the ACK bit is off we drop the segment and return. 4094601Swnj */ 4105085Swnj if ((tiflags & TH_ACK) == 0) 4115065Swnj goto drop; 4125065Swnj 4135065Swnj /* 4145065Swnj * Ack processing. 4155065Swnj */ 4164601Swnj switch (tp->t_state) { 4174601Swnj 4185065Swnj /* 4195065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4205065Swnj * ESTABLISHED state and continue processing, othewise 4215065Swnj * send an RST. 4225065Swnj */ 4235065Swnj case TCPS_SYN_RECEIVED: 4245085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4255231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4265085Swnj goto dropwithreset; 4275244Sroot tp->snd_una++; /* SYN acked */ 4285357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4295357Sroot tp->snd_nxt = tp->snd_una; 4305244Sroot tp->t_timer[TCPT_REXMT] = 0; 4315391Swnj if (so->so_options & SO_ACCEPTCONN) 4325391Swnj so->so_state |= SS_CONNAWAITING; 4335085Swnj soisconnected(so); 4345085Swnj tp->t_state = TCPS_ESTABLISHED; 4355162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4365244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4375085Swnj /* fall into ... */ 4384601Swnj 4395065Swnj /* 4405065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4415065Swnj * ACKs. If the ack is in the range 4425231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4435065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4445065Swnj * data from the retransmission queue. If this ACK reflects 4455065Swnj * more up to date window information we update our window information. 4465065Swnj */ 4475065Swnj case TCPS_ESTABLISHED: 4485065Swnj case TCPS_FIN_WAIT_1: 4495065Swnj case TCPS_FIN_WAIT_2: 4505065Swnj case TCPS_CLOSE_WAIT: 4515065Swnj case TCPS_CLOSING: 4525244Sroot case TCPS_LAST_ACK: 4535244Sroot case TCPS_TIME_WAIT: 4545085Swnj #define ourfinisacked (acked > 0) 4555085Swnj 4565244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4575065Swnj break; 4585300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4595065Swnj goto dropafterack; 4605085Swnj acked = ti->ti_ack - tp->snd_una; 4615951Swnj 4625951Swnj /* 4635951Swnj * If transmit timer is running and timed sequence 4645951Swnj * number was acked, update smoothed round trip time. 4655951Swnj */ 4665951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4675951Swnj if (tp->t_srtt == 0) 4685951Swnj tp->t_srtt = tp->t_rtt; 4695951Swnj else 4705951Swnj tp->t_srtt = 4715951Swnj tcp_alpha * tp->t_srtt + 4725951Swnj (1 - tcp_alpha) * tp->t_rtt; 4735951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */ 4745951Swnj tp->t_rtt = 0; 4755951Swnj } 4765951Swnj 4775307Sroot if (ti->ti_ack == tp->snd_max) 4785244Sroot tp->t_timer[TCPT_REXMT] = 0; 4795307Sroot else { 4805244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4815244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4825951Swnj tp->t_rtt = 1; 4835300Sroot tp->t_rxtshift = 0; 4845085Swnj } 4855307Sroot if (acked > so->so_snd.sb_cc) { 4865307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4875307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4885307Sroot } else { 489*6161Ssam sbdrop(&so->so_snd, acked); 4905307Sroot tp->snd_wnd -= acked; 4915307Sroot acked = 0; 4925307Sroot } 4935300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4945300Sroot sowwakeup(so); 4955231Swnj tp->snd_una = ti->ti_ack; 4965357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4975357Sroot tp->snd_nxt = tp->snd_una; 4985162Swnj 4994601Swnj switch (tp->t_state) { 5004601Swnj 5015065Swnj /* 5025065Swnj * In FIN_WAIT_1 STATE in addition to the processing 5035065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 5045085Swnj * then enter FIN_WAIT_2. 5055065Swnj */ 5065065Swnj case TCPS_FIN_WAIT_1: 5075896Swnj if (ourfinisacked) { 5085896Swnj /* 5095896Swnj * If we can't receive any more 5105896Swnj * data, then closing user can proceed. 5115896Swnj */ 5125896Swnj if (so->so_state & SS_CANTRCVMORE) 5135896Swnj soisdisconnected(so); 5145085Swnj tp->t_state = TCPS_FIN_WAIT_2; 5155896Swnj } 5164601Swnj break; 5174601Swnj 5185065Swnj /* 5195065Swnj * In CLOSING STATE in addition to the processing for 5205065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5215065Swnj * then enter the TIME-WAIT state, otherwise ignore 5225065Swnj * the segment. 5235065Swnj */ 5245065Swnj case TCPS_CLOSING: 5255244Sroot if (ourfinisacked) { 5265065Swnj tp->t_state = TCPS_TIME_WAIT; 5275244Sroot tcp_canceltimers(tp); 5285244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5295244Sroot soisdisconnected(so); 5305244Sroot } 5315244Sroot break; 5324601Swnj 5335065Swnj /* 5345085Swnj * The only thing that can arrive in LAST_ACK state 5355085Swnj * is an acknowledgment of our FIN. If our FIN is now 5365085Swnj * acknowledged, delete the TCB, enter the closed state 5375085Swnj * and return. 5385065Swnj */ 5395065Swnj case TCPS_LAST_ACK: 5405251Sroot if (ourfinisacked) 5415065Swnj tcp_close(tp); 5425065Swnj goto drop; 5434601Swnj 5445065Swnj /* 5455065Swnj * In TIME_WAIT state the only thing that should arrive 5465065Swnj * is a retransmission of the remote FIN. Acknowledge 5475065Swnj * it and restart the finack timer. 5485065Swnj */ 5495065Swnj case TCPS_TIME_WAIT: 5505162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5515065Swnj goto dropafterack; 5524601Swnj } 5535085Swnj #undef ourfinisacked 5545085Swnj } 5554601Swnj 5565065Swnj step6: 5575065Swnj /* 5585244Sroot * Update window information. 5595244Sroot */ 5605300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5615391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5625300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5635244Sroot tp->snd_wnd = ti->ti_win; 5645244Sroot tp->snd_wl1 = ti->ti_seq; 5655244Sroot tp->snd_wl2 = ti->ti_ack; 5665244Sroot if (tp->snd_wnd > 0) 5675244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5685244Sroot } 5695244Sroot 5705244Sroot /* 5715547Swnj * Process segments with URG. 5725065Swnj */ 5735547Swnj if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5745547Swnj /* 5755547Swnj * If this segment advances the known urgent pointer, 5765547Swnj * then mark the data stream. This should not happen 5775547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5785547Swnj * a FIN has been received from the remote side. 5795547Swnj * In these states we ignore the URG. 5805547Swnj */ 5815547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5825547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 5835547Swnj so->so_oobmark = so->so_rcv.sb_cc + 5845547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 5855547Swnj if (so->so_oobmark == 0) 5865547Swnj so->so_state |= SS_RCVATMARK; 5875440Swnj #ifdef TCPTRUEOOB 5885547Swnj if ((tp->t_flags & TF_DOOOB) == 0) 5895440Swnj #endif 5905547Swnj sohasoutofband(so); 5915547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 5925440Swnj } 5935547Swnj /* 5945547Swnj * Remove out of band data so doesn't get presented to user. 5955547Swnj * This can happen independent of advancing the URG pointer, 5965547Swnj * but if two URG's are pending at once, some out-of-band 5975547Swnj * data may creep in... ick. 5985547Swnj */ 5995547Swnj if (ti->ti_urp <= ti->ti_len) { 6005547Swnj tcp_pulloutofband(so, ti); 6015547Swnj } 6025419Swnj } 6034601Swnj 6044601Swnj /* 6055065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6065065Swnj * and arranging for acknowledgment of receipt if necessary. 6075065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6085065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6095065Swnj * case PRU_RCVD). If a FIN has already been received on this 6105065Swnj * connection then we just ignore the text. 6114601Swnj */ 6125263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6135263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6145065Swnj tiflags = tcp_reass(tp, ti); 6155440Swnj if (tcpnodelack == 0) 6165440Swnj tp->t_flags |= TF_DELACK; 6175440Swnj else 6185440Swnj tp->t_flags |= TF_ACKNOW; 6195244Sroot } else { 6204924Swnj m_freem(m); 6215263Swnj tiflags &= ~TH_FIN; 6225244Sroot } 6234601Swnj 6244601Swnj /* 6255263Swnj * If FIN is received ACK the FIN and let the user know 6265263Swnj * that the connection is closing. 6274601Swnj */ 6285263Swnj if (tiflags & TH_FIN) { 6295244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6305244Sroot socantrcvmore(so); 6315244Sroot tp->t_flags |= TF_ACKNOW; 6325244Sroot tp->rcv_nxt++; 6335244Sroot } 6345065Swnj switch (tp->t_state) { 6354601Swnj 6365065Swnj /* 6375065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6385065Swnj * enter the CLOSE_WAIT state. 6394884Swnj */ 6405065Swnj case TCPS_SYN_RECEIVED: 6415065Swnj case TCPS_ESTABLISHED: 6425065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6435065Swnj break; 6444884Swnj 6455065Swnj /* 6465085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6475085Swnj * enter the CLOSING state. 6484884Swnj */ 6495065Swnj case TCPS_FIN_WAIT_1: 6505085Swnj tp->t_state = TCPS_CLOSING; 6515065Swnj break; 6524601Swnj 6535065Swnj /* 6545065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6555065Swnj * starting the time-wait timer, turning off the other 6565065Swnj * standard timers. 6575065Swnj */ 6585065Swnj case TCPS_FIN_WAIT_2: 6595244Sroot tp->t_state = TCPS_TIME_WAIT; 6605074Swnj tcp_canceltimers(tp); 6615162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6625244Sroot soisdisconnected(so); 6635065Swnj break; 6645065Swnj 6654884Swnj /* 6665065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6674884Swnj */ 6685065Swnj case TCPS_TIME_WAIT: 6695162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6705065Swnj break; 6715085Swnj } 6724601Swnj } 6735267Sroot if (so->so_options & SO_DEBUG) 6745267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6755085Swnj 6765085Swnj /* 6775085Swnj * Return any desired output. 6785085Swnj */ 679*6161Ssam (void) tcp_output(tp); 6805065Swnj return; 6815085Swnj 6825065Swnj dropafterack: 6835085Swnj /* 6845244Sroot * Generate an ACK dropping incoming segment. 6855085Swnj * Make ACK reflect our state. 6865085Swnj */ 6875085Swnj if (tiflags & TH_RST) 6885085Swnj goto drop; 6895391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6905231Swnj return; 6915085Swnj 6925085Swnj dropwithreset: 6935440Swnj if (om) 694*6161Ssam (void) m_free(om); 6955085Swnj /* 6965244Sroot * Generate a RST, dropping incoming segment. 6975085Swnj * Make ACK acceptable to originator of segment. 6985085Swnj */ 6995085Swnj if (tiflags & TH_RST) 7005085Swnj goto drop; 7015085Swnj if (tiflags & TH_ACK) 7025391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7035085Swnj else { 7045085Swnj if (tiflags & TH_SYN) 7055085Swnj ti->ti_len++; 7065391Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 7075085Swnj } 7085231Swnj return; 7095085Swnj 7105065Swnj drop: 7115085Swnj /* 7125085Swnj * Drop space held by incoming segment and return. 7135085Swnj */ 7145065Swnj m_freem(m); 7155267Sroot return; 7165065Swnj } 7175065Swnj 7185440Swnj tcp_dooptions(tp, om) 7195440Swnj struct tcpcb *tp; 7205440Swnj struct mbuf *om; 7215419Swnj { 7225440Swnj register u_char *cp; 7235440Swnj int opt, optlen, cnt; 7245419Swnj 7255440Swnj cp = mtod(om, u_char *); 7265440Swnj cnt = om->m_len; 7275440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7285440Swnj opt = cp[0]; 7295440Swnj if (opt == TCPOPT_EOL) 7305440Swnj break; 7315440Swnj if (opt == TCPOPT_NOP) 7325440Swnj optlen = 1; 7335440Swnj else 7345440Swnj optlen = cp[1]; 7355440Swnj switch (opt) { 7365440Swnj 7375440Swnj default: 7385440Swnj break; 7395440Swnj 7405440Swnj case TCPOPT_MAXSEG: 7415440Swnj if (optlen != 4) 7425440Swnj continue; 7435440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7445440Swnj #if vax 745*6161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7465440Swnj #endif 7475440Swnj break; 7485440Swnj 7495440Swnj #ifdef TCPTRUEOOB 7505440Swnj case TCPOPT_WILLOOB: 7515440Swnj tp->t_flags |= TF_DOOOB; 7525440Swnj printf("tp %x dooob\n", tp); 7535440Swnj break; 7545440Swnj 7555440Swnj case TCPOPT_OOBDATA: { 7565440Swnj int seq; 7575547Swnj register struct socket *so = tp->t_inpcb->inp_socket; 7585547Swnj tcp_seq mark; 7595440Swnj 7605547Swnj if (optlen != 8) 7615440Swnj continue; 7625440Swnj seq = cp[2]; 7635440Swnj if (seq < tp->t_iobseq) 7645440Swnj seq += 256; 7655440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 7665440Swnj if (seq - tp->t_iobseq > 128) { 7675440Swnj printf("bad seq\n"); 7685440Swnj tp->t_oobflags |= TCPOOB_OWEACK; 7695440Swnj break; 7705440Swnj } 7715440Swnj tp->t_iobseq = cp[2]; 7725440Swnj tp->t_iobc = cp[3]; 7735547Swnj mark = *(tcp_seq *)(cp + 4); 7745547Swnj #if vax 7755547Swnj mark = ntohl(mark); 7765547Swnj #endif 7775547Swnj so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 7785547Swnj if (so->so_oobmark == 0) 7795547Swnj so->so_state |= SS_RCVATMARK; 7805440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 7815547Swnj sohasoutofband(so); 7825440Swnj break; 7835419Swnj } 7845440Swnj 7855440Swnj case TCPOPT_OOBACK: { 7865440Swnj int seq; 7875440Swnj 7885440Swnj if (optlen != 4) 7895440Swnj continue; 7905440Swnj if (tp->t_oobseq != cp[2]) { 7915440Swnj printf("wrong ack\n"); 7925440Swnj break; 7935440Swnj } 7945440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]); 7955440Swnj tp->t_oobflags &= ~TCPOOB_NEEDACK; 7965440Swnj tp->t_timer[TCPT_OOBREXMT] = 0; 7975419Swnj break; 7985440Swnj } 7995440Swnj #endif TCPTRUEOOB 8005440Swnj } 8015419Swnj } 802*6161Ssam (void) m_free(om); 8035419Swnj } 8045419Swnj 8055419Swnj /* 8065547Swnj * Pull out of band byte out of a segment so 8075547Swnj * it doesn't appear in the user's data queue. 8085547Swnj * It is still reflected in the segment length for 8095547Swnj * sequencing purposes. 8105547Swnj */ 8115547Swnj tcp_pulloutofband(so, ti) 8125547Swnj struct socket *so; 8135547Swnj struct tcpiphdr *ti; 8145547Swnj { 8155547Swnj register struct mbuf *m; 8166116Swnj int cnt = ti->ti_urp - 1; 8175547Swnj 8185547Swnj m = dtom(ti); 8195547Swnj while (cnt >= 0) { 8205547Swnj if (m->m_len > cnt) { 8215547Swnj char *cp = mtod(m, caddr_t) + cnt; 8225547Swnj struct tcpcb *tp = sototcpcb(so); 8235547Swnj 8245547Swnj tp->t_iobc = *cp; 8255547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 826*6161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 8275547Swnj m->m_len--; 8285547Swnj return; 8295547Swnj } 8305547Swnj cnt -= m->m_len; 8315547Swnj m = m->m_next; 8325547Swnj if (m == 0) 8335547Swnj break; 8345547Swnj } 8355547Swnj panic("tcp_pulloutofband"); 8365547Swnj } 8375547Swnj 8385547Swnj /* 8395065Swnj * Insert segment ti into reassembly queue of tcp with 8405065Swnj * control block tp. Return TH_FIN if reassembly now includes 8415065Swnj * a segment with FIN. 8425065Swnj */ 8435109Swnj tcp_reass(tp, ti) 8445065Swnj register struct tcpcb *tp; 8455065Swnj register struct tcpiphdr *ti; 8465065Swnj { 8475065Swnj register struct tcpiphdr *q; 8485085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8495263Swnj struct mbuf *m; 8505263Swnj int flags; 8515085Swnj COUNT(TCP_REASS); 8525065Swnj 8535065Swnj /* 8545162Swnj * Call with ti==0 after become established to 8555162Swnj * force pre-ESTABLISHED data up to user socket. 8565065Swnj */ 8575162Swnj if (ti == 0) 8585065Swnj goto present; 8594601Swnj 8605065Swnj /* 8615065Swnj * Find a segment which begins after this one does. 8625065Swnj */ 8635065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8645065Swnj q = (struct tcpiphdr *)q->ti_next) 8655065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8665065Swnj break; 8674601Swnj 8685065Swnj /* 8695065Swnj * If there is a preceding segment, it may provide some of 8705065Swnj * our data already. If so, drop the data from the incoming 8715065Swnj * segment. If it provides all of our data, drop us. 8725065Swnj */ 8735065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8745065Swnj register int i; 8755690Swnj q = (struct tcpiphdr *)q->ti_prev; 8765065Swnj /* conversion to int (in i) handles seq wraparound */ 8775065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8785065Swnj if (i > 0) { 8794924Swnj if (i >= ti->ti_len) 8805065Swnj goto drop; 8815065Swnj m_adj(dtom(tp), i); 8825065Swnj ti->ti_len -= i; 8834924Swnj ti->ti_seq += i; 8844601Swnj } 8855065Swnj q = (struct tcpiphdr *)(q->ti_next); 8865065Swnj } 8874601Swnj 8885065Swnj /* 8895065Swnj * While we overlap succeeding segments trim them or, 8905065Swnj * if they are completely covered, dequeue them. 8915065Swnj */ 8925690Swnj while (q != (struct tcpiphdr *)tp) { 8935065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8945690Swnj if (i <= 0) 8955690Swnj break; 8965065Swnj if (i < q->ti_len) { 8975690Swnj q->ti_seq += i; 8985065Swnj q->ti_len -= i; 8995065Swnj m_adj(dtom(q), i); 9005065Swnj break; 9014601Swnj } 9025065Swnj q = (struct tcpiphdr *)q->ti_next; 9035623Swnj m = dtom(q->ti_prev); 9045065Swnj remque(q->ti_prev); 9055623Swnj m_freem(m); 9065065Swnj } 9074601Swnj 9085065Swnj /* 9095065Swnj * Stick new segment in its place. 9105065Swnj */ 9115065Swnj insque(ti, q->ti_prev); 9124601Swnj 9135065Swnj present: 9145065Swnj /* 9155244Sroot * Present data to user, advancing rcv_nxt through 9165244Sroot * completed sequence space. 9175065Swnj */ 9185263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 9195244Sroot return (0); 9204924Swnj ti = tp->seg_next; 9215263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 9225263Swnj return (0); 9235263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9245263Swnj return (0); 9255263Swnj do { 9265244Sroot tp->rcv_nxt += ti->ti_len; 9275244Sroot flags = ti->ti_flags & TH_FIN; 9284924Swnj remque(ti); 9295263Swnj m = dtom(ti); 9304924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9315263Swnj if (so->so_state & SS_CANTRCVMORE) 932*6161Ssam m_freem(m); 9335263Swnj else 9345263Swnj sbappend(&so->so_rcv, m); 9355263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9365263Swnj sorwakeup(so); 9375065Swnj return (flags); 9385065Swnj drop: 9395065Swnj m_freem(dtom(ti)); 9405263Swnj return (0); 9414601Swnj } 942