1*5300Sroot /* tcp_input.c 1.44 81/12/22 */ 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 24*5300Sroot 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); 565085Swnj if (m->m_len < sizeof (struct tcpiphdr)) { 575085Swnj if (m_pullup(m, sizeof (struct tcpiphdr)) == 0) { 585085Swnj tcpstat.tcps_hdrops++; 595085Swnj goto drop; 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 */ 119*5300Sroot if (inp == 0) 1205085Swnj goto dropwithreset; 1215065Swnj tp = intotcpcb(inp); 122*5300Sroot 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; 162*5300Sroot if (tiflags & TH_ACK) 1635085Swnj goto dropwithreset; 164*5300Sroot if ((tiflags & TH_SYN) == 0) 1655065Swnj goto drop; 1665244Sroot tcp_in.sin_addr = ti->ti_src; 1675244Sroot tcp_in.sin_port = ti->ti_sport; 168*5300Sroot 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) && 197*5300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 198*5300Sroot (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 */ 250*5300Sroot 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) { 264*5300Sroot 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 } 272*5300Sroot 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); 294*5300Sroot 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; 398*5300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 3995065Swnj goto dropafterack; 4005085Swnj acked = ti->ti_ack - tp->snd_una; 4015244Sroot if (acked >= so->so_snd.sb_cc) { 4025085Swnj acked -= so->so_snd.sb_cc; 4035287Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4045244Sroot /* if acked > 0 our FIN is acked */ 4055244Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4065244Sroot tp->t_timer[TCPT_REXMT] = 0; 4075085Swnj } else { 4085287Sroot if (acked) { 4095287Sroot sbdrop(&so->so_snd, acked); 4105287Sroot tp->snd_wnd -= acked; 4115287Sroot acked = 0; 4125287Sroot } 4135244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4145244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 415*5300Sroot tp->t_rtt = 0; 416*5300Sroot tp->t_rxtshift = 0; 4175085Swnj } 418*5300Sroot if (so->so_snd.sb_flags & SB_WAIT) 419*5300Sroot sowwakeup(so); 4205231Swnj tp->snd_una = ti->ti_ack; 4215162Swnj 4225162Swnj /* 4235162Swnj * If transmit timer is running and timed sequence 4245162Swnj * number was acked, update smoothed round trip time. 4255162Swnj */ 4265162Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4275244Sroot if (tp->t_srtt == 0) 4285244Sroot tp->t_srtt = tp->t_rtt; 4295244Sroot else 4305244Sroot tp->t_srtt = 4315244Sroot tcp_alpha * tp->t_srtt + 4325244Sroot (1 - tcp_alpha) * tp->t_rtt; 4335162Swnj tp->t_rtt = 0; 4345162Swnj } 4355162Swnj 4364601Swnj switch (tp->t_state) { 4374601Swnj 4385065Swnj /* 4395065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4405065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4415085Swnj * then enter FIN_WAIT_2. 4425065Swnj */ 4435065Swnj case TCPS_FIN_WAIT_1: 4445085Swnj if (ourfinisacked) 4455085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4464601Swnj break; 4474601Swnj 4485065Swnj /* 4495065Swnj * In CLOSING STATE in addition to the processing for 4505065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4515065Swnj * then enter the TIME-WAIT state, otherwise ignore 4525065Swnj * the segment. 4535065Swnj */ 4545065Swnj case TCPS_CLOSING: 4555244Sroot if (ourfinisacked) { 4565065Swnj tp->t_state = TCPS_TIME_WAIT; 4575244Sroot tcp_canceltimers(tp); 4585244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4595244Sroot soisdisconnected(so); 4605244Sroot } 4615244Sroot break; 4624601Swnj 4635065Swnj /* 4645085Swnj * The only thing that can arrive in LAST_ACK state 4655085Swnj * is an acknowledgment of our FIN. If our FIN is now 4665085Swnj * acknowledged, delete the TCB, enter the closed state 4675085Swnj * and return. 4685065Swnj */ 4695065Swnj case TCPS_LAST_ACK: 4705251Sroot if (ourfinisacked) 4715065Swnj tcp_close(tp); 4725065Swnj goto drop; 4734601Swnj 4745065Swnj /* 4755065Swnj * In TIME_WAIT state the only thing that should arrive 4765065Swnj * is a retransmission of the remote FIN. Acknowledge 4775065Swnj * it and restart the finack timer. 4785065Swnj */ 4795065Swnj case TCPS_TIME_WAIT: 4805162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4815065Swnj goto dropafterack; 4824601Swnj } 4835085Swnj #undef ourfinisacked 4845085Swnj } 4854601Swnj 4865065Swnj step6: 4875065Swnj /* 4885244Sroot * Update window information. 4895244Sroot */ 490*5300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 491*5300Sroot (SEQ_LEQ(tp->snd_wl2, ti->ti_ack) || 492*5300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 4935244Sroot tp->snd_wnd = ti->ti_win; 4945244Sroot tp->snd_wl1 = ti->ti_seq; 4955244Sroot tp->snd_wl2 = ti->ti_ack; 4965244Sroot if (tp->snd_wnd > 0) 4975244Sroot tp->t_timer[TCPT_PERSIST] = 0; 4985244Sroot } 4995244Sroot 5005244Sroot /* 5015065Swnj * If an URG bit is set in the segment and is greater than the 5025065Swnj * current known urgent pointer, then signal the user that the 5035244Sroot * remote side has out of band data. This should not happen 5045065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 5055065Swnj * a FIN has been received from the remote side. In these states 5065065Swnj * we ignore the URG. 5075065Swnj */ 5085085Swnj if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 5095085Swnj if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 5105065Swnj tp->rcv_up = ti->ti_urp; 5115085Swnj #if 0 5125244Sroot sohasoutofband(so); /* XXX */ 5135085Swnj #endif 5144601Swnj } 5154601Swnj 5164601Swnj /* 5175065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5185065Swnj * and arranging for acknowledgment of receipt if necessary. 5195065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5205065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5215065Swnj * case PRU_RCVD). If a FIN has already been received on this 5225065Swnj * connection then we just ignore the text. 5234601Swnj */ 5245263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 5255263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5265085Swnj off += sizeof (struct ip); /* drop IP header */ 5275085Swnj m->m_off += off; 5285085Swnj m->m_len -= off; 5295065Swnj tiflags = tcp_reass(tp, ti); 5305287Sroot { extern tcpdelack; 5315287Sroot if (tcpdelack) tp->t_flags |= TF_DELACK; else 5325085Swnj tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 5335287Sroot } 5345244Sroot } else { 5354924Swnj m_freem(m); 5365263Swnj tiflags &= ~TH_FIN; 5375244Sroot } 5384601Swnj 5394601Swnj /* 5405263Swnj * If FIN is received ACK the FIN and let the user know 5415263Swnj * that the connection is closing. 5424601Swnj */ 5435263Swnj if (tiflags & TH_FIN) { 5445244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5455244Sroot socantrcvmore(so); 5465244Sroot tp->t_flags |= TF_ACKNOW; 5475244Sroot tp->rcv_nxt++; 5485244Sroot } 5495065Swnj switch (tp->t_state) { 5504601Swnj 5515065Swnj /* 5525065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 5535065Swnj * enter the CLOSE_WAIT state. 5544884Swnj */ 5555065Swnj case TCPS_SYN_RECEIVED: 5565065Swnj case TCPS_ESTABLISHED: 5575065Swnj tp->t_state = TCPS_CLOSE_WAIT; 5585065Swnj break; 5594884Swnj 5605065Swnj /* 5615085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 5625085Swnj * enter the CLOSING state. 5634884Swnj */ 5645065Swnj case TCPS_FIN_WAIT_1: 5655085Swnj tp->t_state = TCPS_CLOSING; 5665065Swnj break; 5674601Swnj 5685065Swnj /* 5695065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 5705065Swnj * starting the time-wait timer, turning off the other 5715065Swnj * standard timers. 5725065Swnj */ 5735065Swnj case TCPS_FIN_WAIT_2: 5745244Sroot tp->t_state = TCPS_TIME_WAIT; 5755074Swnj tcp_canceltimers(tp); 5765162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5775244Sroot soisdisconnected(so); 5785065Swnj break; 5795065Swnj 5804884Swnj /* 5815065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 5824884Swnj */ 5835065Swnj case TCPS_TIME_WAIT: 5845162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5855065Swnj break; 5865085Swnj } 5874601Swnj } 5885267Sroot if (so->so_options & SO_DEBUG) 5895267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 5905085Swnj 5915085Swnj /* 5925085Swnj * Return any desired output. 5935085Swnj */ 5945085Swnj tcp_output(tp); 5955065Swnj return; 5965085Swnj 5975065Swnj dropafterack: 5985085Swnj /* 5995244Sroot * Generate an ACK dropping incoming segment. 6005085Swnj * Make ACK reflect our state. 6015085Swnj */ 6025085Swnj if (tiflags & TH_RST) 6035085Swnj goto drop; 6045085Swnj tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6055231Swnj return; 6065085Swnj 6075085Swnj dropwithreset: 6085085Swnj /* 6095244Sroot * Generate a RST, dropping incoming segment. 6105085Swnj * Make ACK acceptable to originator of segment. 6115085Swnj */ 6125085Swnj if (tiflags & TH_RST) 6135085Swnj goto drop; 6145085Swnj if (tiflags & TH_ACK) 6155109Swnj tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6165085Swnj else { 6175085Swnj if (tiflags & TH_SYN) 6185085Swnj ti->ti_len++; 6195109Swnj tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6205085Swnj } 6215231Swnj return; 6225085Swnj 6235065Swnj drop: 6245085Swnj /* 6255085Swnj * Drop space held by incoming segment and return. 6265085Swnj */ 6275065Swnj m_freem(m); 6285267Sroot return; 6295065Swnj } 6305065Swnj 6315065Swnj /* 6325065Swnj * Insert segment ti into reassembly queue of tcp with 6335065Swnj * control block tp. Return TH_FIN if reassembly now includes 6345065Swnj * a segment with FIN. 6355065Swnj */ 6365109Swnj tcp_reass(tp, ti) 6375065Swnj register struct tcpcb *tp; 6385065Swnj register struct tcpiphdr *ti; 6395065Swnj { 6405065Swnj register struct tcpiphdr *q; 6415085Swnj struct socket *so = tp->t_inpcb->inp_socket; 6425263Swnj struct mbuf *m; 6435263Swnj int flags; 6445085Swnj COUNT(TCP_REASS); 6455065Swnj 6465065Swnj /* 6475162Swnj * Call with ti==0 after become established to 6485162Swnj * force pre-ESTABLISHED data up to user socket. 6495065Swnj */ 6505162Swnj if (ti == 0) 6515065Swnj goto present; 6524601Swnj 6535065Swnj /* 6545065Swnj * Find a segment which begins after this one does. 6555065Swnj */ 6565065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 6575065Swnj q = (struct tcpiphdr *)q->ti_next) 6585065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 6595065Swnj break; 6604601Swnj 6615065Swnj /* 6625065Swnj * If there is a preceding segment, it may provide some of 6635065Swnj * our data already. If so, drop the data from the incoming 6645065Swnj * segment. If it provides all of our data, drop us. 6655065Swnj */ 6665065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 6675065Swnj register int i; 6685065Swnj q = (struct tcpiphdr *)(q->ti_prev); 6695065Swnj /* conversion to int (in i) handles seq wraparound */ 6705065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 6715065Swnj if (i > 0) { 6724924Swnj if (i >= ti->ti_len) 6735065Swnj goto drop; 6745065Swnj m_adj(dtom(tp), i); 6755065Swnj ti->ti_len -= i; 6764924Swnj ti->ti_seq += i; 6774601Swnj } 6785065Swnj q = (struct tcpiphdr *)(q->ti_next); 6795065Swnj } 6804601Swnj 6815065Swnj /* 6825065Swnj * While we overlap succeeding segments trim them or, 6835065Swnj * if they are completely covered, dequeue them. 6845065Swnj */ 6855065Swnj while (q != (struct tcpiphdr *)tp && 6865065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 6875065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 6885065Swnj if (i < q->ti_len) { 6895065Swnj q->ti_len -= i; 6905065Swnj m_adj(dtom(q), i); 6915065Swnj break; 6924601Swnj } 6935065Swnj q = (struct tcpiphdr *)q->ti_next; 6945065Swnj m_freem(dtom(q->ti_prev)); 6955065Swnj remque(q->ti_prev); 6965065Swnj } 6974601Swnj 6985065Swnj /* 6995065Swnj * Stick new segment in its place. 7005065Swnj */ 7015065Swnj insque(ti, q->ti_prev); 7024601Swnj 7035065Swnj present: 7045065Swnj /* 7055244Sroot * Present data to user, advancing rcv_nxt through 7065244Sroot * completed sequence space. 7075065Swnj */ 7085263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 7095244Sroot return (0); 7104924Swnj ti = tp->seg_next; 7115263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 7125263Swnj return (0); 7135263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 7145263Swnj return (0); 7155263Swnj do { 7165244Sroot tp->rcv_nxt += ti->ti_len; 7175244Sroot flags = ti->ti_flags & TH_FIN; 7184924Swnj remque(ti); 7195263Swnj m = dtom(ti); 7204924Swnj ti = (struct tcpiphdr *)ti->ti_next; 7215263Swnj if (so->so_state & SS_CANTRCVMORE) 7225263Swnj (void) m_freem(m); 7235263Swnj else 7245263Swnj sbappend(&so->so_rcv, m); 7255263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 7265263Swnj sorwakeup(so); 7275065Swnj return (flags); 7285065Swnj drop: 7295065Swnj m_freem(dtom(ti)); 7305263Swnj return (0); 7314601Swnj } 732