1*5547Swnj /* tcp_input.c 1.50 82/01/18 */ 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; 484924Swnj 494601Swnj COUNT(TCP_INPUT); 504924Swnj /* 515244Sroot * Get IP and TCP header together in first mbuf. 525244Sroot * Note: IP leaves IP header in first mbuf. 534924Swnj */ 544924Swnj m = m0; 555020Sroot ti = mtod(m, struct tcpiphdr *); 565244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 575208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 585307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 595307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 605085Swnj tcpstat.tcps_hdrops++; 615307Sroot return; 625085Swnj } 635085Swnj ti = mtod(m, struct tcpiphdr *); 645085Swnj } 654601Swnj 664601Swnj /* 675244Sroot * Checksum extended TCP header and data. 684601Swnj */ 694924Swnj tlen = ((struct ip *)ti)->ip_len; 704924Swnj len = sizeof (struct ip) + tlen; 714679Swnj if (tcpcksum) { 724924Swnj ti->ti_next = ti->ti_prev = 0; 734924Swnj ti->ti_x1 = 0; 745223Swnj ti->ti_len = (u_short)tlen; 755223Swnj #if vax 765223Swnj ti->ti_len = htons(ti->ti_len); 775223Swnj #endif 785231Swnj if (ti->ti_sum = in_cksum(m, len)) { 794924Swnj tcpstat.tcps_badsum++; 805065Swnj printf("tcp cksum %x\n", ti->ti_sum); 815085Swnj goto drop; 824601Swnj } 834601Swnj } 844601Swnj 854601Swnj /* 865244Sroot * Check that TCP offset makes sense, 875440Swnj * pull out TCP options and adjust length. 884601Swnj */ 894924Swnj off = ti->ti_off << 2; 905231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 914924Swnj tcpstat.tcps_badoff++; 925085Swnj goto drop; 934924Swnj } 944924Swnj ti->ti_len = tlen - off; 955440Swnj if (off > sizeof (struct tcphdr)) { 965440Swnj if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 975440Swnj tcpstat.tcps_hdrops++; 985440Swnj goto drop; 995440Swnj } 1005440Swnj ti = mtod(m, struct tcpiphdr *); 1015440Swnj om = m_get(M_DONTWAIT); 1025440Swnj if (om == 0) 1035440Swnj goto drop; 1045440Swnj om->m_off = MMINOFF; 1055440Swnj om->m_len = off - sizeof (struct tcphdr); 1065440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 1075440Swnj bcopy(op, mtod(om, caddr_t), om->m_len); 1085440Swnj m->m_len -= om->m_len; 1095440Swnj bcopy(op+om->m_len, op, m->m_len-sizeof (struct tcpiphdr)); 1105440Swnj } 1115440Swnj } 1125065Swnj tiflags = ti->ti_flags; 1134924Swnj 1145231Swnj #if vax 1154924Swnj /* 1165244Sroot * Convert TCP protocol specific fields to host format. 1175085Swnj */ 1185085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1195085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1205085Swnj ti->ti_win = ntohs(ti->ti_win); 1215085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1225231Swnj #endif 1235085Swnj 1245085Swnj /* 1254924Swnj * Locate pcb for segment. 1264924Swnj */ 1275065Swnj inp = in_pcblookup 1285065Swnj (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 1295065Swnj 1305065Swnj /* 1315065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1325244Sroot * all data in the incoming segment is discarded. 1335065Swnj */ 1345300Sroot if (inp == 0) 1355085Swnj goto dropwithreset; 1365065Swnj tp = intotcpcb(inp); 1375300Sroot if (tp == 0) 1385085Swnj goto dropwithreset; 1395109Swnj so = inp->inp_socket; 1405267Sroot if (so->so_options & SO_DEBUG) { 1415267Sroot ostate = tp->t_state; 1425267Sroot tcp_saveti = *ti; 1435267Sroot } 1444601Swnj 1454601Swnj /* 1465162Swnj * Segment received on connection. 1475162Swnj * Reset idle time and keep-alive timer. 1485162Swnj */ 1495162Swnj tp->t_idle = 0; 1505162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1515162Swnj 1525162Swnj /* 1535440Swnj * Process options. 1545440Swnj */ 1555440Swnj if (om) { 1565440Swnj tcp_dooptions(tp, om); 1575440Swnj om = 0; 1585440Swnj } 1595440Swnj 1605440Swnj /* 1615085Swnj * Calculate amount of space in receive window, 1625085Swnj * and then do TCP input processing. 1634601Swnj */ 1645085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1655231Swnj if (tp->rcv_wnd < 0) 1665231Swnj tp->rcv_wnd = 0; 1674601Swnj 1684601Swnj switch (tp->t_state) { 1694601Swnj 1705065Swnj /* 1715065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1725065Swnj * If the segment contains an ACK then it is bad and send a RST. 1735065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1745085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1755065Swnj * tp->iss, and send a segment: 1765085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1775065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1785065Swnj * Fill in remote peer address fields if not previously specified. 1795065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1805244Sroot * segment in this state. 1815065Swnj */ 1825065Swnj case TCPS_LISTEN: 1835065Swnj if (tiflags & TH_RST) 1845065Swnj goto drop; 1855300Sroot if (tiflags & TH_ACK) 1865085Swnj goto dropwithreset; 1875300Sroot if ((tiflags & TH_SYN) == 0) 1885065Swnj goto drop; 1895244Sroot tcp_in.sin_addr = ti->ti_src; 1905244Sroot tcp_in.sin_port = ti->ti_sport; 1915300Sroot if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in)) 1925244Sroot goto drop; 1935244Sroot tp->t_template = tcp_template(tp); 1945244Sroot if (tp->t_template == 0) { 1955244Sroot in_pcbdisconnect(inp); 1965244Sroot goto drop; 1975244Sroot } 1985085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1995065Swnj tp->irs = ti->ti_seq; 2005085Swnj tcp_sendseqinit(tp); 2015085Swnj tcp_rcvseqinit(tp); 2025065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2035244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 2045085Swnj goto trimthenstep6; 2054601Swnj 2065065Swnj /* 2075065Swnj * If the state is SYN_SENT: 2085065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2095065Swnj * if seg contains a RST, then drop the connection. 2105065Swnj * if seg does not contain SYN, then drop it. 2115065Swnj * Otherwise this is an acceptable SYN segment 2125065Swnj * initialize tp->rcv_nxt and tp->irs 2135065Swnj * if seg contains ack then advance tp->snd_una 2145065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2155065Swnj * arrange for segment to be acked (eventually) 2165065Swnj * continue processing rest of data/controls, beginning with URG 2175065Swnj */ 2185065Swnj case TCPS_SYN_SENT: 2195065Swnj if ((tiflags & TH_ACK) && 2205300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2215300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2225231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2235085Swnj goto dropwithreset; 2245065Swnj if (tiflags & TH_RST) { 2255065Swnj if (tiflags & TH_ACK) 2265267Sroot tcp_drop(tp, ECONNREFUSED); 2275065Swnj goto drop; 2284601Swnj } 2295065Swnj if ((tiflags & TH_SYN) == 0) 2305065Swnj goto drop; 2315231Swnj tp->snd_una = ti->ti_ack; 2325357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2335357Sroot tp->snd_nxt = tp->snd_una; 2345244Sroot tp->t_timer[TCPT_REXMT] = 0; 2355065Swnj tp->irs = ti->ti_seq; 2365085Swnj tcp_rcvseqinit(tp); 2375085Swnj tp->t_flags |= TF_ACKNOW; 2385162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2395391Swnj if (so->so_options & SO_ACCEPTCONN) 2405391Swnj so->so_state |= SS_CONNAWAITING; 2415244Sroot soisconnected(so); 2425065Swnj tp->t_state = TCPS_ESTABLISHED; 2435162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2445162Swnj } else 2455085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2465085Swnj goto trimthenstep6; 2475085Swnj 2485085Swnj trimthenstep6: 2495085Swnj /* 2505231Swnj * Advance ti->ti_seq to correspond to first data byte. 2515085Swnj * If data, trim to stay within window, 2525085Swnj * dropping FIN if necessary. 2535085Swnj */ 2545231Swnj ti->ti_seq++; 2555085Swnj if (ti->ti_len > tp->rcv_wnd) { 2565085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2575085Swnj m_adj(m, -todrop); 2585085Swnj ti->ti_len = tp->rcv_wnd; 2595085Swnj ti->ti_flags &= ~TH_FIN; 2605065Swnj } 2615263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2625085Swnj goto step6; 2635065Swnj } 2644601Swnj 2655065Swnj /* 2665065Swnj * States other than LISTEN or SYN_SENT. 2675065Swnj * First check that at least some bytes of segment are within 2685065Swnj * receive window. 2695065Swnj */ 2705065Swnj if (tp->rcv_wnd == 0) { 2715065Swnj /* 2725065Swnj * If window is closed can only take segments at 2735231Swnj * window edge, and have to drop data and PUSH from 2745065Swnj * incoming segments. 2755065Swnj */ 2765300Sroot if (tp->rcv_nxt != ti->ti_seq) 2775065Swnj goto dropafterack; 2785085Swnj if (ti->ti_len > 0) { 2795085Swnj ti->ti_len = 0; 2805085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2815065Swnj } 2825065Swnj } else { 2835065Swnj /* 2845231Swnj * If segment begins before rcv_nxt, drop leading 2855065Swnj * data (and SYN); if nothing left, just ack. 2865065Swnj */ 2875065Swnj if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 2885085Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2895085Swnj if (tiflags & TH_SYN) { 2905300Sroot tiflags &= ~TH_SYN; 2915085Swnj ti->ti_seq++; 2925085Swnj if (ti->ti_urp > 1) 2935085Swnj ti->ti_urp--; 2945085Swnj else 2955085Swnj tiflags &= ~TH_URG; 2965085Swnj todrop--; 2975085Swnj } 2985300Sroot if (todrop > ti->ti_len) 2995065Swnj goto dropafterack; 3005065Swnj m_adj(m, todrop); 3015065Swnj ti->ti_seq += todrop; 3025065Swnj ti->ti_len -= todrop; 3035085Swnj if (ti->ti_urp > todrop) 3045085Swnj ti->ti_urp -= todrop; 3055085Swnj else { 3065085Swnj tiflags &= ~TH_URG; 3075085Swnj /* ti->ti_flags &= ~TH_URG; */ 3085085Swnj /* ti->ti_urp = 0; */ 3095085Swnj } 3105085Swnj /* tiflags &= ~TH_SYN; */ 3115085Swnj /* ti->ti_flags &= ~TH_SYN; */ 3125065Swnj } 3135065Swnj /* 3145065Swnj * If segment ends after window, drop trailing data 3155085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3165065Swnj */ 3175065Swnj if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 3185085Swnj todrop = 3195065Swnj ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 3205300Sroot if (todrop > ti->ti_len) 3215065Swnj goto dropafterack; 3225065Swnj m_adj(m, -todrop); 3235065Swnj ti->ti_len -= todrop; 3245085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3255065Swnj } 3265065Swnj } 3274601Swnj 3285065Swnj /* 3295065Swnj * If the RST bit is set examine the state: 3305065Swnj * SYN_RECEIVED STATE: 3315065Swnj * If passive open, return to LISTEN state. 3325065Swnj * If active open, inform user that connection was refused. 3335065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3345065Swnj * Inform user that connection was reset, and close tcb. 3355065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3365065Swnj * Close the tcb. 3375065Swnj */ 3385065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3395267Sroot 3405065Swnj case TCPS_SYN_RECEIVED: 3415065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3425267Sroot /* a miniature tcp_close, but invisible to user */ 3435267Sroot (void) m_free(dtom(tp->t_template)); 3445267Sroot (void) m_free(dtom(tp)); 3455267Sroot inp->inp_ppcb = 0; 3465267Sroot tp = tcp_newtcpcb(inp); 3475085Swnj tp->t_state = TCPS_LISTEN; 3485065Swnj goto drop; 3494601Swnj } 3505085Swnj tcp_drop(tp, ECONNREFUSED); 3515065Swnj goto drop; 3524601Swnj 3535065Swnj case TCPS_ESTABLISHED: 3545065Swnj case TCPS_FIN_WAIT_1: 3555065Swnj case TCPS_FIN_WAIT_2: 3565065Swnj case TCPS_CLOSE_WAIT: 3575065Swnj tcp_drop(tp, ECONNRESET); 3585065Swnj goto drop; 3595065Swnj 3605065Swnj case TCPS_CLOSING: 3615065Swnj case TCPS_LAST_ACK: 3625065Swnj case TCPS_TIME_WAIT: 3635065Swnj tcp_close(tp); 3645065Swnj goto drop; 3654601Swnj } 3664601Swnj 3674601Swnj /* 3685065Swnj * If a SYN is in the window, then this is an 3695065Swnj * error and we send an RST and drop the connection. 3704601Swnj */ 3715065Swnj if (tiflags & TH_SYN) { 3725231Swnj tcp_drop(tp, ECONNRESET); 3735085Swnj goto dropwithreset; 3744601Swnj } 3754601Swnj 3764601Swnj /* 3775065Swnj * If the ACK bit is off we drop the segment and return. 3784601Swnj */ 3795085Swnj if ((tiflags & TH_ACK) == 0) 3805065Swnj goto drop; 3815065Swnj 3825065Swnj /* 3835065Swnj * Ack processing. 3845065Swnj */ 3854601Swnj switch (tp->t_state) { 3864601Swnj 3875065Swnj /* 3885065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3895065Swnj * ESTABLISHED state and continue processing, othewise 3905065Swnj * send an RST. 3915065Swnj */ 3925065Swnj case TCPS_SYN_RECEIVED: 3935085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 3945231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 3955085Swnj goto dropwithreset; 3965244Sroot tp->snd_una++; /* SYN acked */ 3975357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 3985357Sroot tp->snd_nxt = tp->snd_una; 3995244Sroot tp->t_timer[TCPT_REXMT] = 0; 4005391Swnj if (so->so_options & SO_ACCEPTCONN) 4015391Swnj so->so_state |= SS_CONNAWAITING; 4025085Swnj soisconnected(so); 4035085Swnj tp->t_state = TCPS_ESTABLISHED; 4045162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4055244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4065085Swnj /* fall into ... */ 4074601Swnj 4085065Swnj /* 4095065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4105065Swnj * ACKs. If the ack is in the range 4115231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4125065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4135065Swnj * data from the retransmission queue. If this ACK reflects 4145065Swnj * more up to date window information we update our window information. 4155065Swnj */ 4165065Swnj case TCPS_ESTABLISHED: 4175065Swnj case TCPS_FIN_WAIT_1: 4185065Swnj case TCPS_FIN_WAIT_2: 4195065Swnj case TCPS_CLOSE_WAIT: 4205065Swnj case TCPS_CLOSING: 4215244Sroot case TCPS_LAST_ACK: 4225244Sroot case TCPS_TIME_WAIT: 4235085Swnj #define ourfinisacked (acked > 0) 4245085Swnj 4255244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4265065Swnj break; 4275300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4285065Swnj goto dropafterack; 4295085Swnj acked = ti->ti_ack - tp->snd_una; 4305307Sroot if (ti->ti_ack == tp->snd_max) 4315244Sroot tp->t_timer[TCPT_REXMT] = 0; 4325307Sroot else { 4335244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4345244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4355300Sroot tp->t_rtt = 0; 4365300Sroot tp->t_rxtshift = 0; 4375085Swnj } 4385307Sroot if (acked > so->so_snd.sb_cc) { 4395307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4405307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4415307Sroot } else { 4425307Sroot sbdrop(&so->so_snd.sb_cc, acked); 4435307Sroot tp->snd_wnd -= acked; 4445307Sroot acked = 0; 4455307Sroot } 4465300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4475300Sroot sowwakeup(so); 4485231Swnj tp->snd_una = ti->ti_ack; 4495357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4505357Sroot tp->snd_nxt = tp->snd_una; 4515162Swnj 4525162Swnj /* 4535162Swnj * If transmit timer is running and timed sequence 4545162Swnj * number was acked, update smoothed round trip time. 4555162Swnj */ 4565162Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4575244Sroot if (tp->t_srtt == 0) 4585244Sroot tp->t_srtt = tp->t_rtt; 4595244Sroot else 4605244Sroot tp->t_srtt = 4615244Sroot tcp_alpha * tp->t_srtt + 4625244Sroot (1 - tcp_alpha) * tp->t_rtt; 4635162Swnj tp->t_rtt = 0; 4645162Swnj } 4655162Swnj 4664601Swnj switch (tp->t_state) { 4674601Swnj 4685065Swnj /* 4695065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4705065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4715085Swnj * then enter FIN_WAIT_2. 4725065Swnj */ 4735065Swnj case TCPS_FIN_WAIT_1: 4745085Swnj if (ourfinisacked) 4755085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4764601Swnj break; 4774601Swnj 4785065Swnj /* 4795065Swnj * In CLOSING STATE in addition to the processing for 4805065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4815065Swnj * then enter the TIME-WAIT state, otherwise ignore 4825065Swnj * the segment. 4835065Swnj */ 4845065Swnj case TCPS_CLOSING: 4855244Sroot if (ourfinisacked) { 4865065Swnj tp->t_state = TCPS_TIME_WAIT; 4875244Sroot tcp_canceltimers(tp); 4885244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4895244Sroot soisdisconnected(so); 4905244Sroot } 4915244Sroot break; 4924601Swnj 4935065Swnj /* 4945085Swnj * The only thing that can arrive in LAST_ACK state 4955085Swnj * is an acknowledgment of our FIN. If our FIN is now 4965085Swnj * acknowledged, delete the TCB, enter the closed state 4975085Swnj * and return. 4985065Swnj */ 4995065Swnj case TCPS_LAST_ACK: 5005251Sroot if (ourfinisacked) 5015065Swnj tcp_close(tp); 5025065Swnj goto drop; 5034601Swnj 5045065Swnj /* 5055065Swnj * In TIME_WAIT state the only thing that should arrive 5065065Swnj * is a retransmission of the remote FIN. Acknowledge 5075065Swnj * it and restart the finack timer. 5085065Swnj */ 5095065Swnj case TCPS_TIME_WAIT: 5105162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5115065Swnj goto dropafterack; 5124601Swnj } 5135085Swnj #undef ourfinisacked 5145085Swnj } 5154601Swnj 5165065Swnj step6: 5175065Swnj /* 5185244Sroot * Update window information. 5195244Sroot */ 5205300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5215391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5225300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5235391Swnj /* 5245391Swnj printf("wl1 %x seq %x wl2 %x ack %x win %x wnd %x\n", tp->snd_wl1, ti->ti_seq, tp->snd_wl2, ti->ti_ack, ti->ti_win, tp->snd_wnd); 5255391Swnj */ 5265244Sroot tp->snd_wnd = ti->ti_win; 5275244Sroot tp->snd_wl1 = ti->ti_seq; 5285244Sroot tp->snd_wl2 = ti->ti_ack; 5295244Sroot if (tp->snd_wnd > 0) 5305244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5315244Sroot } 5325244Sroot 5335244Sroot /* 534*5547Swnj * Process segments with URG. 5355065Swnj */ 536*5547Swnj if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 537*5547Swnj /* 538*5547Swnj * If this segment advances the known urgent pointer, 539*5547Swnj * then mark the data stream. This should not happen 540*5547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 541*5547Swnj * a FIN has been received from the remote side. 542*5547Swnj * In these states we ignore the URG. 543*5547Swnj */ 544*5547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 545*5547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 546*5547Swnj so->so_oobmark = so->so_rcv.sb_cc + 547*5547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 548*5547Swnj if (so->so_oobmark == 0) 549*5547Swnj so->so_state |= SS_RCVATMARK; 5505440Swnj #ifdef TCPTRUEOOB 551*5547Swnj if ((tp->t_flags & TF_DOOOB) == 0) 5525440Swnj #endif 553*5547Swnj sohasoutofband(so); 554*5547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 5555440Swnj } 556*5547Swnj /* 557*5547Swnj * Remove out of band data so doesn't get presented to user. 558*5547Swnj * This can happen independent of advancing the URG pointer, 559*5547Swnj * but if two URG's are pending at once, some out-of-band 560*5547Swnj * data may creep in... ick. 561*5547Swnj */ 562*5547Swnj if (ti->ti_urp <= ti->ti_len) { 563*5547Swnj tcp_pulloutofband(so, ti); 564*5547Swnj } 5655419Swnj } 5664601Swnj 5674601Swnj /* 5685065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5695065Swnj * and arranging for acknowledgment of receipt if necessary. 5705065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5715065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5725065Swnj * case PRU_RCVD). If a FIN has already been received on this 5735065Swnj * connection then we just ignore the text. 5744601Swnj */ 5755263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 5765263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5775085Swnj off += sizeof (struct ip); /* drop IP header */ 5785085Swnj m->m_off += off; 5795085Swnj m->m_len -= off; 5805065Swnj tiflags = tcp_reass(tp, ti); 5815440Swnj if (tcpnodelack == 0) 5825440Swnj tp->t_flags |= TF_DELACK; 5835440Swnj else 5845440Swnj tp->t_flags |= TF_ACKNOW; 5855244Sroot } else { 5864924Swnj m_freem(m); 5875263Swnj tiflags &= ~TH_FIN; 5885244Sroot } 5894601Swnj 5904601Swnj /* 5915263Swnj * If FIN is received ACK the FIN and let the user know 5925263Swnj * that the connection is closing. 5934601Swnj */ 5945263Swnj if (tiflags & TH_FIN) { 5955244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5965244Sroot socantrcvmore(so); 5975244Sroot tp->t_flags |= TF_ACKNOW; 5985244Sroot tp->rcv_nxt++; 5995244Sroot } 6005065Swnj switch (tp->t_state) { 6014601Swnj 6025065Swnj /* 6035065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6045065Swnj * enter the CLOSE_WAIT state. 6054884Swnj */ 6065065Swnj case TCPS_SYN_RECEIVED: 6075065Swnj case TCPS_ESTABLISHED: 6085065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6095065Swnj break; 6104884Swnj 6115065Swnj /* 6125085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6135085Swnj * enter the CLOSING state. 6144884Swnj */ 6155065Swnj case TCPS_FIN_WAIT_1: 6165085Swnj tp->t_state = TCPS_CLOSING; 6175065Swnj break; 6184601Swnj 6195065Swnj /* 6205065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6215065Swnj * starting the time-wait timer, turning off the other 6225065Swnj * standard timers. 6235065Swnj */ 6245065Swnj case TCPS_FIN_WAIT_2: 6255244Sroot tp->t_state = TCPS_TIME_WAIT; 6265074Swnj tcp_canceltimers(tp); 6275162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6285244Sroot soisdisconnected(so); 6295065Swnj break; 6305065Swnj 6314884Swnj /* 6325065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6334884Swnj */ 6345065Swnj case TCPS_TIME_WAIT: 6355162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6365065Swnj break; 6375085Swnj } 6384601Swnj } 6395267Sroot if (so->so_options & SO_DEBUG) 6405267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6415085Swnj 6425085Swnj /* 6435085Swnj * Return any desired output. 6445085Swnj */ 6455085Swnj tcp_output(tp); 6465065Swnj return; 6475085Swnj 6485065Swnj dropafterack: 6495085Swnj /* 6505244Sroot * Generate an ACK dropping incoming segment. 6515085Swnj * Make ACK reflect our state. 6525085Swnj */ 6535085Swnj if (tiflags & TH_RST) 6545085Swnj goto drop; 6555391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6565231Swnj return; 6575085Swnj 6585085Swnj dropwithreset: 6595440Swnj if (om) 6605440Swnj m_free(om); 6615085Swnj /* 6625244Sroot * Generate a RST, dropping incoming segment. 6635085Swnj * Make ACK acceptable to originator of segment. 6645085Swnj */ 6655085Swnj if (tiflags & TH_RST) 6665085Swnj goto drop; 6675085Swnj if (tiflags & TH_ACK) 6685391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6695085Swnj else { 6705085Swnj if (tiflags & TH_SYN) 6715085Swnj ti->ti_len++; 6725391Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6735085Swnj } 6745231Swnj return; 6755085Swnj 6765065Swnj drop: 6775085Swnj /* 6785085Swnj * Drop space held by incoming segment and return. 6795085Swnj */ 6805065Swnj m_freem(m); 6815267Sroot return; 6825065Swnj } 6835065Swnj 6845440Swnj tcp_dooptions(tp, om) 6855440Swnj struct tcpcb *tp; 6865440Swnj struct mbuf *om; 6875419Swnj { 6885440Swnj register u_char *cp; 6895440Swnj int opt, optlen, cnt; 6905419Swnj 6915440Swnj cp = mtod(om, u_char *); 6925440Swnj cnt = om->m_len; 6935440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 6945440Swnj opt = cp[0]; 6955440Swnj if (opt == TCPOPT_EOL) 6965440Swnj break; 6975440Swnj if (opt == TCPOPT_NOP) 6985440Swnj optlen = 1; 6995440Swnj else 7005440Swnj optlen = cp[1]; 7015440Swnj switch (opt) { 7025440Swnj 7035440Swnj default: 7045440Swnj break; 7055440Swnj 7065440Swnj case TCPOPT_MAXSEG: 7075440Swnj if (optlen != 4) 7085440Swnj continue; 7095440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7105440Swnj #if vax 7115440Swnj tp->t_maxseg = ntohs(tp->t_maxseg); 7125440Swnj #endif 7135440Swnj break; 7145440Swnj 7155440Swnj #ifdef TCPTRUEOOB 7165440Swnj case TCPOPT_WILLOOB: 7175440Swnj tp->t_flags |= TF_DOOOB; 7185440Swnj printf("tp %x dooob\n", tp); 7195440Swnj break; 7205440Swnj 7215440Swnj case TCPOPT_OOBDATA: { 7225440Swnj int seq; 723*5547Swnj register struct socket *so = tp->t_inpcb->inp_socket; 724*5547Swnj tcp_seq mark; 7255440Swnj 726*5547Swnj if (optlen != 8) 7275440Swnj continue; 7285440Swnj seq = cp[2]; 7295440Swnj if (seq < tp->t_iobseq) 7305440Swnj seq += 256; 7315440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 7325440Swnj if (seq - tp->t_iobseq > 128) { 7335440Swnj printf("bad seq\n"); 7345440Swnj tp->t_oobflags |= TCPOOB_OWEACK; 7355440Swnj break; 7365440Swnj } 7375440Swnj tp->t_iobseq = cp[2]; 7385440Swnj tp->t_iobc = cp[3]; 739*5547Swnj mark = *(tcp_seq *)(cp + 4); 740*5547Swnj #if vax 741*5547Swnj mark = ntohl(mark); 742*5547Swnj #endif 743*5547Swnj so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 744*5547Swnj if (so->so_oobmark == 0) 745*5547Swnj so->so_state |= SS_RCVATMARK; 7465440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 747*5547Swnj sohasoutofband(so); 7485440Swnj break; 7495419Swnj } 7505440Swnj 7515440Swnj case TCPOPT_OOBACK: { 7525440Swnj int seq; 7535440Swnj 7545440Swnj if (optlen != 4) 7555440Swnj continue; 7565440Swnj if (tp->t_oobseq != cp[2]) { 7575440Swnj printf("wrong ack\n"); 7585440Swnj break; 7595440Swnj } 7605440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]); 7615440Swnj tp->t_oobflags &= ~TCPOOB_NEEDACK; 7625440Swnj tp->t_timer[TCPT_OOBREXMT] = 0; 7635419Swnj break; 7645440Swnj } 7655440Swnj #endif TCPTRUEOOB 7665440Swnj } 7675419Swnj } 7685440Swnj m_free(om); 7695419Swnj } 7705419Swnj 7715419Swnj /* 772*5547Swnj * Pull out of band byte out of a segment so 773*5547Swnj * it doesn't appear in the user's data queue. 774*5547Swnj * It is still reflected in the segment length for 775*5547Swnj * sequencing purposes. 776*5547Swnj */ 777*5547Swnj tcp_pulloutofband(so, ti) 778*5547Swnj struct socket *so; 779*5547Swnj struct tcpiphdr *ti; 780*5547Swnj { 781*5547Swnj register struct mbuf *m; 782*5547Swnj int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1; 783*5547Swnj 784*5547Swnj m = dtom(ti); 785*5547Swnj while (cnt >= 0) { 786*5547Swnj if (m->m_len > cnt) { 787*5547Swnj char *cp = mtod(m, caddr_t) + cnt; 788*5547Swnj struct tcpcb *tp = sototcpcb(so); 789*5547Swnj 790*5547Swnj tp->t_iobc = *cp; 791*5547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 792*5547Swnj bcopy(cp+1, cp, m->m_len - cnt - 1); 793*5547Swnj m->m_len--; 794*5547Swnj return; 795*5547Swnj } 796*5547Swnj cnt -= m->m_len; 797*5547Swnj m = m->m_next; 798*5547Swnj if (m == 0) 799*5547Swnj break; 800*5547Swnj } 801*5547Swnj panic("tcp_pulloutofband"); 802*5547Swnj } 803*5547Swnj 804*5547Swnj /* 8055065Swnj * Insert segment ti into reassembly queue of tcp with 8065065Swnj * control block tp. Return TH_FIN if reassembly now includes 8075065Swnj * a segment with FIN. 8085065Swnj */ 8095109Swnj tcp_reass(tp, ti) 8105065Swnj register struct tcpcb *tp; 8115065Swnj register struct tcpiphdr *ti; 8125065Swnj { 8135065Swnj register struct tcpiphdr *q; 8145085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8155263Swnj struct mbuf *m; 8165263Swnj int flags; 8175085Swnj COUNT(TCP_REASS); 8185065Swnj 8195065Swnj /* 8205162Swnj * Call with ti==0 after become established to 8215162Swnj * force pre-ESTABLISHED data up to user socket. 8225065Swnj */ 8235162Swnj if (ti == 0) 8245065Swnj goto present; 8254601Swnj 8265065Swnj /* 8275065Swnj * Find a segment which begins after this one does. 8285065Swnj */ 8295065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8305065Swnj q = (struct tcpiphdr *)q->ti_next) 8315065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8325065Swnj break; 8334601Swnj 8345065Swnj /* 8355065Swnj * If there is a preceding segment, it may provide some of 8365065Swnj * our data already. If so, drop the data from the incoming 8375065Swnj * segment. If it provides all of our data, drop us. 8385065Swnj */ 8395065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8405065Swnj register int i; 8415065Swnj q = (struct tcpiphdr *)(q->ti_prev); 8425065Swnj /* conversion to int (in i) handles seq wraparound */ 8435065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8445065Swnj if (i > 0) { 8454924Swnj if (i >= ti->ti_len) 8465065Swnj goto drop; 8475065Swnj m_adj(dtom(tp), i); 8485065Swnj ti->ti_len -= i; 8494924Swnj ti->ti_seq += i; 8504601Swnj } 8515065Swnj q = (struct tcpiphdr *)(q->ti_next); 8525065Swnj } 8534601Swnj 8545065Swnj /* 8555065Swnj * While we overlap succeeding segments trim them or, 8565065Swnj * if they are completely covered, dequeue them. 8575065Swnj */ 8585065Swnj while (q != (struct tcpiphdr *)tp && 8595065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 8605065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8615065Swnj if (i < q->ti_len) { 8625065Swnj q->ti_len -= i; 8635065Swnj m_adj(dtom(q), i); 8645065Swnj break; 8654601Swnj } 8665065Swnj q = (struct tcpiphdr *)q->ti_next; 8675065Swnj m_freem(dtom(q->ti_prev)); 8685065Swnj remque(q->ti_prev); 8695065Swnj } 8704601Swnj 8715065Swnj /* 8725065Swnj * Stick new segment in its place. 8735065Swnj */ 8745065Swnj insque(ti, q->ti_prev); 8754601Swnj 8765065Swnj present: 8775065Swnj /* 8785244Sroot * Present data to user, advancing rcv_nxt through 8795244Sroot * completed sequence space. 8805065Swnj */ 8815263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 8825244Sroot return (0); 8834924Swnj ti = tp->seg_next; 8845263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 8855263Swnj return (0); 8865263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 8875263Swnj return (0); 8885263Swnj do { 8895244Sroot tp->rcv_nxt += ti->ti_len; 8905244Sroot flags = ti->ti_flags & TH_FIN; 8914924Swnj remque(ti); 8925263Swnj m = dtom(ti); 8934924Swnj ti = (struct tcpiphdr *)ti->ti_next; 8945263Swnj if (so->so_state & SS_CANTRCVMORE) 8955263Swnj (void) m_freem(m); 8965263Swnj else 8975263Swnj sbappend(&so->so_rcv, m); 8985263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 8995263Swnj sorwakeup(so); 9005065Swnj return (flags); 9015065Swnj drop: 9025065Swnj m_freem(dtom(ti)); 9035263Swnj return (0); 9044601Swnj } 905