1*5267Sroot /* tcp_input.c 1.42 81/12/20 */ 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" 21*5267Sroot #include "../net/tcp_debug.h" 225109Swnj #include "../errno.h" 234601Swnj 244679Swnj int tcpcksum = 1; 255244Sroot struct sockaddr_in tcp_in = { AF_INET }; 26*5267Sroot struct tcpiphdr tcp_saveti; 274601Swnj 28*5267Sroot struct tcpcb *tcp_newtcpcb(); 295065Swnj /* 305065Swnj * TCP input routine, follows pages 65-76 of the 315065Swnj * protocol specification dated September, 1981 very closely. 325065Swnj */ 334924Swnj tcp_input(m0) 344924Swnj struct mbuf *m0; 354601Swnj { 364924Swnj register struct tcpiphdr *ti; 374924Swnj struct inpcb *inp; 384924Swnj register struct mbuf *m; 394924Swnj int len, tlen, off; 404924Swnj register struct tcpcb *tp; 414924Swnj register int tiflags; 424803Swnj struct socket *so; 435109Swnj int todrop, acked; 44*5267Sroot short ostate; 454924Swnj 464601Swnj COUNT(TCP_INPUT); 474924Swnj /* 485244Sroot * Get IP and TCP header together in first mbuf. 495244Sroot * Note: IP leaves IP header in first mbuf. 504924Swnj */ 514924Swnj m = m0; 525020Sroot ti = mtod(m, struct tcpiphdr *); 535244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 545208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 555085Swnj if (m->m_len < sizeof (struct tcpiphdr)) { 565085Swnj if (m_pullup(m, sizeof (struct tcpiphdr)) == 0) { 575085Swnj tcpstat.tcps_hdrops++; 585085Swnj goto drop; 595085Swnj } 605085Swnj ti = mtod(m, struct tcpiphdr *); 615085Swnj } 624601Swnj 634601Swnj /* 645244Sroot * Checksum extended TCP header and data. 654601Swnj */ 664924Swnj tlen = ((struct ip *)ti)->ip_len; 674924Swnj len = sizeof (struct ip) + tlen; 684679Swnj if (tcpcksum) { 694924Swnj ti->ti_next = ti->ti_prev = 0; 704924Swnj ti->ti_x1 = 0; 715223Swnj ti->ti_len = (u_short)tlen; 725223Swnj #if vax 735223Swnj ti->ti_len = htons(ti->ti_len); 745223Swnj #endif 755231Swnj if (ti->ti_sum = in_cksum(m, len)) { 764924Swnj tcpstat.tcps_badsum++; 775065Swnj printf("tcp cksum %x\n", ti->ti_sum); 785085Swnj goto drop; 794601Swnj } 804601Swnj } 814601Swnj 824601Swnj /* 835244Sroot * Check that TCP offset makes sense, 845244Sroot * process TCP options and adjust length. 854601Swnj */ 864924Swnj off = ti->ti_off << 2; 875231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 884924Swnj tcpstat.tcps_badoff++; 895085Swnj goto drop; 904924Swnj } 914924Swnj ti->ti_len = tlen - off; 925085Swnj #if 0 935231Swnj if (off > sizeof (struct tcphdr)) 945085Swnj tcp_options(ti); 955085Swnj #endif 965065Swnj tiflags = ti->ti_flags; 974924Swnj 985231Swnj #if vax 994924Swnj /* 1005244Sroot * Convert TCP protocol specific fields to host format. 1015085Swnj */ 1025085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1035085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1045085Swnj ti->ti_win = ntohs(ti->ti_win); 1055085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1065231Swnj #endif 1075085Swnj 1085085Swnj /* 1094924Swnj * Locate pcb for segment. 1104924Swnj */ 1115065Swnj inp = in_pcblookup 1125065Swnj (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 1135065Swnj 1145065Swnj /* 1155065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1165244Sroot * all data in the incoming segment is discarded. 1175065Swnj */ 1184884Swnj if (inp == 0) 1195085Swnj goto dropwithreset; 1205065Swnj tp = intotcpcb(inp); 1215065Swnj if (tp == 0) 1225085Swnj goto dropwithreset; 1235109Swnj so = inp->inp_socket; 124*5267Sroot if (so->so_options & SO_DEBUG) { 125*5267Sroot ostate = tp->t_state; 126*5267Sroot tcp_saveti = *ti; 127*5267Sroot } 128*5267Sroot 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; 1625065Swnj if (tiflags & TH_ACK) 1635085Swnj goto dropwithreset; 1645065Swnj if ((tiflags & TH_SYN) == 0) 1655065Swnj goto drop; 1665244Sroot tcp_in.sin_addr = ti->ti_src; 1675244Sroot tcp_in.sin_port = ti->ti_sport; 1685244Sroot 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) && 1975065Swnj (SEQ_LEQ(ti->ti_ack, tp->iss) || 1985231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 1995085Swnj goto dropwithreset; 2005065Swnj if (tiflags & TH_RST) { 2015065Swnj if (tiflags & TH_ACK) 202*5267Sroot tcp_drop(tp, ECONNREFUSED); 2035065Swnj goto drop; 2044601Swnj } 2055065Swnj if ((tiflags & TH_SYN) == 0) 2065065Swnj goto drop; 2075231Swnj tp->snd_una = ti->ti_ack; 2085244Sroot tp->t_timer[TCPT_REXMT] = 0; 2095065Swnj tp->irs = ti->ti_seq; 2105085Swnj tcp_rcvseqinit(tp); 2115085Swnj tp->t_flags |= TF_ACKNOW; 2125162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2135263Swnj so->so_state |= SS_CONNAWAITING; 2145244Sroot soisconnected(so); 2155065Swnj tp->t_state = TCPS_ESTABLISHED; 2165162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2175162Swnj } else 2185085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2195085Swnj goto trimthenstep6; 2205085Swnj 2215085Swnj trimthenstep6: 2225085Swnj /* 2235231Swnj * Advance ti->ti_seq to correspond to first data byte. 2245085Swnj * If data, trim to stay within window, 2255085Swnj * dropping FIN if necessary. 2265085Swnj */ 2275231Swnj ti->ti_seq++; 2285085Swnj if (ti->ti_len > tp->rcv_wnd) { 2295085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2305085Swnj m_adj(m, -todrop); 2315085Swnj ti->ti_len = tp->rcv_wnd; 2325085Swnj ti->ti_flags &= ~TH_FIN; 2335065Swnj } 2345263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2355085Swnj goto step6; 2365065Swnj } 2374601Swnj 2385065Swnj /* 2395065Swnj * States other than LISTEN or SYN_SENT. 2405065Swnj * First check that at least some bytes of segment are within 2415065Swnj * receive window. 2425065Swnj */ 2435065Swnj if (tp->rcv_wnd == 0) { 2445065Swnj /* 2455065Swnj * If window is closed can only take segments at 2465231Swnj * window edge, and have to drop data and PUSH from 2475065Swnj * incoming segments. 2485065Swnj */ 2495065Swnj if (tp->rcv_nxt != ti->ti_seq) 2505065Swnj goto dropafterack; 2515085Swnj if (ti->ti_len > 0) { 2525085Swnj ti->ti_len = 0; 2535085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2545065Swnj } 2555065Swnj } else { 2565065Swnj /* 2575231Swnj * If segment begins before rcv_nxt, drop leading 2585065Swnj * data (and SYN); if nothing left, just ack. 2595065Swnj */ 2605065Swnj if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 2615085Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2625085Swnj if (tiflags & TH_SYN) { 2635085Swnj ti->ti_seq++; 2645085Swnj if (ti->ti_urp > 1) 2655085Swnj ti->ti_urp--; 2665085Swnj else 2675085Swnj tiflags &= ~TH_URG; 2685085Swnj todrop--; 2695085Swnj } 2705065Swnj if (todrop > ti->ti_len) 2715065Swnj goto dropafterack; 2725065Swnj m_adj(m, todrop); 2735065Swnj ti->ti_seq += todrop; 2745065Swnj ti->ti_len -= todrop; 2755085Swnj if (ti->ti_urp > todrop) 2765085Swnj ti->ti_urp -= todrop; 2775085Swnj else { 2785085Swnj tiflags &= ~TH_URG; 2795085Swnj /* ti->ti_flags &= ~TH_URG; */ 2805085Swnj /* ti->ti_urp = 0; */ 2815085Swnj } 2825085Swnj /* tiflags &= ~TH_SYN; */ 2835085Swnj /* ti->ti_flags &= ~TH_SYN; */ 2845065Swnj } 2855065Swnj /* 2865065Swnj * If segment ends after window, drop trailing data 2875085Swnj * (and PUSH and FIN); if nothing left, just ACK. 2885065Swnj */ 2895065Swnj if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 2905085Swnj todrop = 2915065Swnj ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 2925065Swnj if (todrop > ti->ti_len) 2935065Swnj goto dropafterack; 2945065Swnj m_adj(m, -todrop); 2955065Swnj ti->ti_len -= todrop; 2965085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2975065Swnj } 2985065Swnj } 2994601Swnj 3005065Swnj /* 3015065Swnj * If the RST bit is set examine the state: 3025065Swnj * SYN_RECEIVED STATE: 3035065Swnj * If passive open, return to LISTEN state. 3045065Swnj * If active open, inform user that connection was refused. 3055065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3065065Swnj * Inform user that connection was reset, and close tcb. 3075065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3085065Swnj * Close the tcb. 3095065Swnj */ 3105065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 311*5267Sroot 3125065Swnj case TCPS_SYN_RECEIVED: 3135065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 314*5267Sroot /* a miniature tcp_close, but invisible to user */ 315*5267Sroot (void) m_free(dtom(tp->t_template)); 316*5267Sroot (void) m_free(dtom(tp)); 317*5267Sroot inp->inp_ppcb = 0; 318*5267Sroot tp = tcp_newtcpcb(inp); 3195085Swnj tp->t_state = TCPS_LISTEN; 3205065Swnj goto drop; 3214601Swnj } 3225085Swnj tcp_drop(tp, ECONNREFUSED); 3235065Swnj goto drop; 3244601Swnj 3255065Swnj case TCPS_ESTABLISHED: 3265065Swnj case TCPS_FIN_WAIT_1: 3275065Swnj case TCPS_FIN_WAIT_2: 3285065Swnj case TCPS_CLOSE_WAIT: 3295065Swnj tcp_drop(tp, ECONNRESET); 3305065Swnj goto drop; 3315065Swnj 3325065Swnj case TCPS_CLOSING: 3335065Swnj case TCPS_LAST_ACK: 3345065Swnj case TCPS_TIME_WAIT: 3355065Swnj tcp_close(tp); 3365065Swnj goto drop; 3374601Swnj } 3384601Swnj 3394601Swnj /* 3405065Swnj * If a SYN is in the window, then this is an 3415065Swnj * error and we send an RST and drop the connection. 3424601Swnj */ 3435065Swnj if (tiflags & TH_SYN) { 3445231Swnj tcp_drop(tp, ECONNRESET); 3455085Swnj goto dropwithreset; 3464601Swnj } 3474601Swnj 3484601Swnj /* 3495065Swnj * If the ACK bit is off we drop the segment and return. 3504601Swnj */ 3515085Swnj if ((tiflags & TH_ACK) == 0) 3525065Swnj goto drop; 3535065Swnj 3545065Swnj /* 3555065Swnj * Ack processing. 3565065Swnj */ 3574601Swnj switch (tp->t_state) { 3584601Swnj 3595065Swnj /* 3605065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3615065Swnj * ESTABLISHED state and continue processing, othewise 3625065Swnj * send an RST. 3635065Swnj */ 3645065Swnj case TCPS_SYN_RECEIVED: 3655085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 3665231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 3675085Swnj goto dropwithreset; 3685244Sroot tp->snd_una++; /* SYN acked */ 3695244Sroot tp->t_timer[TCPT_REXMT] = 0; 3705263Swnj so->so_state |= SS_CONNAWAITING; 3715085Swnj soisconnected(so); 3725085Swnj tp->t_state = TCPS_ESTABLISHED; 3735162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 3745244Sroot tp->snd_wl1 = ti->ti_seq - 1; 3755085Swnj /* fall into ... */ 3764601Swnj 3775065Swnj /* 3785065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 3795065Swnj * ACKs. If the ack is in the range 3805231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 3815065Swnj * then advance tp->snd_una to ti->ti_ack and drop 3825065Swnj * data from the retransmission queue. If this ACK reflects 3835065Swnj * more up to date window information we update our window information. 3845065Swnj */ 3855065Swnj case TCPS_ESTABLISHED: 3865065Swnj case TCPS_FIN_WAIT_1: 3875065Swnj case TCPS_FIN_WAIT_2: 3885065Swnj case TCPS_CLOSE_WAIT: 3895065Swnj case TCPS_CLOSING: 3905244Sroot case TCPS_LAST_ACK: 3915244Sroot case TCPS_TIME_WAIT: 3925085Swnj #define ourfinisacked (acked > 0) 3935085Swnj 3945244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 3955065Swnj break; 3965231Swnj if (SEQ_GT(ti->ti_ack, tp->snd_max)) 3975065Swnj goto dropafterack; 3985085Swnj acked = ti->ti_ack - tp->snd_una; 3995244Sroot if (acked >= so->so_snd.sb_cc) { 4005085Swnj acked -= so->so_snd.sb_cc; 4015244Sroot /* if acked > 0 our FIN is acked */ 4025263Swnj if (so->so_snd.sb_cc) 4035244Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4045244Sroot tp->t_timer[TCPT_REXMT] = 0; 4055085Swnj } else { 4065263Swnj if (acked) 4075085Swnj sbdrop(&so->so_snd, acked); 4085085Swnj acked = 0; 4095244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4105244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4115085Swnj } 4125231Swnj tp->snd_una = ti->ti_ack; 4135162Swnj 4145162Swnj /* 4155162Swnj * If transmit timer is running and timed sequence 4165162Swnj * number was acked, update smoothed round trip time. 4175162Swnj */ 4185162Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4195244Sroot if (tp->t_srtt == 0) 4205244Sroot tp->t_srtt = tp->t_rtt; 4215244Sroot else 4225244Sroot tp->t_srtt = 4235244Sroot tcp_alpha * tp->t_srtt + 4245244Sroot (1 - tcp_alpha) * tp->t_rtt; 4255162Swnj tp->t_rtt = 0; 4265162Swnj } 4275162Swnj 4284601Swnj switch (tp->t_state) { 4294601Swnj 4305065Swnj /* 4315065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4325065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4335085Swnj * then enter FIN_WAIT_2. 4345065Swnj */ 4355065Swnj case TCPS_FIN_WAIT_1: 4365085Swnj if (ourfinisacked) 4375085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4384601Swnj break; 4394601Swnj 4405065Swnj /* 4415065Swnj * In CLOSING STATE in addition to the processing for 4425065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4435065Swnj * then enter the TIME-WAIT state, otherwise ignore 4445065Swnj * the segment. 4455065Swnj */ 4465065Swnj case TCPS_CLOSING: 4475244Sroot if (ourfinisacked) { 4485065Swnj tp->t_state = TCPS_TIME_WAIT; 4495244Sroot tcp_canceltimers(tp); 4505244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4515244Sroot soisdisconnected(so); 4525244Sroot } 4535244Sroot break; 4544601Swnj 4555065Swnj /* 4565085Swnj * The only thing that can arrive in LAST_ACK state 4575085Swnj * is an acknowledgment of our FIN. If our FIN is now 4585085Swnj * acknowledged, delete the TCB, enter the closed state 4595085Swnj * and return. 4605065Swnj */ 4615065Swnj case TCPS_LAST_ACK: 4625251Sroot if (ourfinisacked) 4635065Swnj tcp_close(tp); 4645065Swnj goto drop; 4654601Swnj 4665065Swnj /* 4675065Swnj * In TIME_WAIT state the only thing that should arrive 4685065Swnj * is a retransmission of the remote FIN. Acknowledge 4695065Swnj * it and restart the finack timer. 4705065Swnj */ 4715065Swnj case TCPS_TIME_WAIT: 4725162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4735065Swnj goto dropafterack; 4744601Swnj } 4755085Swnj #undef ourfinisacked 4765085Swnj } 4774601Swnj 4785065Swnj step6: 4795065Swnj /* 4805244Sroot * Update window information. 4815244Sroot */ 4825244Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 4835244Sroot tp->snd_wl1==ti->ti_seq && SEQ_LEQ(tp->snd_wl2,ti->ti_seq)) { 4845244Sroot tp->snd_wnd = ti->ti_win; 4855244Sroot tp->snd_wl1 = ti->ti_seq; 4865244Sroot tp->snd_wl2 = ti->ti_ack; 4875244Sroot if (tp->snd_wnd > 0) 4885244Sroot tp->t_timer[TCPT_PERSIST] = 0; 4895244Sroot } 4905244Sroot 4915244Sroot /* 4925065Swnj * If an URG bit is set in the segment and is greater than the 4935065Swnj * current known urgent pointer, then signal the user that the 4945244Sroot * remote side has out of band data. This should not happen 4955065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 4965065Swnj * a FIN has been received from the remote side. In these states 4975065Swnj * we ignore the URG. 4985065Swnj */ 4995085Swnj if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 5005085Swnj if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 5015065Swnj tp->rcv_up = ti->ti_urp; 5025085Swnj #if 0 5035244Sroot sohasoutofband(so); /* XXX */ 5045085Swnj #endif 5054601Swnj } 5064601Swnj 5074601Swnj /* 5085065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5095065Swnj * and arranging for acknowledgment of receipt if necessary. 5105065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5115065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5125065Swnj * case PRU_RCVD). If a FIN has already been received on this 5135065Swnj * connection then we just ignore the text. 5144601Swnj */ 5155263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 5165263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5175085Swnj off += sizeof (struct ip); /* drop IP header */ 5185085Swnj m->m_off += off; 5195085Swnj m->m_len -= off; 5205065Swnj tiflags = tcp_reass(tp, ti); 5215085Swnj tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 5225244Sroot } else { 5234924Swnj m_freem(m); 5245263Swnj tiflags &= ~TH_FIN; 5255244Sroot } 5264601Swnj 5274601Swnj /* 5285263Swnj * If FIN is received ACK the FIN and let the user know 5295263Swnj * that the connection is closing. 5304601Swnj */ 5315263Swnj if (tiflags & TH_FIN) { 5325244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5335244Sroot socantrcvmore(so); 5345244Sroot tp->t_flags |= TF_ACKNOW; 5355244Sroot tp->rcv_nxt++; 5365244Sroot } 5375065Swnj switch (tp->t_state) { 5384601Swnj 5395065Swnj /* 5405065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 5415065Swnj * enter the CLOSE_WAIT state. 5424884Swnj */ 5435065Swnj case TCPS_SYN_RECEIVED: 5445065Swnj case TCPS_ESTABLISHED: 5455065Swnj tp->t_state = TCPS_CLOSE_WAIT; 5465065Swnj break; 5474884Swnj 5485065Swnj /* 5495085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 5505085Swnj * enter the CLOSING state. 5514884Swnj */ 5525065Swnj case TCPS_FIN_WAIT_1: 5535085Swnj tp->t_state = TCPS_CLOSING; 5545065Swnj break; 5554601Swnj 5565065Swnj /* 5575065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 5585065Swnj * starting the time-wait timer, turning off the other 5595065Swnj * standard timers. 5605065Swnj */ 5615065Swnj case TCPS_FIN_WAIT_2: 5625244Sroot tp->t_state = TCPS_TIME_WAIT; 5635074Swnj tcp_canceltimers(tp); 5645162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5655244Sroot soisdisconnected(so); 5665065Swnj break; 5675065Swnj 5684884Swnj /* 5695065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 5704884Swnj */ 5715065Swnj case TCPS_TIME_WAIT: 5725162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5735065Swnj break; 5745085Swnj } 5754601Swnj } 576*5267Sroot if (so->so_options & SO_DEBUG) 577*5267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 5785085Swnj 5795085Swnj /* 5805085Swnj * Return any desired output. 5815085Swnj */ 5825085Swnj tcp_output(tp); 5835065Swnj return; 5845085Swnj 5855065Swnj dropafterack: 5865085Swnj /* 5875244Sroot * Generate an ACK dropping incoming segment. 5885085Swnj * Make ACK reflect our state. 5895085Swnj */ 5905085Swnj if (tiflags & TH_RST) 5915085Swnj goto drop; 5925085Swnj tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 5935231Swnj return; 5945085Swnj 5955085Swnj dropwithreset: 5965085Swnj /* 5975244Sroot * Generate a RST, dropping incoming segment. 5985085Swnj * Make ACK acceptable to originator of segment. 5995085Swnj */ 6005085Swnj if (tiflags & TH_RST) 6015085Swnj goto drop; 6025085Swnj if (tiflags & TH_ACK) 6035109Swnj tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6045085Swnj else { 6055085Swnj if (tiflags & TH_SYN) 6065085Swnj ti->ti_len++; 6075109Swnj tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6085085Swnj } 6095231Swnj return; 6105085Swnj 6115065Swnj drop: 6125085Swnj /* 6135085Swnj * Drop space held by incoming segment and return. 6145085Swnj */ 6155065Swnj m_freem(m); 616*5267Sroot return; 6175065Swnj } 6185065Swnj 6195065Swnj /* 6205065Swnj * Insert segment ti into reassembly queue of tcp with 6215065Swnj * control block tp. Return TH_FIN if reassembly now includes 6225065Swnj * a segment with FIN. 6235065Swnj */ 6245109Swnj tcp_reass(tp, ti) 6255065Swnj register struct tcpcb *tp; 6265065Swnj register struct tcpiphdr *ti; 6275065Swnj { 6285065Swnj register struct tcpiphdr *q; 6295085Swnj struct socket *so = tp->t_inpcb->inp_socket; 6305263Swnj struct mbuf *m; 6315263Swnj int flags; 6325085Swnj COUNT(TCP_REASS); 6335065Swnj 6345065Swnj /* 6355162Swnj * Call with ti==0 after become established to 6365162Swnj * force pre-ESTABLISHED data up to user socket. 6375065Swnj */ 6385162Swnj if (ti == 0) 6395065Swnj goto present; 6404601Swnj 6415065Swnj /* 6425065Swnj * Find a segment which begins after this one does. 6435065Swnj */ 6445065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 6455065Swnj q = (struct tcpiphdr *)q->ti_next) 6465065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 6475065Swnj break; 6484601Swnj 6495065Swnj /* 6505065Swnj * If there is a preceding segment, it may provide some of 6515065Swnj * our data already. If so, drop the data from the incoming 6525065Swnj * segment. If it provides all of our data, drop us. 6535065Swnj */ 6545065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 6555065Swnj register int i; 6565065Swnj q = (struct tcpiphdr *)(q->ti_prev); 6575065Swnj /* conversion to int (in i) handles seq wraparound */ 6585065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 6595065Swnj if (i > 0) { 6604924Swnj if (i >= ti->ti_len) 6615065Swnj goto drop; 6625065Swnj m_adj(dtom(tp), i); 6635065Swnj ti->ti_len -= i; 6644924Swnj ti->ti_seq += i; 6654601Swnj } 6665065Swnj q = (struct tcpiphdr *)(q->ti_next); 6675065Swnj } 6684601Swnj 6695065Swnj /* 6705065Swnj * While we overlap succeeding segments trim them or, 6715065Swnj * if they are completely covered, dequeue them. 6725065Swnj */ 6735065Swnj while (q != (struct tcpiphdr *)tp && 6745065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 6755065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 6765065Swnj if (i < q->ti_len) { 6775065Swnj q->ti_len -= i; 6785065Swnj m_adj(dtom(q), i); 6795065Swnj break; 6804601Swnj } 6815065Swnj q = (struct tcpiphdr *)q->ti_next; 6825065Swnj m_freem(dtom(q->ti_prev)); 6835065Swnj remque(q->ti_prev); 6845065Swnj } 6854601Swnj 6865065Swnj /* 6875065Swnj * Stick new segment in its place. 6885065Swnj */ 6895065Swnj insque(ti, q->ti_prev); 6904601Swnj 6915065Swnj present: 6925065Swnj /* 6935244Sroot * Present data to user, advancing rcv_nxt through 6945244Sroot * completed sequence space. 6955065Swnj */ 6965263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 6975244Sroot return (0); 6984924Swnj ti = tp->seg_next; 6995263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 7005263Swnj return (0); 7015263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 7025263Swnj return (0); 7035263Swnj do { 7045244Sroot tp->rcv_nxt += ti->ti_len; 7055244Sroot flags = ti->ti_flags & TH_FIN; 7064924Swnj remque(ti); 7075263Swnj m = dtom(ti); 7084924Swnj ti = (struct tcpiphdr *)ti->ti_next; 7095263Swnj if (so->so_state & SS_CANTRCVMORE) 7105263Swnj (void) m_freem(m); 7115263Swnj else 7125263Swnj sbappend(&so->so_rcv, m); 7135263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 7145263Swnj sorwakeup(so); 7155065Swnj return (flags); 7165065Swnj drop: 7175065Swnj m_freem(dtom(ti)); 7185263Swnj return (0); 7194601Swnj } 720