1*5307Sroot /* tcp_input.c 1.45 81/12/23 */ 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; 414924Swnj register struct tcpcb *tp; 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); 56*5307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 57*5307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 585085Swnj tcpstat.tcps_hdrops++; 59*5307Sroot 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; 2095244Sroot tp->t_timer[TCPT_REXMT] = 0; 2105065Swnj tp->irs = ti->ti_seq; 2115085Swnj tcp_rcvseqinit(tp); 2125085Swnj tp->t_flags |= TF_ACKNOW; 2135162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2145263Swnj so->so_state |= SS_CONNAWAITING; 2155244Sroot soisconnected(so); 2165065Swnj tp->t_state = TCPS_ESTABLISHED; 2175162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2185162Swnj } else 2195085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2205085Swnj goto trimthenstep6; 2215085Swnj 2225085Swnj trimthenstep6: 2235085Swnj /* 2245231Swnj * Advance ti->ti_seq to correspond to first data byte. 2255085Swnj * If data, trim to stay within window, 2265085Swnj * dropping FIN if necessary. 2275085Swnj */ 2285231Swnj ti->ti_seq++; 2295085Swnj if (ti->ti_len > tp->rcv_wnd) { 2305085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2315085Swnj m_adj(m, -todrop); 2325085Swnj ti->ti_len = tp->rcv_wnd; 2335085Swnj ti->ti_flags &= ~TH_FIN; 2345065Swnj } 2355263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2365085Swnj goto step6; 2375065Swnj } 2384601Swnj 2395065Swnj /* 2405065Swnj * States other than LISTEN or SYN_SENT. 2415065Swnj * First check that at least some bytes of segment are within 2425065Swnj * receive window. 2435065Swnj */ 2445065Swnj if (tp->rcv_wnd == 0) { 2455065Swnj /* 2465065Swnj * If window is closed can only take segments at 2475231Swnj * window edge, and have to drop data and PUSH from 2485065Swnj * incoming segments. 2495065Swnj */ 2505300Sroot if (tp->rcv_nxt != ti->ti_seq) 2515065Swnj goto dropafterack; 2525085Swnj if (ti->ti_len > 0) { 2535085Swnj ti->ti_len = 0; 2545085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2555065Swnj } 2565065Swnj } else { 2575065Swnj /* 2585231Swnj * If segment begins before rcv_nxt, drop leading 2595065Swnj * data (and SYN); if nothing left, just ack. 2605065Swnj */ 2615065Swnj if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 2625085Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2635085Swnj if (tiflags & TH_SYN) { 2645300Sroot tiflags &= ~TH_SYN; 2655085Swnj ti->ti_seq++; 2665085Swnj if (ti->ti_urp > 1) 2675085Swnj ti->ti_urp--; 2685085Swnj else 2695085Swnj tiflags &= ~TH_URG; 2705085Swnj todrop--; 2715085Swnj } 2725300Sroot if (todrop > ti->ti_len) 2735065Swnj goto dropafterack; 2745065Swnj m_adj(m, todrop); 2755065Swnj ti->ti_seq += todrop; 2765065Swnj ti->ti_len -= todrop; 2775085Swnj if (ti->ti_urp > todrop) 2785085Swnj ti->ti_urp -= todrop; 2795085Swnj else { 2805085Swnj tiflags &= ~TH_URG; 2815085Swnj /* ti->ti_flags &= ~TH_URG; */ 2825085Swnj /* ti->ti_urp = 0; */ 2835085Swnj } 2845085Swnj /* tiflags &= ~TH_SYN; */ 2855085Swnj /* ti->ti_flags &= ~TH_SYN; */ 2865065Swnj } 2875065Swnj /* 2885065Swnj * If segment ends after window, drop trailing data 2895085Swnj * (and PUSH and FIN); if nothing left, just ACK. 2905065Swnj */ 2915065Swnj if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 2925085Swnj todrop = 2935065Swnj ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 2945300Sroot if (todrop > ti->ti_len) 2955065Swnj goto dropafterack; 2965065Swnj m_adj(m, -todrop); 2975065Swnj ti->ti_len -= todrop; 2985085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2995065Swnj } 3005065Swnj } 3014601Swnj 3025065Swnj /* 3035065Swnj * If the RST bit is set examine the state: 3045065Swnj * SYN_RECEIVED STATE: 3055065Swnj * If passive open, return to LISTEN state. 3065065Swnj * If active open, inform user that connection was refused. 3075065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3085065Swnj * Inform user that connection was reset, and close tcb. 3095065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3105065Swnj * Close the tcb. 3115065Swnj */ 3125065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3135267Sroot 3145065Swnj case TCPS_SYN_RECEIVED: 3155065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3165267Sroot /* a miniature tcp_close, but invisible to user */ 3175267Sroot (void) m_free(dtom(tp->t_template)); 3185267Sroot (void) m_free(dtom(tp)); 3195267Sroot inp->inp_ppcb = 0; 3205267Sroot tp = tcp_newtcpcb(inp); 3215085Swnj tp->t_state = TCPS_LISTEN; 3225065Swnj goto drop; 3234601Swnj } 3245085Swnj tcp_drop(tp, ECONNREFUSED); 3255065Swnj goto drop; 3264601Swnj 3275065Swnj case TCPS_ESTABLISHED: 3285065Swnj case TCPS_FIN_WAIT_1: 3295065Swnj case TCPS_FIN_WAIT_2: 3305065Swnj case TCPS_CLOSE_WAIT: 3315065Swnj tcp_drop(tp, ECONNRESET); 3325065Swnj goto drop; 3335065Swnj 3345065Swnj case TCPS_CLOSING: 3355065Swnj case TCPS_LAST_ACK: 3365065Swnj case TCPS_TIME_WAIT: 3375065Swnj tcp_close(tp); 3385065Swnj goto drop; 3394601Swnj } 3404601Swnj 3414601Swnj /* 3425065Swnj * If a SYN is in the window, then this is an 3435065Swnj * error and we send an RST and drop the connection. 3444601Swnj */ 3455065Swnj if (tiflags & TH_SYN) { 3465231Swnj tcp_drop(tp, ECONNRESET); 3475085Swnj goto dropwithreset; 3484601Swnj } 3494601Swnj 3504601Swnj /* 3515065Swnj * If the ACK bit is off we drop the segment and return. 3524601Swnj */ 3535085Swnj if ((tiflags & TH_ACK) == 0) 3545065Swnj goto drop; 3555065Swnj 3565065Swnj /* 3575065Swnj * Ack processing. 3585065Swnj */ 3594601Swnj switch (tp->t_state) { 3604601Swnj 3615065Swnj /* 3625065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3635065Swnj * ESTABLISHED state and continue processing, othewise 3645065Swnj * send an RST. 3655065Swnj */ 3665065Swnj case TCPS_SYN_RECEIVED: 3675085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 3685231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 3695085Swnj goto dropwithreset; 3705244Sroot tp->snd_una++; /* SYN acked */ 3715244Sroot tp->t_timer[TCPT_REXMT] = 0; 3725263Swnj so->so_state |= SS_CONNAWAITING; 3735085Swnj soisconnected(so); 3745085Swnj tp->t_state = TCPS_ESTABLISHED; 3755162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 3765244Sroot tp->snd_wl1 = ti->ti_seq - 1; 3775085Swnj /* fall into ... */ 3784601Swnj 3795065Swnj /* 3805065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 3815065Swnj * ACKs. If the ack is in the range 3825231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 3835065Swnj * then advance tp->snd_una to ti->ti_ack and drop 3845065Swnj * data from the retransmission queue. If this ACK reflects 3855065Swnj * more up to date window information we update our window information. 3865065Swnj */ 3875065Swnj case TCPS_ESTABLISHED: 3885065Swnj case TCPS_FIN_WAIT_1: 3895065Swnj case TCPS_FIN_WAIT_2: 3905065Swnj case TCPS_CLOSE_WAIT: 3915065Swnj case TCPS_CLOSING: 3925244Sroot case TCPS_LAST_ACK: 3935244Sroot case TCPS_TIME_WAIT: 3945085Swnj #define ourfinisacked (acked > 0) 3955085Swnj 3965244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 3975065Swnj break; 3985300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 3995065Swnj goto dropafterack; 4005085Swnj acked = ti->ti_ack - tp->snd_una; 401*5307Sroot if (ti->ti_ack == tp->snd_max) 4025244Sroot tp->t_timer[TCPT_REXMT] = 0; 403*5307Sroot else { 4045244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4055244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4065300Sroot tp->t_rtt = 0; 4075300Sroot tp->t_rxtshift = 0; 4085085Swnj } 409*5307Sroot if (acked > so->so_snd.sb_cc) { 410*5307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 411*5307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 412*5307Sroot } else { 413*5307Sroot sbdrop(&so->so_snd.sb_cc, acked); 414*5307Sroot tp->snd_wnd -= acked; 415*5307Sroot acked = 0; 416*5307Sroot } 4175300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4185300Sroot sowwakeup(so); 4195231Swnj tp->snd_una = ti->ti_ack; 4205162Swnj 4215162Swnj /* 4225162Swnj * If transmit timer is running and timed sequence 4235162Swnj * number was acked, update smoothed round trip time. 4245162Swnj */ 4255162Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4265244Sroot if (tp->t_srtt == 0) 4275244Sroot tp->t_srtt = tp->t_rtt; 4285244Sroot else 4295244Sroot tp->t_srtt = 4305244Sroot tcp_alpha * tp->t_srtt + 4315244Sroot (1 - tcp_alpha) * tp->t_rtt; 4325162Swnj tp->t_rtt = 0; 4335162Swnj } 4345162Swnj 4354601Swnj switch (tp->t_state) { 4364601Swnj 4375065Swnj /* 4385065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4395065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4405085Swnj * then enter FIN_WAIT_2. 4415065Swnj */ 4425065Swnj case TCPS_FIN_WAIT_1: 4435085Swnj if (ourfinisacked) 4445085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4454601Swnj break; 4464601Swnj 4475065Swnj /* 4485065Swnj * In CLOSING STATE in addition to the processing for 4495065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4505065Swnj * then enter the TIME-WAIT state, otherwise ignore 4515065Swnj * the segment. 4525065Swnj */ 4535065Swnj case TCPS_CLOSING: 4545244Sroot if (ourfinisacked) { 4555065Swnj tp->t_state = TCPS_TIME_WAIT; 4565244Sroot tcp_canceltimers(tp); 4575244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4585244Sroot soisdisconnected(so); 4595244Sroot } 4605244Sroot break; 4614601Swnj 4625065Swnj /* 4635085Swnj * The only thing that can arrive in LAST_ACK state 4645085Swnj * is an acknowledgment of our FIN. If our FIN is now 4655085Swnj * acknowledged, delete the TCB, enter the closed state 4665085Swnj * and return. 4675065Swnj */ 4685065Swnj case TCPS_LAST_ACK: 4695251Sroot if (ourfinisacked) 4705065Swnj tcp_close(tp); 4715065Swnj goto drop; 4724601Swnj 4735065Swnj /* 4745065Swnj * In TIME_WAIT state the only thing that should arrive 4755065Swnj * is a retransmission of the remote FIN. Acknowledge 4765065Swnj * it and restart the finack timer. 4775065Swnj */ 4785065Swnj case TCPS_TIME_WAIT: 4795162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4805065Swnj goto dropafterack; 4814601Swnj } 4825085Swnj #undef ourfinisacked 4835085Swnj } 4844601Swnj 4855065Swnj step6: 4865065Swnj /* 4875244Sroot * Update window information. 4885244Sroot */ 4895300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 4905300Sroot (SEQ_LEQ(tp->snd_wl2, ti->ti_ack) || 4915300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 4925244Sroot tp->snd_wnd = ti->ti_win; 4935244Sroot tp->snd_wl1 = ti->ti_seq; 4945244Sroot tp->snd_wl2 = ti->ti_ack; 4955244Sroot if (tp->snd_wnd > 0) 4965244Sroot tp->t_timer[TCPT_PERSIST] = 0; 4975244Sroot } 4985244Sroot 4995244Sroot /* 5005065Swnj * If an URG bit is set in the segment and is greater than the 5015065Swnj * current known urgent pointer, then signal the user that the 5025244Sroot * remote side has out of band data. This should not happen 5035065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 5045065Swnj * a FIN has been received from the remote side. In these states 5055065Swnj * we ignore the URG. 5065065Swnj */ 5075085Swnj if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 5085085Swnj if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 5095065Swnj tp->rcv_up = ti->ti_urp; 5105085Swnj #if 0 5115244Sroot sohasoutofband(so); /* XXX */ 5125085Swnj #endif 5134601Swnj } 5144601Swnj 5154601Swnj /* 5165065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5175065Swnj * and arranging for acknowledgment of receipt if necessary. 5185065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5195065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5205065Swnj * case PRU_RCVD). If a FIN has already been received on this 5215065Swnj * connection then we just ignore the text. 5224601Swnj */ 5235263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 5245263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5255085Swnj off += sizeof (struct ip); /* drop IP header */ 5265085Swnj m->m_off += off; 5275085Swnj m->m_len -= off; 5285065Swnj tiflags = tcp_reass(tp, ti); 5295287Sroot { extern tcpdelack; 5305287Sroot if (tcpdelack) tp->t_flags |= TF_DELACK; else 5315085Swnj tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 5325287Sroot } 5335244Sroot } else { 5344924Swnj m_freem(m); 5355263Swnj tiflags &= ~TH_FIN; 5365244Sroot } 5374601Swnj 5384601Swnj /* 5395263Swnj * If FIN is received ACK the FIN and let the user know 5405263Swnj * that the connection is closing. 5414601Swnj */ 5425263Swnj if (tiflags & TH_FIN) { 5435244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5445244Sroot socantrcvmore(so); 5455244Sroot tp->t_flags |= TF_ACKNOW; 5465244Sroot tp->rcv_nxt++; 5475244Sroot } 5485065Swnj switch (tp->t_state) { 5494601Swnj 5505065Swnj /* 5515065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 5525065Swnj * enter the CLOSE_WAIT state. 5534884Swnj */ 5545065Swnj case TCPS_SYN_RECEIVED: 5555065Swnj case TCPS_ESTABLISHED: 5565065Swnj tp->t_state = TCPS_CLOSE_WAIT; 5575065Swnj break; 5584884Swnj 5595065Swnj /* 5605085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 5615085Swnj * enter the CLOSING state. 5624884Swnj */ 5635065Swnj case TCPS_FIN_WAIT_1: 5645085Swnj tp->t_state = TCPS_CLOSING; 5655065Swnj break; 5664601Swnj 5675065Swnj /* 5685065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 5695065Swnj * starting the time-wait timer, turning off the other 5705065Swnj * standard timers. 5715065Swnj */ 5725065Swnj case TCPS_FIN_WAIT_2: 5735244Sroot tp->t_state = TCPS_TIME_WAIT; 5745074Swnj tcp_canceltimers(tp); 5755162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5765244Sroot soisdisconnected(so); 5775065Swnj break; 5785065Swnj 5794884Swnj /* 5805065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 5814884Swnj */ 5825065Swnj case TCPS_TIME_WAIT: 5835162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5845065Swnj break; 5855085Swnj } 5864601Swnj } 5875267Sroot if (so->so_options & SO_DEBUG) 5885267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 5895085Swnj 5905085Swnj /* 5915085Swnj * Return any desired output. 5925085Swnj */ 5935085Swnj tcp_output(tp); 5945065Swnj return; 5955085Swnj 5965065Swnj dropafterack: 5975085Swnj /* 5985244Sroot * Generate an ACK dropping incoming segment. 5995085Swnj * Make ACK reflect our state. 6005085Swnj */ 6015085Swnj if (tiflags & TH_RST) 6025085Swnj goto drop; 6035085Swnj tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6045231Swnj return; 6055085Swnj 6065085Swnj dropwithreset: 6075085Swnj /* 6085244Sroot * Generate a RST, dropping incoming segment. 6095085Swnj * Make ACK acceptable to originator of segment. 6105085Swnj */ 6115085Swnj if (tiflags & TH_RST) 6125085Swnj goto drop; 6135085Swnj if (tiflags & TH_ACK) 6145109Swnj tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6155085Swnj else { 6165085Swnj if (tiflags & TH_SYN) 6175085Swnj ti->ti_len++; 6185109Swnj tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6195085Swnj } 6205231Swnj return; 6215085Swnj 6225065Swnj drop: 6235085Swnj /* 6245085Swnj * Drop space held by incoming segment and return. 6255085Swnj */ 6265065Swnj m_freem(m); 6275267Sroot return; 6285065Swnj } 6295065Swnj 6305065Swnj /* 6315065Swnj * Insert segment ti into reassembly queue of tcp with 6325065Swnj * control block tp. Return TH_FIN if reassembly now includes 6335065Swnj * a segment with FIN. 6345065Swnj */ 6355109Swnj tcp_reass(tp, ti) 6365065Swnj register struct tcpcb *tp; 6375065Swnj register struct tcpiphdr *ti; 6385065Swnj { 6395065Swnj register struct tcpiphdr *q; 6405085Swnj struct socket *so = tp->t_inpcb->inp_socket; 6415263Swnj struct mbuf *m; 6425263Swnj int flags; 6435085Swnj COUNT(TCP_REASS); 6445065Swnj 6455065Swnj /* 6465162Swnj * Call with ti==0 after become established to 6475162Swnj * force pre-ESTABLISHED data up to user socket. 6485065Swnj */ 6495162Swnj if (ti == 0) 6505065Swnj goto present; 6514601Swnj 6525065Swnj /* 6535065Swnj * Find a segment which begins after this one does. 6545065Swnj */ 6555065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 6565065Swnj q = (struct tcpiphdr *)q->ti_next) 6575065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 6585065Swnj break; 6594601Swnj 6605065Swnj /* 6615065Swnj * If there is a preceding segment, it may provide some of 6625065Swnj * our data already. If so, drop the data from the incoming 6635065Swnj * segment. If it provides all of our data, drop us. 6645065Swnj */ 6655065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 6665065Swnj register int i; 6675065Swnj q = (struct tcpiphdr *)(q->ti_prev); 6685065Swnj /* conversion to int (in i) handles seq wraparound */ 6695065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 6705065Swnj if (i > 0) { 6714924Swnj if (i >= ti->ti_len) 6725065Swnj goto drop; 6735065Swnj m_adj(dtom(tp), i); 6745065Swnj ti->ti_len -= i; 6754924Swnj ti->ti_seq += i; 6764601Swnj } 6775065Swnj q = (struct tcpiphdr *)(q->ti_next); 6785065Swnj } 6794601Swnj 6805065Swnj /* 6815065Swnj * While we overlap succeeding segments trim them or, 6825065Swnj * if they are completely covered, dequeue them. 6835065Swnj */ 6845065Swnj while (q != (struct tcpiphdr *)tp && 6855065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 6865065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 6875065Swnj if (i < q->ti_len) { 6885065Swnj q->ti_len -= i; 6895065Swnj m_adj(dtom(q), i); 6905065Swnj break; 6914601Swnj } 6925065Swnj q = (struct tcpiphdr *)q->ti_next; 6935065Swnj m_freem(dtom(q->ti_prev)); 6945065Swnj remque(q->ti_prev); 6955065Swnj } 6964601Swnj 6975065Swnj /* 6985065Swnj * Stick new segment in its place. 6995065Swnj */ 7005065Swnj insque(ti, q->ti_prev); 7014601Swnj 7025065Swnj present: 7035065Swnj /* 7045244Sroot * Present data to user, advancing rcv_nxt through 7055244Sroot * completed sequence space. 7065065Swnj */ 7075263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 7085244Sroot return (0); 7094924Swnj ti = tp->seg_next; 7105263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 7115263Swnj return (0); 7125263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 7135263Swnj return (0); 7145263Swnj do { 7155244Sroot tp->rcv_nxt += ti->ti_len; 7165244Sroot flags = ti->ti_flags & TH_FIN; 7174924Swnj remque(ti); 7185263Swnj m = dtom(ti); 7194924Swnj ti = (struct tcpiphdr *)ti->ti_next; 7205263Swnj if (so->so_state & SS_CANTRCVMORE) 7215263Swnj (void) m_freem(m); 7225263Swnj else 7235263Swnj sbappend(&so->so_rcv, m); 7245263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 7255263Swnj sorwakeup(so); 7265065Swnj return (flags); 7275065Swnj drop: 7285065Swnj m_freem(dtom(ti)); 7295263Swnj return (0); 7304601Swnj } 731