1*5440Swnj /* tcp_input.c 1.49 82/01/17 */ 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; 28*5440Swnj 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; 41*5440Swnj 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, 87*5440Swnj * 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; 95*5440Swnj if (off > sizeof (struct tcphdr)) { 96*5440Swnj if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 97*5440Swnj tcpstat.tcps_hdrops++; 98*5440Swnj goto drop; 99*5440Swnj } 100*5440Swnj ti = mtod(m, struct tcpiphdr *); 101*5440Swnj om = m_get(M_DONTWAIT); 102*5440Swnj if (om == 0) 103*5440Swnj goto drop; 104*5440Swnj om->m_off = MMINOFF; 105*5440Swnj om->m_len = off - sizeof (struct tcphdr); 106*5440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 107*5440Swnj bcopy(op, mtod(om, caddr_t), om->m_len); 108*5440Swnj m->m_len -= om->m_len; 109*5440Swnj bcopy(op+om->m_len, op, m->m_len-sizeof (struct tcpiphdr)); 110*5440Swnj } 111*5440Swnj } 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 /* 153*5440Swnj * Process options. 154*5440Swnj */ 155*5440Swnj if (om) { 156*5440Swnj tcp_dooptions(tp, om); 157*5440Swnj om = 0; 158*5440Swnj } 159*5440Swnj 160*5440Swnj /* 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 /* 5345419Swnj * If an URG bit is set and in the segment and is greater than the 535*5440Swnj * current known urgent pointer, then mark the data stream. 536*5440Swnj * If the TCP is not doing out-of-band data, then indicate 537*5440Swnj * urgent to the user. This should not happen 5385065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 5395065Swnj * a FIN has been received from the remote side. In these states 5405065Swnj * we ignore the URG. 5415065Swnj */ 5425419Swnj if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0 && 5435419Swnj SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5445419Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 5455419Swnj so->so_oobmark = so->so_rcv.sb_cc + 5465419Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 5475419Swnj if (so->so_oobmark == 0) 5485419Swnj so->so_state |= SS_RCVATMARK; 549*5440Swnj #ifdef TCPTRUEOOB 550*5440Swnj if ((tp->t_flags & TF_DOOOB) == 0) 551*5440Swnj #endif 552*5440Swnj { 553*5440Swnj sohasoutofband(so); 554*5440Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 555*5440Swnj } 556*5440Swnj 5575419Swnj } 5584601Swnj 5594601Swnj /* 5605065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5615065Swnj * and arranging for acknowledgment of receipt if necessary. 5625065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5635065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5645065Swnj * case PRU_RCVD). If a FIN has already been received on this 5655065Swnj * connection then we just ignore the text. 5664601Swnj */ 5675263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 5685263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5695085Swnj off += sizeof (struct ip); /* drop IP header */ 5705085Swnj m->m_off += off; 5715085Swnj m->m_len -= off; 5725065Swnj tiflags = tcp_reass(tp, ti); 573*5440Swnj if (tcpnodelack == 0) 574*5440Swnj tp->t_flags |= TF_DELACK; 575*5440Swnj else 576*5440Swnj tp->t_flags |= TF_ACKNOW; 5775244Sroot } else { 5784924Swnj m_freem(m); 5795263Swnj tiflags &= ~TH_FIN; 5805244Sroot } 5814601Swnj 5824601Swnj /* 5835263Swnj * If FIN is received ACK the FIN and let the user know 5845263Swnj * that the connection is closing. 5854601Swnj */ 5865263Swnj if (tiflags & TH_FIN) { 5875244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5885244Sroot socantrcvmore(so); 5895244Sroot tp->t_flags |= TF_ACKNOW; 5905244Sroot tp->rcv_nxt++; 5915244Sroot } 5925065Swnj switch (tp->t_state) { 5934601Swnj 5945065Swnj /* 5955065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 5965065Swnj * enter the CLOSE_WAIT state. 5974884Swnj */ 5985065Swnj case TCPS_SYN_RECEIVED: 5995065Swnj case TCPS_ESTABLISHED: 6005065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6015065Swnj break; 6024884Swnj 6035065Swnj /* 6045085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6055085Swnj * enter the CLOSING state. 6064884Swnj */ 6075065Swnj case TCPS_FIN_WAIT_1: 6085085Swnj tp->t_state = TCPS_CLOSING; 6095065Swnj break; 6104601Swnj 6115065Swnj /* 6125065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6135065Swnj * starting the time-wait timer, turning off the other 6145065Swnj * standard timers. 6155065Swnj */ 6165065Swnj case TCPS_FIN_WAIT_2: 6175244Sroot tp->t_state = TCPS_TIME_WAIT; 6185074Swnj tcp_canceltimers(tp); 6195162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6205244Sroot soisdisconnected(so); 6215065Swnj break; 6225065Swnj 6234884Swnj /* 6245065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6254884Swnj */ 6265065Swnj case TCPS_TIME_WAIT: 6275162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6285065Swnj break; 6295085Swnj } 6304601Swnj } 6315267Sroot if (so->so_options & SO_DEBUG) 6325267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6335085Swnj 6345085Swnj /* 6355085Swnj * Return any desired output. 6365085Swnj */ 6375085Swnj tcp_output(tp); 6385065Swnj return; 6395085Swnj 6405065Swnj dropafterack: 6415085Swnj /* 6425244Sroot * Generate an ACK dropping incoming segment. 6435085Swnj * Make ACK reflect our state. 6445085Swnj */ 6455085Swnj if (tiflags & TH_RST) 6465085Swnj goto drop; 6475391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6485231Swnj return; 6495085Swnj 6505085Swnj dropwithreset: 651*5440Swnj if (om) 652*5440Swnj m_free(om); 6535085Swnj /* 6545244Sroot * Generate a RST, dropping incoming segment. 6555085Swnj * Make ACK acceptable to originator of segment. 6565085Swnj */ 6575085Swnj if (tiflags & TH_RST) 6585085Swnj goto drop; 6595085Swnj if (tiflags & TH_ACK) 6605391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6615085Swnj else { 6625085Swnj if (tiflags & TH_SYN) 6635085Swnj ti->ti_len++; 6645391Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6655085Swnj } 6665231Swnj return; 6675085Swnj 6685065Swnj drop: 6695085Swnj /* 6705085Swnj * Drop space held by incoming segment and return. 6715085Swnj */ 6725065Swnj m_freem(m); 6735267Sroot return; 6745065Swnj } 6755065Swnj 676*5440Swnj tcp_dooptions(tp, om) 677*5440Swnj struct tcpcb *tp; 678*5440Swnj struct mbuf *om; 6795419Swnj { 680*5440Swnj register u_char *cp; 681*5440Swnj int opt, optlen, cnt; 6825419Swnj 683*5440Swnj cp = mtod(om, u_char *); 684*5440Swnj cnt = om->m_len; 685*5440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 686*5440Swnj opt = cp[0]; 687*5440Swnj if (opt == TCPOPT_EOL) 688*5440Swnj break; 689*5440Swnj if (opt == TCPOPT_NOP) 690*5440Swnj optlen = 1; 691*5440Swnj else 692*5440Swnj optlen = cp[1]; 693*5440Swnj switch (opt) { 694*5440Swnj 695*5440Swnj default: 696*5440Swnj break; 697*5440Swnj 698*5440Swnj case TCPOPT_MAXSEG: 699*5440Swnj if (optlen != 4) 700*5440Swnj continue; 701*5440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 702*5440Swnj #if vax 703*5440Swnj tp->t_maxseg = ntohs(tp->t_maxseg); 704*5440Swnj #endif 705*5440Swnj break; 706*5440Swnj 707*5440Swnj #ifdef TCPTRUEOOB 708*5440Swnj case TCPOPT_WILLOOB: 709*5440Swnj tp->t_flags |= TF_DOOOB; 710*5440Swnj printf("tp %x dooob\n", tp); 711*5440Swnj break; 712*5440Swnj 713*5440Swnj case TCPOPT_OOBDATA: { 714*5440Swnj int seq; 715*5440Swnj 716*5440Swnj if (optlen != 4) 717*5440Swnj continue; 718*5440Swnj seq = cp[2]; 719*5440Swnj if (seq < tp->t_iobseq) 720*5440Swnj seq += 256; 721*5440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 722*5440Swnj if (seq - tp->t_iobseq > 128) { 723*5440Swnj printf("bad seq\n"); 724*5440Swnj tp->t_oobflags |= TCPOOB_OWEACK; 725*5440Swnj break; 726*5440Swnj } 727*5440Swnj tp->t_iobseq = cp[2]; 728*5440Swnj tp->t_iobc = cp[3]; 729*5440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 730*5440Swnj sohasoutofband(tp->t_inpcb->inp_socket); 731*5440Swnj break; 7325419Swnj } 733*5440Swnj 734*5440Swnj case TCPOPT_OOBACK: { 735*5440Swnj int seq; 736*5440Swnj 737*5440Swnj if (optlen != 4) 738*5440Swnj continue; 739*5440Swnj if (tp->t_oobseq != cp[2]) { 740*5440Swnj printf("wrong ack\n"); 741*5440Swnj break; 742*5440Swnj } 743*5440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]); 744*5440Swnj tp->t_oobflags &= ~TCPOOB_NEEDACK; 745*5440Swnj tp->t_timer[TCPT_OOBREXMT] = 0; 7465419Swnj break; 747*5440Swnj } 748*5440Swnj #endif TCPTRUEOOB 749*5440Swnj } 7505419Swnj } 751*5440Swnj m_free(om); 7525419Swnj } 7535419Swnj 7545419Swnj /* 7555065Swnj * Insert segment ti into reassembly queue of tcp with 7565065Swnj * control block tp. Return TH_FIN if reassembly now includes 7575065Swnj * a segment with FIN. 7585065Swnj */ 7595109Swnj tcp_reass(tp, ti) 7605065Swnj register struct tcpcb *tp; 7615065Swnj register struct tcpiphdr *ti; 7625065Swnj { 7635065Swnj register struct tcpiphdr *q; 7645085Swnj struct socket *so = tp->t_inpcb->inp_socket; 7655263Swnj struct mbuf *m; 7665263Swnj int flags; 7675085Swnj COUNT(TCP_REASS); 7685065Swnj 7695065Swnj /* 7705162Swnj * Call with ti==0 after become established to 7715162Swnj * force pre-ESTABLISHED data up to user socket. 7725065Swnj */ 7735162Swnj if (ti == 0) 7745065Swnj goto present; 7754601Swnj 7765065Swnj /* 7775065Swnj * Find a segment which begins after this one does. 7785065Swnj */ 7795065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 7805065Swnj q = (struct tcpiphdr *)q->ti_next) 7815065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 7825065Swnj break; 7834601Swnj 7845065Swnj /* 7855065Swnj * If there is a preceding segment, it may provide some of 7865065Swnj * our data already. If so, drop the data from the incoming 7875065Swnj * segment. If it provides all of our data, drop us. 7885065Swnj */ 7895065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 7905065Swnj register int i; 7915065Swnj q = (struct tcpiphdr *)(q->ti_prev); 7925065Swnj /* conversion to int (in i) handles seq wraparound */ 7935065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 7945065Swnj if (i > 0) { 7954924Swnj if (i >= ti->ti_len) 7965065Swnj goto drop; 7975065Swnj m_adj(dtom(tp), i); 7985065Swnj ti->ti_len -= i; 7994924Swnj ti->ti_seq += i; 8004601Swnj } 8015065Swnj q = (struct tcpiphdr *)(q->ti_next); 8025065Swnj } 8034601Swnj 8045065Swnj /* 8055065Swnj * While we overlap succeeding segments trim them or, 8065065Swnj * if they are completely covered, dequeue them. 8075065Swnj */ 8085065Swnj while (q != (struct tcpiphdr *)tp && 8095065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 8105065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8115065Swnj if (i < q->ti_len) { 8125065Swnj q->ti_len -= i; 8135065Swnj m_adj(dtom(q), i); 8145065Swnj break; 8154601Swnj } 8165065Swnj q = (struct tcpiphdr *)q->ti_next; 8175065Swnj m_freem(dtom(q->ti_prev)); 8185065Swnj remque(q->ti_prev); 8195065Swnj } 8204601Swnj 8215065Swnj /* 8225065Swnj * Stick new segment in its place. 8235065Swnj */ 8245065Swnj insque(ti, q->ti_prev); 8254601Swnj 8265065Swnj present: 8275065Swnj /* 8285244Sroot * Present data to user, advancing rcv_nxt through 8295244Sroot * completed sequence space. 8305065Swnj */ 8315263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 8325244Sroot return (0); 8334924Swnj ti = tp->seg_next; 8345263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 8355263Swnj return (0); 8365263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 8375263Swnj return (0); 8385263Swnj do { 8395244Sroot tp->rcv_nxt += ti->ti_len; 8405244Sroot flags = ti->ti_flags & TH_FIN; 8414924Swnj remque(ti); 8425263Swnj m = dtom(ti); 8434924Swnj ti = (struct tcpiphdr *)ti->ti_next; 8445263Swnj if (so->so_state & SS_CANTRCVMORE) 8455263Swnj (void) m_freem(m); 8465263Swnj else 8475263Swnj sbappend(&so->so_rcv, m); 8485263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 8495263Swnj sorwakeup(so); 8505065Swnj return (flags); 8515065Swnj drop: 8525065Swnj m_freem(dtom(ti)); 8535263Swnj return (0); 8544601Swnj } 855