1*6028Sroot /* tcp_input.c 1.56 82/03/03 */ 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; 48*6028Sroot 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 775223Swnj ti->ti_len = htons(ti->ti_len); 785223Swnj #endif 795231Swnj if (ti->ti_sum = in_cksum(m, len)) { 804924Swnj tcpstat.tcps_badsum++; 815065Swnj printf("tcp cksum %x\n", ti->ti_sum); 825085Swnj goto drop; 834601Swnj } 844601Swnj } 854601Swnj 864601Swnj /* 875244Sroot * Check that TCP offset makes sense, 885440Swnj * pull out TCP options and adjust length. 894601Swnj */ 904924Swnj off = ti->ti_off << 2; 915231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 924924Swnj tcpstat.tcps_badoff++; 935085Swnj goto drop; 944924Swnj } 954924Swnj ti->ti_len = tlen - off; 965440Swnj if (off > sizeof (struct tcphdr)) { 975440Swnj if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 985440Swnj tcpstat.tcps_hdrops++; 995440Swnj goto drop; 1005440Swnj } 1015440Swnj ti = mtod(m, struct tcpiphdr *); 1025440Swnj om = m_get(M_DONTWAIT); 1035440Swnj if (om == 0) 1045440Swnj goto drop; 1055440Swnj om->m_off = MMINOFF; 1065440Swnj om->m_len = off - sizeof (struct tcphdr); 1075440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 1085440Swnj bcopy(op, mtod(om, caddr_t), om->m_len); 1095440Swnj m->m_len -= om->m_len; 1105440Swnj bcopy(op+om->m_len, op, m->m_len-sizeof (struct tcpiphdr)); 1115440Swnj } 1125440Swnj } 1135065Swnj tiflags = ti->ti_flags; 1144924Swnj 1155231Swnj #if vax 1164924Swnj /* 1175244Sroot * Convert TCP protocol specific fields to host format. 1185085Swnj */ 1195085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1205085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1215085Swnj ti->ti_win = ntohs(ti->ti_win); 1225085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1235231Swnj #endif 1245085Swnj 1255085Swnj /* 1265994Swnj * Locate pcb for segment. On match, update the local 1275994Swnj * address stored in the block to reflect anchoring. 1284924Swnj */ 1295065Swnj inp = in_pcblookup 130*6028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 131*6028Sroot INPLOOKUP_WILDCARD); 1325065Swnj 1335065Swnj /* 1345065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1355244Sroot * all data in the incoming segment is discarded. 1365065Swnj */ 1375300Sroot if (inp == 0) 1385085Swnj goto dropwithreset; 1395065Swnj tp = intotcpcb(inp); 1405300Sroot if (tp == 0) 1415085Swnj goto dropwithreset; 1425109Swnj so = inp->inp_socket; 1435267Sroot if (so->so_options & SO_DEBUG) { 1445267Sroot ostate = tp->t_state; 1455267Sroot tcp_saveti = *ti; 1465267Sroot } 1474601Swnj 1484601Swnj /* 1495162Swnj * Segment received on connection. 1505162Swnj * Reset idle time and keep-alive timer. 1515162Swnj */ 1525162Swnj tp->t_idle = 0; 1535162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1545162Swnj 1555162Swnj /* 1565440Swnj * Process options. 1575440Swnj */ 1585440Swnj if (om) { 1595440Swnj tcp_dooptions(tp, om); 1605440Swnj om = 0; 1615440Swnj } 1625440Swnj 1635440Swnj /* 1645085Swnj * Calculate amount of space in receive window, 1655085Swnj * and then do TCP input processing. 1664601Swnj */ 1675085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1685231Swnj if (tp->rcv_wnd < 0) 1695231Swnj tp->rcv_wnd = 0; 1704601Swnj 1714601Swnj switch (tp->t_state) { 1724601Swnj 1735065Swnj /* 1745065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1755065Swnj * If the segment contains an ACK then it is bad and send a RST. 1765065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1775085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1785065Swnj * tp->iss, and send a segment: 1795085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1805065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1815065Swnj * Fill in remote peer address fields if not previously specified. 1825065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1835244Sroot * segment in this state. 1845065Swnj */ 1855065Swnj case TCPS_LISTEN: 1865065Swnj if (tiflags & TH_RST) 1875065Swnj goto drop; 1885300Sroot if (tiflags & TH_ACK) 1895085Swnj goto dropwithreset; 1905300Sroot if ((tiflags & TH_SYN) == 0) 1915065Swnj goto drop; 1925244Sroot tcp_in.sin_addr = ti->ti_src; 1935244Sroot tcp_in.sin_port = ti->ti_sport; 194*6028Sroot laddr = inp->inp_laddr; 195*6028Sroot if (inp->inp_laddr.s_addr == 0) 196*6028Sroot inp->inp_laddr = ti->ti_dst; 197*6028Sroot if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in)) { 198*6028Sroot inp->inp_laddr = laddr; 1995244Sroot goto drop; 200*6028Sroot } 2015244Sroot tp->t_template = tcp_template(tp); 2025244Sroot if (tp->t_template == 0) { 2035244Sroot in_pcbdisconnect(inp); 204*6028Sroot inp->inp_laddr = laddr; 2055244Sroot goto drop; 2065244Sroot } 207*6028Sroot in_setsockaddr(inp); 2085085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 2095065Swnj tp->irs = ti->ti_seq; 2105085Swnj tcp_sendseqinit(tp); 2115085Swnj tcp_rcvseqinit(tp); 2125065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2135244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 2145085Swnj goto trimthenstep6; 2154601Swnj 2165065Swnj /* 2175065Swnj * If the state is SYN_SENT: 2185065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2195065Swnj * if seg contains a RST, then drop the connection. 2205065Swnj * if seg does not contain SYN, then drop it. 2215065Swnj * Otherwise this is an acceptable SYN segment 2225065Swnj * initialize tp->rcv_nxt and tp->irs 2235065Swnj * if seg contains ack then advance tp->snd_una 2245065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2255065Swnj * arrange for segment to be acked (eventually) 2265065Swnj * continue processing rest of data/controls, beginning with URG 2275065Swnj */ 2285065Swnj case TCPS_SYN_SENT: 2295065Swnj if ((tiflags & TH_ACK) && 2305300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2315300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2325231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2335085Swnj goto dropwithreset; 2345065Swnj if (tiflags & TH_RST) { 2355065Swnj if (tiflags & TH_ACK) 2365267Sroot tcp_drop(tp, ECONNREFUSED); 2375065Swnj goto drop; 2384601Swnj } 2395065Swnj if ((tiflags & TH_SYN) == 0) 2405065Swnj goto drop; 2415231Swnj tp->snd_una = ti->ti_ack; 2425357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2435357Sroot tp->snd_nxt = tp->snd_una; 2445244Sroot tp->t_timer[TCPT_REXMT] = 0; 2455065Swnj tp->irs = ti->ti_seq; 2465085Swnj tcp_rcvseqinit(tp); 2475085Swnj tp->t_flags |= TF_ACKNOW; 2485162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2495391Swnj if (so->so_options & SO_ACCEPTCONN) 2505391Swnj so->so_state |= SS_CONNAWAITING; 2515244Sroot soisconnected(so); 2525065Swnj tp->t_state = TCPS_ESTABLISHED; 2535162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2545162Swnj } else 2555085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2565085Swnj goto trimthenstep6; 2575085Swnj 2585085Swnj trimthenstep6: 2595085Swnj /* 2605231Swnj * Advance ti->ti_seq to correspond to first data byte. 2615085Swnj * If data, trim to stay within window, 2625085Swnj * dropping FIN if necessary. 2635085Swnj */ 2645231Swnj ti->ti_seq++; 2655085Swnj if (ti->ti_len > tp->rcv_wnd) { 2665085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2675085Swnj m_adj(m, -todrop); 2685085Swnj ti->ti_len = tp->rcv_wnd; 2695085Swnj ti->ti_flags &= ~TH_FIN; 2705065Swnj } 2715263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2725085Swnj goto step6; 2735065Swnj } 2744601Swnj 2755065Swnj /* 2765065Swnj * States other than LISTEN or SYN_SENT. 2775065Swnj * First check that at least some bytes of segment are within 2785065Swnj * receive window. 2795065Swnj */ 2805065Swnj if (tp->rcv_wnd == 0) { 2815065Swnj /* 2825065Swnj * If window is closed can only take segments at 2835231Swnj * window edge, and have to drop data and PUSH from 2845065Swnj * incoming segments. 2855065Swnj */ 2865300Sroot if (tp->rcv_nxt != ti->ti_seq) 2875065Swnj goto dropafterack; 2885085Swnj if (ti->ti_len > 0) { 2895690Swnj m_adj(m, ti->ti_len); 2905085Swnj ti->ti_len = 0; 2915085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2925065Swnj } 2935065Swnj } else { 2945065Swnj /* 2955231Swnj * If segment begins before rcv_nxt, drop leading 2965065Swnj * data (and SYN); if nothing left, just ack. 2975065Swnj */ 2985690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2995690Swnj if (todrop > 0) { 3005085Swnj if (tiflags & TH_SYN) { 3015300Sroot tiflags &= ~TH_SYN; 3025690Swnj ti->ti_flags &= ~TH_SYN; 3035085Swnj ti->ti_seq++; 3045085Swnj if (ti->ti_urp > 1) 3055085Swnj ti->ti_urp--; 3065085Swnj else 3075085Swnj tiflags &= ~TH_URG; 3085085Swnj todrop--; 3095085Swnj } 3105300Sroot if (todrop > ti->ti_len) 3115065Swnj goto dropafterack; 3125065Swnj m_adj(m, todrop); 3135065Swnj ti->ti_seq += todrop; 3145065Swnj ti->ti_len -= todrop; 3155085Swnj if (ti->ti_urp > todrop) 3165085Swnj ti->ti_urp -= todrop; 3175085Swnj else { 3185085Swnj tiflags &= ~TH_URG; 3195690Swnj ti->ti_flags &= ~TH_URG; 3205690Swnj ti->ti_urp = 0; 3215085Swnj } 3225065Swnj } 3235065Swnj /* 3245065Swnj * If segment ends after window, drop trailing data 3255085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3265065Swnj */ 3275690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3285690Swnj if (todrop > 0) { 3295300Sroot if (todrop > ti->ti_len) 3305065Swnj goto dropafterack; 3315065Swnj m_adj(m, -todrop); 3325065Swnj ti->ti_len -= todrop; 3335085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3345065Swnj } 3355065Swnj } 3364601Swnj 3375065Swnj /* 3385951Swnj * If a segment is received on a connection after the 3395951Swnj * user processes are gone, then RST the other end. 3405951Swnj */ 3415951Swnj if (so->so_state & SS_USERGONE) { 3425951Swnj tcp_close(tp); 3435951Swnj goto dropwithreset; 3445951Swnj } 3455951Swnj 3465951Swnj /* 3475065Swnj * If the RST bit is set examine the state: 3485065Swnj * SYN_RECEIVED STATE: 3495065Swnj * If passive open, return to LISTEN state. 3505065Swnj * If active open, inform user that connection was refused. 3515065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3525065Swnj * Inform user that connection was reset, and close tcb. 3535065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3545065Swnj * Close the tcb. 3555065Swnj */ 3565065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3575267Sroot 3585065Swnj case TCPS_SYN_RECEIVED: 3595065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3605267Sroot /* a miniature tcp_close, but invisible to user */ 3615267Sroot (void) m_free(dtom(tp->t_template)); 3625267Sroot (void) m_free(dtom(tp)); 3635267Sroot inp->inp_ppcb = 0; 3645267Sroot tp = tcp_newtcpcb(inp); 3655085Swnj tp->t_state = TCPS_LISTEN; 366*6028Sroot inp->inp_faddr.s_addr = 0; 367*6028Sroot inp->inp_fport = 0; 368*6028Sroot inp->inp_laddr.s_addr = 0; /* not quite right */ 3695065Swnj goto drop; 3704601Swnj } 3715085Swnj tcp_drop(tp, ECONNREFUSED); 3725065Swnj goto drop; 3734601Swnj 3745065Swnj case TCPS_ESTABLISHED: 3755065Swnj case TCPS_FIN_WAIT_1: 3765065Swnj case TCPS_FIN_WAIT_2: 3775065Swnj case TCPS_CLOSE_WAIT: 3785065Swnj tcp_drop(tp, ECONNRESET); 3795065Swnj goto drop; 3805065Swnj 3815065Swnj case TCPS_CLOSING: 3825065Swnj case TCPS_LAST_ACK: 3835065Swnj case TCPS_TIME_WAIT: 3845065Swnj tcp_close(tp); 3855065Swnj goto drop; 3864601Swnj } 3874601Swnj 3884601Swnj /* 3895065Swnj * If a SYN is in the window, then this is an 3905065Swnj * error and we send an RST and drop the connection. 3914601Swnj */ 3925065Swnj if (tiflags & TH_SYN) { 3935231Swnj tcp_drop(tp, ECONNRESET); 3945085Swnj goto dropwithreset; 3954601Swnj } 3964601Swnj 3974601Swnj /* 3985065Swnj * If the ACK bit is off we drop the segment and return. 3994601Swnj */ 4005085Swnj if ((tiflags & TH_ACK) == 0) 4015065Swnj goto drop; 4025065Swnj 4035065Swnj /* 4045065Swnj * Ack processing. 4055065Swnj */ 4064601Swnj switch (tp->t_state) { 4074601Swnj 4085065Swnj /* 4095065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4105065Swnj * ESTABLISHED state and continue processing, othewise 4115065Swnj * send an RST. 4125065Swnj */ 4135065Swnj case TCPS_SYN_RECEIVED: 4145085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4155231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4165085Swnj goto dropwithreset; 4175244Sroot tp->snd_una++; /* SYN acked */ 4185357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4195357Sroot tp->snd_nxt = tp->snd_una; 4205244Sroot tp->t_timer[TCPT_REXMT] = 0; 4215391Swnj if (so->so_options & SO_ACCEPTCONN) 4225391Swnj so->so_state |= SS_CONNAWAITING; 4235085Swnj soisconnected(so); 4245085Swnj tp->t_state = TCPS_ESTABLISHED; 4255162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4265244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4275085Swnj /* fall into ... */ 4284601Swnj 4295065Swnj /* 4305065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4315065Swnj * ACKs. If the ack is in the range 4325231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4335065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4345065Swnj * data from the retransmission queue. If this ACK reflects 4355065Swnj * more up to date window information we update our window information. 4365065Swnj */ 4375065Swnj case TCPS_ESTABLISHED: 4385065Swnj case TCPS_FIN_WAIT_1: 4395065Swnj case TCPS_FIN_WAIT_2: 4405065Swnj case TCPS_CLOSE_WAIT: 4415065Swnj case TCPS_CLOSING: 4425244Sroot case TCPS_LAST_ACK: 4435244Sroot case TCPS_TIME_WAIT: 4445085Swnj #define ourfinisacked (acked > 0) 4455085Swnj 4465244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4475065Swnj break; 4485300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4495065Swnj goto dropafterack; 4505085Swnj acked = ti->ti_ack - tp->snd_una; 4515951Swnj 4525951Swnj /* 4535951Swnj * If transmit timer is running and timed sequence 4545951Swnj * number was acked, update smoothed round trip time. 4555951Swnj */ 4565951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4575951Swnj if (tp->t_srtt == 0) 4585951Swnj tp->t_srtt = tp->t_rtt; 4595951Swnj else 4605951Swnj tp->t_srtt = 4615951Swnj tcp_alpha * tp->t_srtt + 4625951Swnj (1 - tcp_alpha) * tp->t_rtt; 4635951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */ 4645951Swnj tp->t_rtt = 0; 4655951Swnj } 4665951Swnj 4675307Sroot if (ti->ti_ack == tp->snd_max) 4685244Sroot tp->t_timer[TCPT_REXMT] = 0; 4695307Sroot else { 4705244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4715244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4725951Swnj tp->t_rtt = 1; 4735300Sroot tp->t_rxtshift = 0; 4745085Swnj } 4755307Sroot if (acked > so->so_snd.sb_cc) { 4765307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4775307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4785307Sroot } else { 4795307Sroot sbdrop(&so->so_snd.sb_cc, acked); 4805307Sroot tp->snd_wnd -= acked; 4815307Sroot acked = 0; 4825307Sroot } 4835300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4845300Sroot sowwakeup(so); 4855231Swnj tp->snd_una = ti->ti_ack; 4865357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4875357Sroot tp->snd_nxt = tp->snd_una; 4885162Swnj 4894601Swnj switch (tp->t_state) { 4904601Swnj 4915065Swnj /* 4925065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4935065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4945085Swnj * then enter FIN_WAIT_2. 4955065Swnj */ 4965065Swnj case TCPS_FIN_WAIT_1: 4975896Swnj if (ourfinisacked) { 4985896Swnj /* 4995896Swnj * If we can't receive any more 5005896Swnj * data, then closing user can proceed. 5015896Swnj */ 5025896Swnj if (so->so_state & SS_CANTRCVMORE) 5035896Swnj soisdisconnected(so); 5045085Swnj tp->t_state = TCPS_FIN_WAIT_2; 5055896Swnj } 5064601Swnj break; 5074601Swnj 5085065Swnj /* 5095065Swnj * In CLOSING STATE in addition to the processing for 5105065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5115065Swnj * then enter the TIME-WAIT state, otherwise ignore 5125065Swnj * the segment. 5135065Swnj */ 5145065Swnj case TCPS_CLOSING: 5155244Sroot if (ourfinisacked) { 5165065Swnj tp->t_state = TCPS_TIME_WAIT; 5175244Sroot tcp_canceltimers(tp); 5185244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5195244Sroot soisdisconnected(so); 5205244Sroot } 5215244Sroot break; 5224601Swnj 5235065Swnj /* 5245085Swnj * The only thing that can arrive in LAST_ACK state 5255085Swnj * is an acknowledgment of our FIN. If our FIN is now 5265085Swnj * acknowledged, delete the TCB, enter the closed state 5275085Swnj * and return. 5285065Swnj */ 5295065Swnj case TCPS_LAST_ACK: 5305251Sroot if (ourfinisacked) 5315065Swnj tcp_close(tp); 5325065Swnj goto drop; 5334601Swnj 5345065Swnj /* 5355065Swnj * In TIME_WAIT state the only thing that should arrive 5365065Swnj * is a retransmission of the remote FIN. Acknowledge 5375065Swnj * it and restart the finack timer. 5385065Swnj */ 5395065Swnj case TCPS_TIME_WAIT: 5405162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5415065Swnj goto dropafterack; 5424601Swnj } 5435085Swnj #undef ourfinisacked 5445085Swnj } 5454601Swnj 5465065Swnj step6: 5475065Swnj /* 5485244Sroot * Update window information. 5495244Sroot */ 5505300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5515391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5525300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5535244Sroot tp->snd_wnd = ti->ti_win; 5545244Sroot tp->snd_wl1 = ti->ti_seq; 5555244Sroot tp->snd_wl2 = ti->ti_ack; 5565244Sroot if (tp->snd_wnd > 0) 5575244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5585244Sroot } 5595244Sroot 5605244Sroot /* 5615547Swnj * Process segments with URG. 5625065Swnj */ 5635547Swnj if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5645547Swnj /* 5655547Swnj * If this segment advances the known urgent pointer, 5665547Swnj * then mark the data stream. This should not happen 5675547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5685547Swnj * a FIN has been received from the remote side. 5695547Swnj * In these states we ignore the URG. 5705547Swnj */ 5715547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5725547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 5735547Swnj so->so_oobmark = so->so_rcv.sb_cc + 5745547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 5755547Swnj if (so->so_oobmark == 0) 5765547Swnj so->so_state |= SS_RCVATMARK; 5775440Swnj #ifdef TCPTRUEOOB 5785547Swnj if ((tp->t_flags & TF_DOOOB) == 0) 5795440Swnj #endif 5805547Swnj sohasoutofband(so); 5815547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 5825440Swnj } 5835547Swnj /* 5845547Swnj * Remove out of band data so doesn't get presented to user. 5855547Swnj * This can happen independent of advancing the URG pointer, 5865547Swnj * but if two URG's are pending at once, some out-of-band 5875547Swnj * data may creep in... ick. 5885547Swnj */ 5895547Swnj if (ti->ti_urp <= ti->ti_len) { 5905547Swnj tcp_pulloutofband(so, ti); 5915547Swnj } 5925419Swnj } 5934601Swnj 5944601Swnj /* 5955065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5965065Swnj * and arranging for acknowledgment of receipt if necessary. 5975065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5985065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5995065Swnj * case PRU_RCVD). If a FIN has already been received on this 6005065Swnj * connection then we just ignore the text. 6014601Swnj */ 6025263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6035263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6045085Swnj off += sizeof (struct ip); /* drop IP header */ 6055085Swnj m->m_off += off; 6065085Swnj m->m_len -= off; 6075065Swnj tiflags = tcp_reass(tp, ti); 6085440Swnj if (tcpnodelack == 0) 6095440Swnj tp->t_flags |= TF_DELACK; 6105440Swnj else 6115440Swnj tp->t_flags |= TF_ACKNOW; 6125244Sroot } else { 6134924Swnj m_freem(m); 6145263Swnj tiflags &= ~TH_FIN; 6155244Sroot } 6164601Swnj 6174601Swnj /* 6185263Swnj * If FIN is received ACK the FIN and let the user know 6195263Swnj * that the connection is closing. 6204601Swnj */ 6215263Swnj if (tiflags & TH_FIN) { 6225244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6235244Sroot socantrcvmore(so); 6245244Sroot tp->t_flags |= TF_ACKNOW; 6255244Sroot tp->rcv_nxt++; 6265244Sroot } 6275065Swnj switch (tp->t_state) { 6284601Swnj 6295065Swnj /* 6305065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6315065Swnj * enter the CLOSE_WAIT state. 6324884Swnj */ 6335065Swnj case TCPS_SYN_RECEIVED: 6345065Swnj case TCPS_ESTABLISHED: 6355065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6365065Swnj break; 6374884Swnj 6385065Swnj /* 6395085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6405085Swnj * enter the CLOSING state. 6414884Swnj */ 6425065Swnj case TCPS_FIN_WAIT_1: 6435085Swnj tp->t_state = TCPS_CLOSING; 6445065Swnj break; 6454601Swnj 6465065Swnj /* 6475065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6485065Swnj * starting the time-wait timer, turning off the other 6495065Swnj * standard timers. 6505065Swnj */ 6515065Swnj case TCPS_FIN_WAIT_2: 6525244Sroot tp->t_state = TCPS_TIME_WAIT; 6535074Swnj tcp_canceltimers(tp); 6545162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6555244Sroot soisdisconnected(so); 6565065Swnj break; 6575065Swnj 6584884Swnj /* 6595065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6604884Swnj */ 6615065Swnj case TCPS_TIME_WAIT: 6625162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6635065Swnj break; 6645085Swnj } 6654601Swnj } 6665267Sroot if (so->so_options & SO_DEBUG) 6675267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6685085Swnj 6695085Swnj /* 6705085Swnj * Return any desired output. 6715085Swnj */ 6725085Swnj tcp_output(tp); 6735065Swnj return; 6745085Swnj 6755065Swnj dropafterack: 6765085Swnj /* 6775244Sroot * Generate an ACK dropping incoming segment. 6785085Swnj * Make ACK reflect our state. 6795085Swnj */ 6805085Swnj if (tiflags & TH_RST) 6815085Swnj goto drop; 6825391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6835231Swnj return; 6845085Swnj 6855085Swnj dropwithreset: 6865440Swnj if (om) 6875440Swnj m_free(om); 6885085Swnj /* 6895244Sroot * Generate a RST, dropping incoming segment. 6905085Swnj * Make ACK acceptable to originator of segment. 6915085Swnj */ 6925085Swnj if (tiflags & TH_RST) 6935085Swnj goto drop; 6945085Swnj if (tiflags & TH_ACK) 6955391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6965085Swnj else { 6975085Swnj if (tiflags & TH_SYN) 6985085Swnj ti->ti_len++; 6995391Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 7005085Swnj } 7015231Swnj return; 7025085Swnj 7035065Swnj drop: 7045085Swnj /* 7055085Swnj * Drop space held by incoming segment and return. 7065085Swnj */ 7075065Swnj m_freem(m); 7085267Sroot return; 7095065Swnj } 7105065Swnj 7115440Swnj tcp_dooptions(tp, om) 7125440Swnj struct tcpcb *tp; 7135440Swnj struct mbuf *om; 7145419Swnj { 7155440Swnj register u_char *cp; 7165440Swnj int opt, optlen, cnt; 7175419Swnj 7185440Swnj cp = mtod(om, u_char *); 7195440Swnj cnt = om->m_len; 7205440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7215440Swnj opt = cp[0]; 7225440Swnj if (opt == TCPOPT_EOL) 7235440Swnj break; 7245440Swnj if (opt == TCPOPT_NOP) 7255440Swnj optlen = 1; 7265440Swnj else 7275440Swnj optlen = cp[1]; 7285440Swnj switch (opt) { 7295440Swnj 7305440Swnj default: 7315440Swnj break; 7325440Swnj 7335440Swnj case TCPOPT_MAXSEG: 7345440Swnj if (optlen != 4) 7355440Swnj continue; 7365440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7375440Swnj #if vax 7385440Swnj tp->t_maxseg = ntohs(tp->t_maxseg); 7395440Swnj #endif 7405440Swnj break; 7415440Swnj 7425440Swnj #ifdef TCPTRUEOOB 7435440Swnj case TCPOPT_WILLOOB: 7445440Swnj tp->t_flags |= TF_DOOOB; 7455440Swnj printf("tp %x dooob\n", tp); 7465440Swnj break; 7475440Swnj 7485440Swnj case TCPOPT_OOBDATA: { 7495440Swnj int seq; 7505547Swnj register struct socket *so = tp->t_inpcb->inp_socket; 7515547Swnj tcp_seq mark; 7525440Swnj 7535547Swnj if (optlen != 8) 7545440Swnj continue; 7555440Swnj seq = cp[2]; 7565440Swnj if (seq < tp->t_iobseq) 7575440Swnj seq += 256; 7585440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 7595440Swnj if (seq - tp->t_iobseq > 128) { 7605440Swnj printf("bad seq\n"); 7615440Swnj tp->t_oobflags |= TCPOOB_OWEACK; 7625440Swnj break; 7635440Swnj } 7645440Swnj tp->t_iobseq = cp[2]; 7655440Swnj tp->t_iobc = cp[3]; 7665547Swnj mark = *(tcp_seq *)(cp + 4); 7675547Swnj #if vax 7685547Swnj mark = ntohl(mark); 7695547Swnj #endif 7705547Swnj so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 7715547Swnj if (so->so_oobmark == 0) 7725547Swnj so->so_state |= SS_RCVATMARK; 7735440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 7745547Swnj sohasoutofband(so); 7755440Swnj break; 7765419Swnj } 7775440Swnj 7785440Swnj case TCPOPT_OOBACK: { 7795440Swnj int seq; 7805440Swnj 7815440Swnj if (optlen != 4) 7825440Swnj continue; 7835440Swnj if (tp->t_oobseq != cp[2]) { 7845440Swnj printf("wrong ack\n"); 7855440Swnj break; 7865440Swnj } 7875440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]); 7885440Swnj tp->t_oobflags &= ~TCPOOB_NEEDACK; 7895440Swnj tp->t_timer[TCPT_OOBREXMT] = 0; 7905419Swnj break; 7915440Swnj } 7925440Swnj #endif TCPTRUEOOB 7935440Swnj } 7945419Swnj } 7955440Swnj m_free(om); 7965419Swnj } 7975419Swnj 7985419Swnj /* 7995547Swnj * Pull out of band byte out of a segment so 8005547Swnj * it doesn't appear in the user's data queue. 8015547Swnj * It is still reflected in the segment length for 8025547Swnj * sequencing purposes. 8035547Swnj */ 8045547Swnj tcp_pulloutofband(so, ti) 8055547Swnj struct socket *so; 8065547Swnj struct tcpiphdr *ti; 8075547Swnj { 8085547Swnj register struct mbuf *m; 8095547Swnj int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1; 8105547Swnj 8115547Swnj m = dtom(ti); 8125547Swnj while (cnt >= 0) { 8135547Swnj if (m->m_len > cnt) { 8145547Swnj char *cp = mtod(m, caddr_t) + cnt; 8155547Swnj struct tcpcb *tp = sototcpcb(so); 8165547Swnj 8175547Swnj tp->t_iobc = *cp; 8185547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 8195547Swnj bcopy(cp+1, cp, m->m_len - cnt - 1); 8205547Swnj m->m_len--; 8215547Swnj return; 8225547Swnj } 8235547Swnj cnt -= m->m_len; 8245547Swnj m = m->m_next; 8255547Swnj if (m == 0) 8265547Swnj break; 8275547Swnj } 8285547Swnj panic("tcp_pulloutofband"); 8295547Swnj } 8305547Swnj 8315547Swnj /* 8325065Swnj * Insert segment ti into reassembly queue of tcp with 8335065Swnj * control block tp. Return TH_FIN if reassembly now includes 8345065Swnj * a segment with FIN. 8355065Swnj */ 8365109Swnj tcp_reass(tp, ti) 8375065Swnj register struct tcpcb *tp; 8385065Swnj register struct tcpiphdr *ti; 8395065Swnj { 8405065Swnj register struct tcpiphdr *q; 8415085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8425263Swnj struct mbuf *m; 8435263Swnj int flags; 8445085Swnj COUNT(TCP_REASS); 8455065Swnj 8465065Swnj /* 8475162Swnj * Call with ti==0 after become established to 8485162Swnj * force pre-ESTABLISHED data up to user socket. 8495065Swnj */ 8505162Swnj if (ti == 0) 8515065Swnj goto present; 8524601Swnj 8535065Swnj /* 8545065Swnj * Find a segment which begins after this one does. 8555065Swnj */ 8565065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8575065Swnj q = (struct tcpiphdr *)q->ti_next) 8585065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8595065Swnj break; 8604601Swnj 8615065Swnj /* 8625065Swnj * If there is a preceding segment, it may provide some of 8635065Swnj * our data already. If so, drop the data from the incoming 8645065Swnj * segment. If it provides all of our data, drop us. 8655065Swnj */ 8665065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8675065Swnj register int i; 8685690Swnj q = (struct tcpiphdr *)q->ti_prev; 8695065Swnj /* conversion to int (in i) handles seq wraparound */ 8705065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8715065Swnj if (i > 0) { 8724924Swnj if (i >= ti->ti_len) 8735065Swnj goto drop; 8745065Swnj m_adj(dtom(tp), i); 8755065Swnj ti->ti_len -= i; 8764924Swnj ti->ti_seq += i; 8774601Swnj } 8785065Swnj q = (struct tcpiphdr *)(q->ti_next); 8795065Swnj } 8804601Swnj 8815065Swnj /* 8825065Swnj * While we overlap succeeding segments trim them or, 8835065Swnj * if they are completely covered, dequeue them. 8845065Swnj */ 8855690Swnj while (q != (struct tcpiphdr *)tp) { 8865065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8875690Swnj if (i <= 0) 8885690Swnj break; 8895065Swnj if (i < q->ti_len) { 8905690Swnj q->ti_seq += i; 8915065Swnj q->ti_len -= i; 8925065Swnj m_adj(dtom(q), i); 8935065Swnj break; 8944601Swnj } 8955065Swnj q = (struct tcpiphdr *)q->ti_next; 8965623Swnj m = dtom(q->ti_prev); 8975065Swnj remque(q->ti_prev); 8985623Swnj m_freem(m); 8995065Swnj } 9004601Swnj 9015065Swnj /* 9025065Swnj * Stick new segment in its place. 9035065Swnj */ 9045065Swnj insque(ti, q->ti_prev); 9054601Swnj 9065065Swnj present: 9075065Swnj /* 9085244Sroot * Present data to user, advancing rcv_nxt through 9095244Sroot * completed sequence space. 9105065Swnj */ 9115263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 9125244Sroot return (0); 9134924Swnj ti = tp->seg_next; 9145263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 9155263Swnj return (0); 9165263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9175263Swnj return (0); 9185263Swnj do { 9195244Sroot tp->rcv_nxt += ti->ti_len; 9205244Sroot flags = ti->ti_flags & TH_FIN; 9214924Swnj remque(ti); 9225263Swnj m = dtom(ti); 9234924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9245263Swnj if (so->so_state & SS_CANTRCVMORE) 9255263Swnj (void) m_freem(m); 9265263Swnj else 9275263Swnj sbappend(&so->so_rcv, m); 9285263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9295263Swnj sorwakeup(so); 9305065Swnj return (flags); 9315065Swnj drop: 9325065Swnj m_freem(dtom(ti)); 9335263Swnj return (0); 9344601Swnj } 935