1*5896Swnj /* tcp_input.c 1.53 82/02/19 */ 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) { 2795690Swnj m_adj(m, ti->ti_len); 2805085Swnj ti->ti_len = 0; 2815085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2825065Swnj } 2835065Swnj } else { 2845065Swnj /* 2855231Swnj * If segment begins before rcv_nxt, drop leading 2865065Swnj * data (and SYN); if nothing left, just ack. 2875065Swnj */ 2885690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2895690Swnj if (todrop > 0) { 2905085Swnj if (tiflags & TH_SYN) { 2915300Sroot tiflags &= ~TH_SYN; 2925690Swnj ti->ti_flags &= ~TH_SYN; 2935085Swnj ti->ti_seq++; 2945085Swnj if (ti->ti_urp > 1) 2955085Swnj ti->ti_urp--; 2965085Swnj else 2975085Swnj tiflags &= ~TH_URG; 2985085Swnj todrop--; 2995085Swnj } 3005300Sroot if (todrop > ti->ti_len) 3015065Swnj goto dropafterack; 3025065Swnj m_adj(m, todrop); 3035065Swnj ti->ti_seq += todrop; 3045065Swnj ti->ti_len -= todrop; 3055085Swnj if (ti->ti_urp > todrop) 3065085Swnj ti->ti_urp -= todrop; 3075085Swnj else { 3085085Swnj tiflags &= ~TH_URG; 3095690Swnj ti->ti_flags &= ~TH_URG; 3105690Swnj ti->ti_urp = 0; 3115085Swnj } 3125065Swnj } 3135065Swnj /* 3145065Swnj * If segment ends after window, drop trailing data 3155085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3165065Swnj */ 3175690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3185690Swnj if (todrop > 0) { 3195300Sroot if (todrop > ti->ti_len) 3205065Swnj goto dropafterack; 3215065Swnj m_adj(m, -todrop); 3225065Swnj ti->ti_len -= todrop; 3235085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3245065Swnj } 3255065Swnj } 3264601Swnj 3275065Swnj /* 3285065Swnj * If the RST bit is set examine the state: 3295065Swnj * SYN_RECEIVED STATE: 3305065Swnj * If passive open, return to LISTEN state. 3315065Swnj * If active open, inform user that connection was refused. 3325065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3335065Swnj * Inform user that connection was reset, and close tcb. 3345065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3355065Swnj * Close the tcb. 3365065Swnj */ 3375065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3385267Sroot 3395065Swnj case TCPS_SYN_RECEIVED: 3405065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3415267Sroot /* a miniature tcp_close, but invisible to user */ 3425267Sroot (void) m_free(dtom(tp->t_template)); 3435267Sroot (void) m_free(dtom(tp)); 3445267Sroot inp->inp_ppcb = 0; 3455267Sroot tp = tcp_newtcpcb(inp); 3465085Swnj tp->t_state = TCPS_LISTEN; 3475065Swnj goto drop; 3484601Swnj } 3495085Swnj tcp_drop(tp, ECONNREFUSED); 3505065Swnj goto drop; 3514601Swnj 3525065Swnj case TCPS_ESTABLISHED: 3535065Swnj case TCPS_FIN_WAIT_1: 3545065Swnj case TCPS_FIN_WAIT_2: 3555065Swnj case TCPS_CLOSE_WAIT: 3565065Swnj tcp_drop(tp, ECONNRESET); 3575065Swnj goto drop; 3585065Swnj 3595065Swnj case TCPS_CLOSING: 3605065Swnj case TCPS_LAST_ACK: 3615065Swnj case TCPS_TIME_WAIT: 3625065Swnj tcp_close(tp); 3635065Swnj goto drop; 3644601Swnj } 3654601Swnj 3664601Swnj /* 3675065Swnj * If a SYN is in the window, then this is an 3685065Swnj * error and we send an RST and drop the connection. 3694601Swnj */ 3705065Swnj if (tiflags & TH_SYN) { 3715231Swnj tcp_drop(tp, ECONNRESET); 3725085Swnj goto dropwithreset; 3734601Swnj } 3744601Swnj 3754601Swnj /* 3765065Swnj * If the ACK bit is off we drop the segment and return. 3774601Swnj */ 3785085Swnj if ((tiflags & TH_ACK) == 0) 3795065Swnj goto drop; 3805065Swnj 3815065Swnj /* 3825065Swnj * Ack processing. 3835065Swnj */ 3844601Swnj switch (tp->t_state) { 3854601Swnj 3865065Swnj /* 3875065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3885065Swnj * ESTABLISHED state and continue processing, othewise 3895065Swnj * send an RST. 3905065Swnj */ 3915065Swnj case TCPS_SYN_RECEIVED: 3925085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 3935231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 3945085Swnj goto dropwithreset; 3955244Sroot tp->snd_una++; /* SYN acked */ 3965357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 3975357Sroot tp->snd_nxt = tp->snd_una; 3985244Sroot tp->t_timer[TCPT_REXMT] = 0; 3995391Swnj if (so->so_options & SO_ACCEPTCONN) 4005391Swnj so->so_state |= SS_CONNAWAITING; 4015085Swnj soisconnected(so); 4025085Swnj tp->t_state = TCPS_ESTABLISHED; 4035162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4045244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4055085Swnj /* fall into ... */ 4064601Swnj 4075065Swnj /* 4085065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4095065Swnj * ACKs. If the ack is in the range 4105231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4115065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4125065Swnj * data from the retransmission queue. If this ACK reflects 4135065Swnj * more up to date window information we update our window information. 4145065Swnj */ 4155065Swnj case TCPS_ESTABLISHED: 4165065Swnj case TCPS_FIN_WAIT_1: 4175065Swnj case TCPS_FIN_WAIT_2: 4185065Swnj case TCPS_CLOSE_WAIT: 4195065Swnj case TCPS_CLOSING: 4205244Sroot case TCPS_LAST_ACK: 4215244Sroot case TCPS_TIME_WAIT: 4225085Swnj #define ourfinisacked (acked > 0) 4235085Swnj 4245244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4255065Swnj break; 4265300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4275065Swnj goto dropafterack; 4285085Swnj acked = ti->ti_ack - tp->snd_una; 4295307Sroot if (ti->ti_ack == tp->snd_max) 4305244Sroot tp->t_timer[TCPT_REXMT] = 0; 4315307Sroot else { 4325244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4335244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4345300Sroot tp->t_rtt = 0; 4355300Sroot tp->t_rxtshift = 0; 4365085Swnj } 4375307Sroot if (acked > so->so_snd.sb_cc) { 4385307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4395307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4405307Sroot } else { 4415307Sroot sbdrop(&so->so_snd.sb_cc, acked); 4425307Sroot tp->snd_wnd -= acked; 4435307Sroot acked = 0; 4445307Sroot } 4455300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4465300Sroot sowwakeup(so); 4475231Swnj tp->snd_una = ti->ti_ack; 4485357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4495357Sroot tp->snd_nxt = tp->snd_una; 4505162Swnj 4515162Swnj /* 4525162Swnj * If transmit timer is running and timed sequence 4535162Swnj * number was acked, update smoothed round trip time. 4545162Swnj */ 4555162Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4565244Sroot if (tp->t_srtt == 0) 4575244Sroot tp->t_srtt = tp->t_rtt; 4585244Sroot else 4595244Sroot tp->t_srtt = 4605244Sroot tcp_alpha * tp->t_srtt + 4615244Sroot (1 - tcp_alpha) * tp->t_rtt; 4625162Swnj tp->t_rtt = 0; 4635162Swnj } 4645162Swnj 4654601Swnj switch (tp->t_state) { 4664601Swnj 4675065Swnj /* 4685065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4695065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4705085Swnj * then enter FIN_WAIT_2. 4715065Swnj */ 4725065Swnj case TCPS_FIN_WAIT_1: 473*5896Swnj if (ourfinisacked) { 474*5896Swnj /* 475*5896Swnj * If we can't receive any more 476*5896Swnj * data, then closing user can proceed. 477*5896Swnj */ 478*5896Swnj if (so->so_state & SS_CANTRCVMORE) 479*5896Swnj soisdisconnected(so); 4805085Swnj tp->t_state = TCPS_FIN_WAIT_2; 481*5896Swnj } 4824601Swnj break; 4834601Swnj 4845065Swnj /* 4855065Swnj * In CLOSING STATE in addition to the processing for 4865065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4875065Swnj * then enter the TIME-WAIT state, otherwise ignore 4885065Swnj * the segment. 4895065Swnj */ 4905065Swnj case TCPS_CLOSING: 4915244Sroot if (ourfinisacked) { 4925065Swnj tp->t_state = TCPS_TIME_WAIT; 4935244Sroot tcp_canceltimers(tp); 4945244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4955244Sroot soisdisconnected(so); 4965244Sroot } 4975244Sroot break; 4984601Swnj 4995065Swnj /* 5005085Swnj * The only thing that can arrive in LAST_ACK state 5015085Swnj * is an acknowledgment of our FIN. If our FIN is now 5025085Swnj * acknowledged, delete the TCB, enter the closed state 5035085Swnj * and return. 5045065Swnj */ 5055065Swnj case TCPS_LAST_ACK: 5065251Sroot if (ourfinisacked) 5075065Swnj tcp_close(tp); 5085065Swnj goto drop; 5094601Swnj 5105065Swnj /* 5115065Swnj * In TIME_WAIT state the only thing that should arrive 5125065Swnj * is a retransmission of the remote FIN. Acknowledge 5135065Swnj * it and restart the finack timer. 5145065Swnj */ 5155065Swnj case TCPS_TIME_WAIT: 5165162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5175065Swnj goto dropafterack; 5184601Swnj } 5195085Swnj #undef ourfinisacked 5205085Swnj } 5214601Swnj 5225065Swnj step6: 5235065Swnj /* 5245244Sroot * Update window information. 5255244Sroot */ 5265300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5275391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5285300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5295391Swnj /* 5305391Swnj 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); 5315391Swnj */ 5325244Sroot tp->snd_wnd = ti->ti_win; 5335244Sroot tp->snd_wl1 = ti->ti_seq; 5345244Sroot tp->snd_wl2 = ti->ti_ack; 5355244Sroot if (tp->snd_wnd > 0) 5365244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5375244Sroot } 5385244Sroot 5395244Sroot /* 5405547Swnj * Process segments with URG. 5415065Swnj */ 5425547Swnj if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5435547Swnj /* 5445547Swnj * If this segment advances the known urgent pointer, 5455547Swnj * then mark the data stream. This should not happen 5465547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5475547Swnj * a FIN has been received from the remote side. 5485547Swnj * In these states we ignore the URG. 5495547Swnj */ 5505547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5515547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 5525547Swnj so->so_oobmark = so->so_rcv.sb_cc + 5535547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 5545547Swnj if (so->so_oobmark == 0) 5555547Swnj so->so_state |= SS_RCVATMARK; 5565440Swnj #ifdef TCPTRUEOOB 5575547Swnj if ((tp->t_flags & TF_DOOOB) == 0) 5585440Swnj #endif 5595547Swnj sohasoutofband(so); 5605547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 5615440Swnj } 5625547Swnj /* 5635547Swnj * Remove out of band data so doesn't get presented to user. 5645547Swnj * This can happen independent of advancing the URG pointer, 5655547Swnj * but if two URG's are pending at once, some out-of-band 5665547Swnj * data may creep in... ick. 5675547Swnj */ 5685547Swnj if (ti->ti_urp <= ti->ti_len) { 5695547Swnj tcp_pulloutofband(so, ti); 5705547Swnj } 5715419Swnj } 5724601Swnj 5734601Swnj /* 5745065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5755065Swnj * and arranging for acknowledgment of receipt if necessary. 5765065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5775065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5785065Swnj * case PRU_RCVD). If a FIN has already been received on this 5795065Swnj * connection then we just ignore the text. 5804601Swnj */ 5815263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 5825263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5835085Swnj off += sizeof (struct ip); /* drop IP header */ 5845085Swnj m->m_off += off; 5855085Swnj m->m_len -= off; 5865065Swnj tiflags = tcp_reass(tp, ti); 5875440Swnj if (tcpnodelack == 0) 5885440Swnj tp->t_flags |= TF_DELACK; 5895440Swnj else 5905440Swnj tp->t_flags |= TF_ACKNOW; 5915244Sroot } else { 5924924Swnj m_freem(m); 5935263Swnj tiflags &= ~TH_FIN; 5945244Sroot } 5954601Swnj 5964601Swnj /* 5975263Swnj * If FIN is received ACK the FIN and let the user know 5985263Swnj * that the connection is closing. 5994601Swnj */ 6005263Swnj if (tiflags & TH_FIN) { 6015244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6025244Sroot socantrcvmore(so); 6035244Sroot tp->t_flags |= TF_ACKNOW; 6045244Sroot tp->rcv_nxt++; 6055244Sroot } 6065065Swnj switch (tp->t_state) { 6074601Swnj 6085065Swnj /* 6095065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6105065Swnj * enter the CLOSE_WAIT state. 6114884Swnj */ 6125065Swnj case TCPS_SYN_RECEIVED: 6135065Swnj case TCPS_ESTABLISHED: 6145065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6155065Swnj break; 6164884Swnj 6175065Swnj /* 6185085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6195085Swnj * enter the CLOSING state. 6204884Swnj */ 6215065Swnj case TCPS_FIN_WAIT_1: 6225085Swnj tp->t_state = TCPS_CLOSING; 6235065Swnj break; 6244601Swnj 6255065Swnj /* 6265065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6275065Swnj * starting the time-wait timer, turning off the other 6285065Swnj * standard timers. 6295065Swnj */ 6305065Swnj case TCPS_FIN_WAIT_2: 6315244Sroot tp->t_state = TCPS_TIME_WAIT; 6325074Swnj tcp_canceltimers(tp); 6335162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6345244Sroot soisdisconnected(so); 6355065Swnj break; 6365065Swnj 6374884Swnj /* 6385065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6394884Swnj */ 6405065Swnj case TCPS_TIME_WAIT: 6415162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6425065Swnj break; 6435085Swnj } 6444601Swnj } 6455267Sroot if (so->so_options & SO_DEBUG) 6465267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6475085Swnj 6485085Swnj /* 6495085Swnj * Return any desired output. 6505085Swnj */ 6515085Swnj tcp_output(tp); 6525065Swnj return; 6535085Swnj 6545065Swnj dropafterack: 6555085Swnj /* 6565244Sroot * Generate an ACK dropping incoming segment. 6575085Swnj * Make ACK reflect our state. 6585085Swnj */ 6595085Swnj if (tiflags & TH_RST) 6605085Swnj goto drop; 6615391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6625231Swnj return; 6635085Swnj 6645085Swnj dropwithreset: 6655440Swnj if (om) 6665440Swnj m_free(om); 6675085Swnj /* 6685244Sroot * Generate a RST, dropping incoming segment. 6695085Swnj * Make ACK acceptable to originator of segment. 6705085Swnj */ 6715085Swnj if (tiflags & TH_RST) 6725085Swnj goto drop; 6735085Swnj if (tiflags & TH_ACK) 6745391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6755085Swnj else { 6765085Swnj if (tiflags & TH_SYN) 6775085Swnj ti->ti_len++; 6785391Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6795085Swnj } 6805231Swnj return; 6815085Swnj 6825065Swnj drop: 6835085Swnj /* 6845085Swnj * Drop space held by incoming segment and return. 6855085Swnj */ 6865065Swnj m_freem(m); 6875267Sroot return; 6885065Swnj } 6895065Swnj 6905440Swnj tcp_dooptions(tp, om) 6915440Swnj struct tcpcb *tp; 6925440Swnj struct mbuf *om; 6935419Swnj { 6945440Swnj register u_char *cp; 6955440Swnj int opt, optlen, cnt; 6965419Swnj 6975440Swnj cp = mtod(om, u_char *); 6985440Swnj cnt = om->m_len; 6995440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7005440Swnj opt = cp[0]; 7015440Swnj if (opt == TCPOPT_EOL) 7025440Swnj break; 7035440Swnj if (opt == TCPOPT_NOP) 7045440Swnj optlen = 1; 7055440Swnj else 7065440Swnj optlen = cp[1]; 7075440Swnj switch (opt) { 7085440Swnj 7095440Swnj default: 7105440Swnj break; 7115440Swnj 7125440Swnj case TCPOPT_MAXSEG: 7135440Swnj if (optlen != 4) 7145440Swnj continue; 7155440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7165440Swnj #if vax 7175440Swnj tp->t_maxseg = ntohs(tp->t_maxseg); 7185440Swnj #endif 7195440Swnj break; 7205440Swnj 7215440Swnj #ifdef TCPTRUEOOB 7225440Swnj case TCPOPT_WILLOOB: 7235440Swnj tp->t_flags |= TF_DOOOB; 7245440Swnj printf("tp %x dooob\n", tp); 7255440Swnj break; 7265440Swnj 7275440Swnj case TCPOPT_OOBDATA: { 7285440Swnj int seq; 7295547Swnj register struct socket *so = tp->t_inpcb->inp_socket; 7305547Swnj tcp_seq mark; 7315440Swnj 7325547Swnj if (optlen != 8) 7335440Swnj continue; 7345440Swnj seq = cp[2]; 7355440Swnj if (seq < tp->t_iobseq) 7365440Swnj seq += 256; 7375440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 7385440Swnj if (seq - tp->t_iobseq > 128) { 7395440Swnj printf("bad seq\n"); 7405440Swnj tp->t_oobflags |= TCPOOB_OWEACK; 7415440Swnj break; 7425440Swnj } 7435440Swnj tp->t_iobseq = cp[2]; 7445440Swnj tp->t_iobc = cp[3]; 7455547Swnj mark = *(tcp_seq *)(cp + 4); 7465547Swnj #if vax 7475547Swnj mark = ntohl(mark); 7485547Swnj #endif 7495547Swnj so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 7505547Swnj if (so->so_oobmark == 0) 7515547Swnj so->so_state |= SS_RCVATMARK; 7525440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 7535547Swnj sohasoutofband(so); 7545440Swnj break; 7555419Swnj } 7565440Swnj 7575440Swnj case TCPOPT_OOBACK: { 7585440Swnj int seq; 7595440Swnj 7605440Swnj if (optlen != 4) 7615440Swnj continue; 7625440Swnj if (tp->t_oobseq != cp[2]) { 7635440Swnj printf("wrong ack\n"); 7645440Swnj break; 7655440Swnj } 7665440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]); 7675440Swnj tp->t_oobflags &= ~TCPOOB_NEEDACK; 7685440Swnj tp->t_timer[TCPT_OOBREXMT] = 0; 7695419Swnj break; 7705440Swnj } 7715440Swnj #endif TCPTRUEOOB 7725440Swnj } 7735419Swnj } 7745440Swnj m_free(om); 7755419Swnj } 7765419Swnj 7775419Swnj /* 7785547Swnj * Pull out of band byte out of a segment so 7795547Swnj * it doesn't appear in the user's data queue. 7805547Swnj * It is still reflected in the segment length for 7815547Swnj * sequencing purposes. 7825547Swnj */ 7835547Swnj tcp_pulloutofband(so, ti) 7845547Swnj struct socket *so; 7855547Swnj struct tcpiphdr *ti; 7865547Swnj { 7875547Swnj register struct mbuf *m; 7885547Swnj int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1; 7895547Swnj 7905547Swnj m = dtom(ti); 7915547Swnj while (cnt >= 0) { 7925547Swnj if (m->m_len > cnt) { 7935547Swnj char *cp = mtod(m, caddr_t) + cnt; 7945547Swnj struct tcpcb *tp = sototcpcb(so); 7955547Swnj 7965547Swnj tp->t_iobc = *cp; 7975547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 7985547Swnj bcopy(cp+1, cp, m->m_len - cnt - 1); 7995547Swnj m->m_len--; 8005547Swnj return; 8015547Swnj } 8025547Swnj cnt -= m->m_len; 8035547Swnj m = m->m_next; 8045547Swnj if (m == 0) 8055547Swnj break; 8065547Swnj } 8075547Swnj panic("tcp_pulloutofband"); 8085547Swnj } 8095547Swnj 8105547Swnj /* 8115065Swnj * Insert segment ti into reassembly queue of tcp with 8125065Swnj * control block tp. Return TH_FIN if reassembly now includes 8135065Swnj * a segment with FIN. 8145065Swnj */ 8155109Swnj tcp_reass(tp, ti) 8165065Swnj register struct tcpcb *tp; 8175065Swnj register struct tcpiphdr *ti; 8185065Swnj { 8195065Swnj register struct tcpiphdr *q; 8205085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8215263Swnj struct mbuf *m; 8225263Swnj int flags; 8235085Swnj COUNT(TCP_REASS); 8245065Swnj 8255065Swnj /* 8265162Swnj * Call with ti==0 after become established to 8275162Swnj * force pre-ESTABLISHED data up to user socket. 8285065Swnj */ 8295162Swnj if (ti == 0) 8305065Swnj goto present; 8314601Swnj 8325065Swnj /* 8335065Swnj * Find a segment which begins after this one does. 8345065Swnj */ 8355065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8365065Swnj q = (struct tcpiphdr *)q->ti_next) 8375065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8385065Swnj break; 8394601Swnj 8405065Swnj /* 8415065Swnj * If there is a preceding segment, it may provide some of 8425065Swnj * our data already. If so, drop the data from the incoming 8435065Swnj * segment. If it provides all of our data, drop us. 8445065Swnj */ 8455065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8465065Swnj register int i; 8475690Swnj q = (struct tcpiphdr *)q->ti_prev; 8485065Swnj /* conversion to int (in i) handles seq wraparound */ 8495065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8505065Swnj if (i > 0) { 8514924Swnj if (i >= ti->ti_len) 8525065Swnj goto drop; 8535065Swnj m_adj(dtom(tp), i); 8545065Swnj ti->ti_len -= i; 8554924Swnj ti->ti_seq += i; 8564601Swnj } 8575065Swnj q = (struct tcpiphdr *)(q->ti_next); 8585065Swnj } 8594601Swnj 8605065Swnj /* 8615065Swnj * While we overlap succeeding segments trim them or, 8625065Swnj * if they are completely covered, dequeue them. 8635065Swnj */ 8645690Swnj while (q != (struct tcpiphdr *)tp) { 8655065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8665690Swnj if (i <= 0) 8675690Swnj break; 8685065Swnj if (i < q->ti_len) { 8695690Swnj q->ti_seq += i; 8705065Swnj q->ti_len -= i; 8715065Swnj m_adj(dtom(q), i); 8725065Swnj break; 8734601Swnj } 8745065Swnj q = (struct tcpiphdr *)q->ti_next; 8755623Swnj m = dtom(q->ti_prev); 8765065Swnj remque(q->ti_prev); 8775623Swnj m_freem(m); 8785065Swnj } 8794601Swnj 8805065Swnj /* 8815065Swnj * Stick new segment in its place. 8825065Swnj */ 8835065Swnj insque(ti, q->ti_prev); 8844601Swnj 8855065Swnj present: 8865065Swnj /* 8875244Sroot * Present data to user, advancing rcv_nxt through 8885244Sroot * completed sequence space. 8895065Swnj */ 8905263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 8915244Sroot return (0); 8924924Swnj ti = tp->seg_next; 8935263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 8945263Swnj return (0); 8955263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 8965263Swnj return (0); 8975263Swnj do { 8985244Sroot tp->rcv_nxt += ti->ti_len; 8995244Sroot flags = ti->ti_flags & TH_FIN; 9004924Swnj remque(ti); 9015263Swnj m = dtom(ti); 9024924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9035263Swnj if (so->so_state & SS_CANTRCVMORE) 9045263Swnj (void) m_freem(m); 9055263Swnj else 9065263Swnj sbappend(&so->so_rcv, m); 9075263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9085263Swnj sorwakeup(so); 9095065Swnj return (flags); 9105065Swnj drop: 9115065Swnj m_freem(dtom(ti)); 9125263Swnj return (0); 9134601Swnj } 914