1*5419Swnj /* tcp_input.c 1.48 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; 284601Swnj 295267Sroot struct tcpcb *tcp_newtcpcb(); 305065Swnj /* 315065Swnj * TCP input routine, follows pages 65-76 of the 325065Swnj * protocol specification dated September, 1981 very closely. 335065Swnj */ 344924Swnj tcp_input(m0) 354924Swnj struct mbuf *m0; 364601Swnj { 374924Swnj register struct tcpiphdr *ti; 384924Swnj struct inpcb *inp; 394924Swnj register struct mbuf *m; 404924Swnj int len, tlen, off; 415391Swnj register struct tcpcb *tp = 0; 424924Swnj register int tiflags; 434803Swnj struct socket *so; 445109Swnj int todrop, acked; 455267Sroot short ostate; 464924Swnj 474601Swnj COUNT(TCP_INPUT); 484924Swnj /* 495244Sroot * Get IP and TCP header together in first mbuf. 505244Sroot * Note: IP leaves IP header in first mbuf. 514924Swnj */ 524924Swnj m = m0; 535020Sroot ti = mtod(m, struct tcpiphdr *); 545244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 555208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 565307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 575307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 585085Swnj tcpstat.tcps_hdrops++; 595307Sroot return; 605085Swnj } 615085Swnj ti = mtod(m, struct tcpiphdr *); 625085Swnj } 634601Swnj 644601Swnj /* 655244Sroot * Checksum extended TCP header and data. 664601Swnj */ 674924Swnj tlen = ((struct ip *)ti)->ip_len; 684924Swnj len = sizeof (struct ip) + tlen; 694679Swnj if (tcpcksum) { 704924Swnj ti->ti_next = ti->ti_prev = 0; 714924Swnj ti->ti_x1 = 0; 725223Swnj ti->ti_len = (u_short)tlen; 735223Swnj #if vax 745223Swnj ti->ti_len = htons(ti->ti_len); 755223Swnj #endif 765231Swnj if (ti->ti_sum = in_cksum(m, len)) { 774924Swnj tcpstat.tcps_badsum++; 785065Swnj printf("tcp cksum %x\n", ti->ti_sum); 795085Swnj goto drop; 804601Swnj } 814601Swnj } 824601Swnj 834601Swnj /* 845244Sroot * Check that TCP offset makes sense, 855244Sroot * process TCP options and adjust length. 864601Swnj */ 874924Swnj off = ti->ti_off << 2; 885231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 894924Swnj tcpstat.tcps_badoff++; 905085Swnj goto drop; 914924Swnj } 924924Swnj ti->ti_len = tlen - off; 935085Swnj #if 0 945231Swnj if (off > sizeof (struct tcphdr)) 955085Swnj tcp_options(ti); 965085Swnj #endif 975065Swnj tiflags = ti->ti_flags; 984924Swnj 995231Swnj #if vax 1004924Swnj /* 1015244Sroot * Convert TCP protocol specific fields to host format. 1025085Swnj */ 1035085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1045085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1055085Swnj ti->ti_win = ntohs(ti->ti_win); 1065085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1075231Swnj #endif 1085085Swnj 1095085Swnj /* 1104924Swnj * Locate pcb for segment. 1114924Swnj */ 1125065Swnj inp = in_pcblookup 1135065Swnj (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 1145065Swnj 1155065Swnj /* 1165065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1175244Sroot * all data in the incoming segment is discarded. 1185065Swnj */ 1195300Sroot if (inp == 0) 1205085Swnj goto dropwithreset; 1215065Swnj tp = intotcpcb(inp); 1225300Sroot if (tp == 0) 1235085Swnj goto dropwithreset; 1245109Swnj so = inp->inp_socket; 1255267Sroot if (so->so_options & SO_DEBUG) { 1265267Sroot ostate = tp->t_state; 1275267Sroot tcp_saveti = *ti; 1285267Sroot } 1294601Swnj 1304601Swnj /* 1315162Swnj * Segment received on connection. 1325162Swnj * Reset idle time and keep-alive timer. 1335162Swnj */ 1345162Swnj tp->t_idle = 0; 1355162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1365162Swnj 1375162Swnj /* 1385085Swnj * Calculate amount of space in receive window, 1395085Swnj * and then do TCP input processing. 1404601Swnj */ 1415085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1425231Swnj if (tp->rcv_wnd < 0) 1435231Swnj tp->rcv_wnd = 0; 1444601Swnj 1454601Swnj switch (tp->t_state) { 1464601Swnj 1475065Swnj /* 1485065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1495065Swnj * If the segment contains an ACK then it is bad and send a RST. 1505065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1515085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1525065Swnj * tp->iss, and send a segment: 1535085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1545065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1555065Swnj * Fill in remote peer address fields if not previously specified. 1565065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1575244Sroot * segment in this state. 1585065Swnj */ 1595065Swnj case TCPS_LISTEN: 1605065Swnj if (tiflags & TH_RST) 1615065Swnj goto drop; 1625300Sroot if (tiflags & TH_ACK) 1635085Swnj goto dropwithreset; 1645300Sroot if ((tiflags & TH_SYN) == 0) 1655065Swnj goto drop; 1665244Sroot tcp_in.sin_addr = ti->ti_src; 1675244Sroot tcp_in.sin_port = ti->ti_sport; 1685300Sroot if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in)) 1695244Sroot goto drop; 1705244Sroot tp->t_template = tcp_template(tp); 1715244Sroot if (tp->t_template == 0) { 1725244Sroot in_pcbdisconnect(inp); 1735244Sroot goto drop; 1745244Sroot } 1755085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1765065Swnj tp->irs = ti->ti_seq; 1775085Swnj tcp_sendseqinit(tp); 1785085Swnj tcp_rcvseqinit(tp); 1795065Swnj tp->t_state = TCPS_SYN_RECEIVED; 1805244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1815085Swnj goto trimthenstep6; 1824601Swnj 1835065Swnj /* 1845065Swnj * If the state is SYN_SENT: 1855065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 1865065Swnj * if seg contains a RST, then drop the connection. 1875065Swnj * if seg does not contain SYN, then drop it. 1885065Swnj * Otherwise this is an acceptable SYN segment 1895065Swnj * initialize tp->rcv_nxt and tp->irs 1905065Swnj * if seg contains ack then advance tp->snd_una 1915065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1925065Swnj * arrange for segment to be acked (eventually) 1935065Swnj * continue processing rest of data/controls, beginning with URG 1945065Swnj */ 1955065Swnj case TCPS_SYN_SENT: 1965065Swnj if ((tiflags & TH_ACK) && 1975300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 1985300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 1995231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2005085Swnj goto dropwithreset; 2015065Swnj if (tiflags & TH_RST) { 2025065Swnj if (tiflags & TH_ACK) 2035267Sroot tcp_drop(tp, ECONNREFUSED); 2045065Swnj goto drop; 2054601Swnj } 2065065Swnj if ((tiflags & TH_SYN) == 0) 2075065Swnj goto drop; 2085231Swnj tp->snd_una = ti->ti_ack; 2095357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2105357Sroot tp->snd_nxt = tp->snd_una; 2115244Sroot tp->t_timer[TCPT_REXMT] = 0; 2125065Swnj tp->irs = ti->ti_seq; 2135085Swnj tcp_rcvseqinit(tp); 2145085Swnj tp->t_flags |= TF_ACKNOW; 2155162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2165391Swnj if (so->so_options & SO_ACCEPTCONN) 2175391Swnj so->so_state |= SS_CONNAWAITING; 2185244Sroot soisconnected(so); 2195065Swnj tp->t_state = TCPS_ESTABLISHED; 2205162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2215162Swnj } else 2225085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2235085Swnj goto trimthenstep6; 2245085Swnj 2255085Swnj trimthenstep6: 2265085Swnj /* 2275231Swnj * Advance ti->ti_seq to correspond to first data byte. 2285085Swnj * If data, trim to stay within window, 2295085Swnj * dropping FIN if necessary. 2305085Swnj */ 2315231Swnj ti->ti_seq++; 2325085Swnj if (ti->ti_len > tp->rcv_wnd) { 2335085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2345085Swnj m_adj(m, -todrop); 2355085Swnj ti->ti_len = tp->rcv_wnd; 2365085Swnj ti->ti_flags &= ~TH_FIN; 2375065Swnj } 2385263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2395085Swnj goto step6; 2405065Swnj } 2414601Swnj 2425065Swnj /* 2435065Swnj * States other than LISTEN or SYN_SENT. 2445065Swnj * First check that at least some bytes of segment are within 2455065Swnj * receive window. 2465065Swnj */ 2475065Swnj if (tp->rcv_wnd == 0) { 2485065Swnj /* 2495065Swnj * If window is closed can only take segments at 2505231Swnj * window edge, and have to drop data and PUSH from 2515065Swnj * incoming segments. 2525065Swnj */ 2535300Sroot if (tp->rcv_nxt != ti->ti_seq) 2545065Swnj goto dropafterack; 2555085Swnj if (ti->ti_len > 0) { 2565085Swnj ti->ti_len = 0; 2575085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2585065Swnj } 2595065Swnj } else { 2605065Swnj /* 2615231Swnj * If segment begins before rcv_nxt, drop leading 2625065Swnj * data (and SYN); if nothing left, just ack. 2635065Swnj */ 2645065Swnj if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 2655085Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2665085Swnj if (tiflags & TH_SYN) { 2675300Sroot tiflags &= ~TH_SYN; 2685085Swnj ti->ti_seq++; 2695085Swnj if (ti->ti_urp > 1) 2705085Swnj ti->ti_urp--; 2715085Swnj else 2725085Swnj tiflags &= ~TH_URG; 2735085Swnj todrop--; 2745085Swnj } 2755300Sroot if (todrop > ti->ti_len) 2765065Swnj goto dropafterack; 2775065Swnj m_adj(m, todrop); 2785065Swnj ti->ti_seq += todrop; 2795065Swnj ti->ti_len -= todrop; 2805085Swnj if (ti->ti_urp > todrop) 2815085Swnj ti->ti_urp -= todrop; 2825085Swnj else { 2835085Swnj tiflags &= ~TH_URG; 2845085Swnj /* ti->ti_flags &= ~TH_URG; */ 2855085Swnj /* ti->ti_urp = 0; */ 2865085Swnj } 2875085Swnj /* tiflags &= ~TH_SYN; */ 2885085Swnj /* ti->ti_flags &= ~TH_SYN; */ 2895065Swnj } 2905065Swnj /* 2915065Swnj * If segment ends after window, drop trailing data 2925085Swnj * (and PUSH and FIN); if nothing left, just ACK. 2935065Swnj */ 2945065Swnj if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 2955085Swnj todrop = 2965065Swnj ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 2975300Sroot if (todrop > ti->ti_len) 2985065Swnj goto dropafterack; 2995065Swnj m_adj(m, -todrop); 3005065Swnj ti->ti_len -= todrop; 3015085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3025065Swnj } 3035065Swnj } 3044601Swnj 3055065Swnj /* 3065065Swnj * If the RST bit is set examine the state: 3075065Swnj * SYN_RECEIVED STATE: 3085065Swnj * If passive open, return to LISTEN state. 3095065Swnj * If active open, inform user that connection was refused. 3105065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3115065Swnj * Inform user that connection was reset, and close tcb. 3125065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3135065Swnj * Close the tcb. 3145065Swnj */ 3155065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3165267Sroot 3175065Swnj case TCPS_SYN_RECEIVED: 3185065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3195267Sroot /* a miniature tcp_close, but invisible to user */ 3205267Sroot (void) m_free(dtom(tp->t_template)); 3215267Sroot (void) m_free(dtom(tp)); 3225267Sroot inp->inp_ppcb = 0; 3235267Sroot tp = tcp_newtcpcb(inp); 3245085Swnj tp->t_state = TCPS_LISTEN; 3255065Swnj goto drop; 3264601Swnj } 3275085Swnj tcp_drop(tp, ECONNREFUSED); 3285065Swnj goto drop; 3294601Swnj 3305065Swnj case TCPS_ESTABLISHED: 3315065Swnj case TCPS_FIN_WAIT_1: 3325065Swnj case TCPS_FIN_WAIT_2: 3335065Swnj case TCPS_CLOSE_WAIT: 3345065Swnj tcp_drop(tp, ECONNRESET); 3355065Swnj goto drop; 3365065Swnj 3375065Swnj case TCPS_CLOSING: 3385065Swnj case TCPS_LAST_ACK: 3395065Swnj case TCPS_TIME_WAIT: 3405065Swnj tcp_close(tp); 3415065Swnj goto drop; 3424601Swnj } 3434601Swnj 3444601Swnj /* 3455065Swnj * If a SYN is in the window, then this is an 3465065Swnj * error and we send an RST and drop the connection. 3474601Swnj */ 3485065Swnj if (tiflags & TH_SYN) { 3495231Swnj tcp_drop(tp, ECONNRESET); 3505085Swnj goto dropwithreset; 3514601Swnj } 3524601Swnj 3534601Swnj /* 3545065Swnj * If the ACK bit is off we drop the segment and return. 3554601Swnj */ 3565085Swnj if ((tiflags & TH_ACK) == 0) 3575065Swnj goto drop; 3585065Swnj 3595065Swnj /* 3605065Swnj * Ack processing. 3615065Swnj */ 3624601Swnj switch (tp->t_state) { 3634601Swnj 3645065Swnj /* 3655065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3665065Swnj * ESTABLISHED state and continue processing, othewise 3675065Swnj * send an RST. 3685065Swnj */ 3695065Swnj case TCPS_SYN_RECEIVED: 3705085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 3715231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 3725085Swnj goto dropwithreset; 3735244Sroot tp->snd_una++; /* SYN acked */ 3745357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 3755357Sroot tp->snd_nxt = tp->snd_una; 3765244Sroot tp->t_timer[TCPT_REXMT] = 0; 3775391Swnj if (so->so_options & SO_ACCEPTCONN) 3785391Swnj so->so_state |= SS_CONNAWAITING; 3795085Swnj soisconnected(so); 3805085Swnj tp->t_state = TCPS_ESTABLISHED; 3815162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 3825244Sroot tp->snd_wl1 = ti->ti_seq - 1; 3835085Swnj /* fall into ... */ 3844601Swnj 3855065Swnj /* 3865065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 3875065Swnj * ACKs. If the ack is in the range 3885231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 3895065Swnj * then advance tp->snd_una to ti->ti_ack and drop 3905065Swnj * data from the retransmission queue. If this ACK reflects 3915065Swnj * more up to date window information we update our window information. 3925065Swnj */ 3935065Swnj case TCPS_ESTABLISHED: 3945065Swnj case TCPS_FIN_WAIT_1: 3955065Swnj case TCPS_FIN_WAIT_2: 3965065Swnj case TCPS_CLOSE_WAIT: 3975065Swnj case TCPS_CLOSING: 3985244Sroot case TCPS_LAST_ACK: 3995244Sroot case TCPS_TIME_WAIT: 4005085Swnj #define ourfinisacked (acked > 0) 4015085Swnj 4025244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4035065Swnj break; 4045300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4055065Swnj goto dropafterack; 4065085Swnj acked = ti->ti_ack - tp->snd_una; 4075307Sroot if (ti->ti_ack == tp->snd_max) 4085244Sroot tp->t_timer[TCPT_REXMT] = 0; 4095307Sroot else { 4105244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4115244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4125300Sroot tp->t_rtt = 0; 4135300Sroot tp->t_rxtshift = 0; 4145085Swnj } 4155307Sroot if (acked > so->so_snd.sb_cc) { 4165307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4175307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4185307Sroot } else { 4195307Sroot sbdrop(&so->so_snd.sb_cc, acked); 4205307Sroot tp->snd_wnd -= acked; 4215307Sroot acked = 0; 4225307Sroot } 4235300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4245300Sroot sowwakeup(so); 4255231Swnj tp->snd_una = ti->ti_ack; 4265357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4275357Sroot tp->snd_nxt = tp->snd_una; 4285162Swnj 4295162Swnj /* 4305162Swnj * If transmit timer is running and timed sequence 4315162Swnj * number was acked, update smoothed round trip time. 4325162Swnj */ 4335162Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4345244Sroot if (tp->t_srtt == 0) 4355244Sroot tp->t_srtt = tp->t_rtt; 4365244Sroot else 4375244Sroot tp->t_srtt = 4385244Sroot tcp_alpha * tp->t_srtt + 4395244Sroot (1 - tcp_alpha) * tp->t_rtt; 4405162Swnj tp->t_rtt = 0; 4415162Swnj } 4425162Swnj 4434601Swnj switch (tp->t_state) { 4444601Swnj 4455065Swnj /* 4465065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4475065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4485085Swnj * then enter FIN_WAIT_2. 4495065Swnj */ 4505065Swnj case TCPS_FIN_WAIT_1: 4515085Swnj if (ourfinisacked) 4525085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4534601Swnj break; 4544601Swnj 4555065Swnj /* 4565065Swnj * In CLOSING STATE in addition to the processing for 4575065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4585065Swnj * then enter the TIME-WAIT state, otherwise ignore 4595065Swnj * the segment. 4605065Swnj */ 4615065Swnj case TCPS_CLOSING: 4625244Sroot if (ourfinisacked) { 4635065Swnj tp->t_state = TCPS_TIME_WAIT; 4645244Sroot tcp_canceltimers(tp); 4655244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4665244Sroot soisdisconnected(so); 4675244Sroot } 4685244Sroot break; 4694601Swnj 4705065Swnj /* 4715085Swnj * The only thing that can arrive in LAST_ACK state 4725085Swnj * is an acknowledgment of our FIN. If our FIN is now 4735085Swnj * acknowledged, delete the TCB, enter the closed state 4745085Swnj * and return. 4755065Swnj */ 4765065Swnj case TCPS_LAST_ACK: 4775251Sroot if (ourfinisacked) 4785065Swnj tcp_close(tp); 4795065Swnj goto drop; 4804601Swnj 4815065Swnj /* 4825065Swnj * In TIME_WAIT state the only thing that should arrive 4835065Swnj * is a retransmission of the remote FIN. Acknowledge 4845065Swnj * it and restart the finack timer. 4855065Swnj */ 4865065Swnj case TCPS_TIME_WAIT: 4875162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4885065Swnj goto dropafterack; 4894601Swnj } 4905085Swnj #undef ourfinisacked 4915085Swnj } 4924601Swnj 4935065Swnj step6: 4945065Swnj /* 4955244Sroot * Update window information. 4965244Sroot */ 4975300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 4985391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 4995300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5005391Swnj /* 5015391Swnj 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); 5025391Swnj */ 5035244Sroot tp->snd_wnd = ti->ti_win; 5045244Sroot tp->snd_wl1 = ti->ti_seq; 5055244Sroot tp->snd_wl2 = ti->ti_ack; 5065244Sroot if (tp->snd_wnd > 0) 5075244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5085244Sroot } 5095244Sroot 5105244Sroot /* 511*5419Swnj * If an URG bit is set and in the segment and is greater than the 5125065Swnj * current known urgent pointer, then signal the user that the 5135244Sroot * remote side has out of band data. This should not happen 5145065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 5155065Swnj * a FIN has been received from the remote side. In these states 5165065Swnj * we ignore the URG. 5175065Swnj */ 518*5419Swnj if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0 && 519*5419Swnj ti->ti_urp <= ti->ti_len && 520*5419Swnj SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 521*5419Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 522*5419Swnj so->so_oobmark = so->so_rcv.sb_cc + 523*5419Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 524*5419Swnj if (so->so_oobmark == 0) 525*5419Swnj so->so_state |= SS_RCVATMARK; 526*5419Swnj tcp_pulloutofband(so, ti); 527*5419Swnj sohasoutofband(so); 528*5419Swnj } 5294601Swnj 5304601Swnj /* 5315065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5325065Swnj * and arranging for acknowledgment of receipt if necessary. 5335065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5345065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5355065Swnj * case PRU_RCVD). If a FIN has already been received on this 5365065Swnj * connection then we just ignore the text. 5374601Swnj */ 5385263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 5395263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5405085Swnj off += sizeof (struct ip); /* drop IP header */ 5415085Swnj m->m_off += off; 5425085Swnj m->m_len -= off; 5435065Swnj tiflags = tcp_reass(tp, ti); 5445287Sroot { extern tcpdelack; 5455287Sroot if (tcpdelack) tp->t_flags |= TF_DELACK; else 5465085Swnj tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 5475287Sroot } 5485244Sroot } else { 5494924Swnj m_freem(m); 5505263Swnj tiflags &= ~TH_FIN; 5515244Sroot } 5524601Swnj 5534601Swnj /* 5545263Swnj * If FIN is received ACK the FIN and let the user know 5555263Swnj * that the connection is closing. 5564601Swnj */ 5575263Swnj if (tiflags & TH_FIN) { 5585244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5595244Sroot socantrcvmore(so); 5605244Sroot tp->t_flags |= TF_ACKNOW; 5615244Sroot tp->rcv_nxt++; 5625244Sroot } 5635065Swnj switch (tp->t_state) { 5644601Swnj 5655065Swnj /* 5665065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 5675065Swnj * enter the CLOSE_WAIT state. 5684884Swnj */ 5695065Swnj case TCPS_SYN_RECEIVED: 5705065Swnj case TCPS_ESTABLISHED: 5715065Swnj tp->t_state = TCPS_CLOSE_WAIT; 5725065Swnj break; 5734884Swnj 5745065Swnj /* 5755085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 5765085Swnj * enter the CLOSING state. 5774884Swnj */ 5785065Swnj case TCPS_FIN_WAIT_1: 5795085Swnj tp->t_state = TCPS_CLOSING; 5805065Swnj break; 5814601Swnj 5825065Swnj /* 5835065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 5845065Swnj * starting the time-wait timer, turning off the other 5855065Swnj * standard timers. 5865065Swnj */ 5875065Swnj case TCPS_FIN_WAIT_2: 5885244Sroot tp->t_state = TCPS_TIME_WAIT; 5895074Swnj tcp_canceltimers(tp); 5905162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5915244Sroot soisdisconnected(so); 5925065Swnj break; 5935065Swnj 5944884Swnj /* 5955065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 5964884Swnj */ 5975065Swnj case TCPS_TIME_WAIT: 5985162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5995065Swnj break; 6005085Swnj } 6014601Swnj } 6025267Sroot if (so->so_options & SO_DEBUG) 6035267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6045085Swnj 6055085Swnj /* 6065085Swnj * Return any desired output. 6075085Swnj */ 6085085Swnj tcp_output(tp); 6095065Swnj return; 6105085Swnj 6115065Swnj dropafterack: 6125085Swnj /* 6135244Sroot * Generate an ACK dropping incoming segment. 6145085Swnj * Make ACK reflect our state. 6155085Swnj */ 6165085Swnj if (tiflags & TH_RST) 6175085Swnj goto drop; 6185391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6195231Swnj return; 6205085Swnj 6215085Swnj dropwithreset: 6225085Swnj /* 6235244Sroot * Generate a RST, dropping incoming segment. 6245085Swnj * Make ACK acceptable to originator of segment. 6255085Swnj */ 6265085Swnj if (tiflags & TH_RST) 6275085Swnj goto drop; 6285085Swnj if (tiflags & TH_ACK) 6295391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6305085Swnj else { 6315085Swnj if (tiflags & TH_SYN) 6325085Swnj ti->ti_len++; 6335391Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6345085Swnj } 6355231Swnj return; 6365085Swnj 6375065Swnj drop: 6385085Swnj /* 6395085Swnj * Drop space held by incoming segment and return. 6405085Swnj */ 6415065Swnj m_freem(m); 6425267Sroot return; 6435065Swnj } 6445065Swnj 6455065Swnj /* 646*5419Swnj * Pull the character before the urgent pointer into 647*5419Swnj * the TCP control block for presentation as out-of-band data. 648*5419Swnj * We leave ti->ti_len reflecting the out-of-band data, 649*5419Swnj * so that sequencing will continue to work. 650*5419Swnj */ 651*5419Swnj tcp_pulloutofband(so, ti) 652*5419Swnj struct socket *so; 653*5419Swnj struct tcpiphdr *ti; 654*5419Swnj { 655*5419Swnj register struct mbuf *m; 656*5419Swnj int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1; 657*5419Swnj 658*5419Swnj m = dtom(ti); 659*5419Swnj while (cnt >= 0) { 660*5419Swnj if (m->m_len > cnt) { 661*5419Swnj char *cp = mtod(m, caddr_t) + cnt; 662*5419Swnj struct tcpcb *tp = sototcpcb(so); 663*5419Swnj 664*5419Swnj tp->t_oobc = *cp; 665*5419Swnj tp->t_haveoob = 1; 666*5419Swnj bcopy(cp+1, cp, m->m_len - cnt - 1); 667*5419Swnj m->m_len--; 668*5419Swnj return; 669*5419Swnj } 670*5419Swnj cnt -= m->m_len; 671*5419Swnj m = m->m_next; 672*5419Swnj if (m == 0) 673*5419Swnj break; 674*5419Swnj } 675*5419Swnj panic("tcp_pulloutofband"); 676*5419Swnj } 677*5419Swnj 678*5419Swnj /* 6795065Swnj * Insert segment ti into reassembly queue of tcp with 6805065Swnj * control block tp. Return TH_FIN if reassembly now includes 6815065Swnj * a segment with FIN. 6825065Swnj */ 6835109Swnj tcp_reass(tp, ti) 6845065Swnj register struct tcpcb *tp; 6855065Swnj register struct tcpiphdr *ti; 6865065Swnj { 6875065Swnj register struct tcpiphdr *q; 6885085Swnj struct socket *so = tp->t_inpcb->inp_socket; 6895263Swnj struct mbuf *m; 6905263Swnj int flags; 6915085Swnj COUNT(TCP_REASS); 6925065Swnj 6935065Swnj /* 6945162Swnj * Call with ti==0 after become established to 6955162Swnj * force pre-ESTABLISHED data up to user socket. 6965065Swnj */ 6975162Swnj if (ti == 0) 6985065Swnj goto present; 6994601Swnj 7005065Swnj /* 7015065Swnj * Find a segment which begins after this one does. 7025065Swnj */ 7035065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 7045065Swnj q = (struct tcpiphdr *)q->ti_next) 7055065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 7065065Swnj break; 7074601Swnj 7085065Swnj /* 7095065Swnj * If there is a preceding segment, it may provide some of 7105065Swnj * our data already. If so, drop the data from the incoming 7115065Swnj * segment. If it provides all of our data, drop us. 7125065Swnj */ 7135065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 7145065Swnj register int i; 7155065Swnj q = (struct tcpiphdr *)(q->ti_prev); 7165065Swnj /* conversion to int (in i) handles seq wraparound */ 7175065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 7185065Swnj if (i > 0) { 7194924Swnj if (i >= ti->ti_len) 7205065Swnj goto drop; 7215065Swnj m_adj(dtom(tp), i); 7225065Swnj ti->ti_len -= i; 7234924Swnj ti->ti_seq += i; 7244601Swnj } 7255065Swnj q = (struct tcpiphdr *)(q->ti_next); 7265065Swnj } 7274601Swnj 7285065Swnj /* 7295065Swnj * While we overlap succeeding segments trim them or, 7305065Swnj * if they are completely covered, dequeue them. 7315065Swnj */ 7325065Swnj while (q != (struct tcpiphdr *)tp && 7335065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 7345065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 7355065Swnj if (i < q->ti_len) { 7365065Swnj q->ti_len -= i; 7375065Swnj m_adj(dtom(q), i); 7385065Swnj break; 7394601Swnj } 7405065Swnj q = (struct tcpiphdr *)q->ti_next; 7415065Swnj m_freem(dtom(q->ti_prev)); 7425065Swnj remque(q->ti_prev); 7435065Swnj } 7444601Swnj 7455065Swnj /* 7465065Swnj * Stick new segment in its place. 7475065Swnj */ 7485065Swnj insque(ti, q->ti_prev); 7494601Swnj 7505065Swnj present: 7515065Swnj /* 7525244Sroot * Present data to user, advancing rcv_nxt through 7535244Sroot * completed sequence space. 7545065Swnj */ 7555263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 7565244Sroot return (0); 7574924Swnj ti = tp->seg_next; 7585263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 7595263Swnj return (0); 7605263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 7615263Swnj return (0); 7625263Swnj do { 7635244Sroot tp->rcv_nxt += ti->ti_len; 7645244Sroot flags = ti->ti_flags & TH_FIN; 7654924Swnj remque(ti); 7665263Swnj m = dtom(ti); 7674924Swnj ti = (struct tcpiphdr *)ti->ti_next; 7685263Swnj if (so->so_state & SS_CANTRCVMORE) 7695263Swnj (void) m_freem(m); 7705263Swnj else 7715263Swnj sbappend(&so->so_rcv, m); 7725263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 7735263Swnj sorwakeup(so); 7745065Swnj return (flags); 7755065Swnj drop: 7765065Swnj m_freem(dtom(ti)); 7775263Swnj return (0); 7784601Swnj } 779