1*5244Sroot /* tcp_input.c 1.39 81/12/12 */ 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" 16*5244Sroot #define TCPSTATES 174803Swnj #include "../net/tcp_fsm.h" 185085Swnj #include "../net/tcp_seq.h" 195085Swnj #include "../net/tcp_timer.h" 204803Swnj #include "../net/tcp_var.h" 215085Swnj #include "../net/tcpip.h" 225109Swnj #include "../errno.h" 234601Swnj 244679Swnj int tcpcksum = 1; 25*5244Sroot struct sockaddr_in tcp_in = { AF_INET }; 264601Swnj 275065Swnj /* 285065Swnj * TCP input routine, follows pages 65-76 of the 295065Swnj * protocol specification dated September, 1981 very closely. 305065Swnj */ 314924Swnj tcp_input(m0) 324924Swnj struct mbuf *m0; 334601Swnj { 344924Swnj register struct tcpiphdr *ti; 354924Swnj struct inpcb *inp; 364924Swnj register struct mbuf *m; 374924Swnj int len, tlen, off; 384924Swnj register struct tcpcb *tp; 394924Swnj register int tiflags; 404803Swnj struct socket *so; 415109Swnj int todrop, acked; 424924Swnj 434601Swnj COUNT(TCP_INPUT); 444924Swnj /* 45*5244Sroot * Get IP and TCP header together in first mbuf. 46*5244Sroot * Note: IP leaves IP header in first mbuf. 474924Swnj */ 484924Swnj m = m0; 495020Sroot ti = mtod(m, struct tcpiphdr *); 50*5244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 515208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 525085Swnj if (m->m_len < sizeof (struct tcpiphdr)) { 53*5244Sroot printf("m->m_len %d\n", m->m_len); 545085Swnj if (m_pullup(m, sizeof (struct tcpiphdr)) == 0) { 55*5244Sroot printf("tcp_input: header drop\n"); 565085Swnj tcpstat.tcps_hdrops++; 575085Swnj goto drop; 585085Swnj } 595085Swnj ti = mtod(m, struct tcpiphdr *); 605085Swnj } 614601Swnj 624601Swnj /* 63*5244Sroot * Checksum extended TCP header and data. 644601Swnj */ 654924Swnj tlen = ((struct ip *)ti)->ip_len; 664924Swnj len = sizeof (struct ip) + tlen; 674679Swnj if (tcpcksum) { 684924Swnj ti->ti_next = ti->ti_prev = 0; 694924Swnj ti->ti_x1 = 0; 705223Swnj ti->ti_len = (u_short)tlen; 715223Swnj #if vax 725223Swnj ti->ti_len = htons(ti->ti_len); 735223Swnj #endif 745231Swnj if (ti->ti_sum = in_cksum(m, len)) { 754924Swnj tcpstat.tcps_badsum++; 765065Swnj printf("tcp cksum %x\n", ti->ti_sum); 775085Swnj goto drop; 784601Swnj } 794601Swnj } 804601Swnj 814601Swnj /* 82*5244Sroot * Check that TCP offset makes sense, 83*5244Sroot * process TCP options and adjust length. 844601Swnj */ 854924Swnj off = ti->ti_off << 2; 865231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 87*5244Sroot printf("tcp_input: bad offset\n"); 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 /* 100*5244Sroot * 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 116*5244Sroot * 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; 123*5244Sroot printf("tcp_input: segment seq %x ack %x win %x inp %x flags", 124*5244Sroot ti->ti_seq, ti->ti_ack, ti->ti_win, inp); 125*5244Sroot if (ti->ti_flags & TH_FIN) printf(" FIN"); 126*5244Sroot if (ti->ti_flags & TH_SYN) printf(" SYN"); 127*5244Sroot if (ti->ti_flags & TH_RST) printf(" RST"); 128*5244Sroot if (ti->ti_flags & TH_PUSH) printf(" PUSH"); 129*5244Sroot if (ti->ti_flags & TH_ACK) printf(" ACK"); 130*5244Sroot if (ti->ti_flags & TH_URG) printf(" URG"); 131*5244Sroot printf("\n"); 132*5244Sroot printf("tcp_input: "); pseqno(tp); 1335109Swnj so = inp->inp_socket; 1344601Swnj 1354601Swnj /* 1365162Swnj * Segment received on connection. 1375162Swnj * Reset idle time and keep-alive timer. 1385162Swnj */ 1395162Swnj tp->t_idle = 0; 1405162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1415162Swnj 1425162Swnj /* 1435085Swnj * Calculate amount of space in receive window, 1445085Swnj * and then do TCP input processing. 1454601Swnj */ 1465085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1475231Swnj if (tp->rcv_wnd < 0) 1485231Swnj tp->rcv_wnd = 0; 1494601Swnj 1504601Swnj switch (tp->t_state) { 1514601Swnj 1525065Swnj /* 1535065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1545065Swnj * If the segment contains an ACK then it is bad and send a RST. 1555065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1565085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1575065Swnj * tp->iss, and send a segment: 1585085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1595065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1605065Swnj * Fill in remote peer address fields if not previously specified. 1615065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 162*5244Sroot * segment in this state. 1635065Swnj */ 1645065Swnj case TCPS_LISTEN: 1655065Swnj if (tiflags & TH_RST) 1665065Swnj goto drop; 1675065Swnj if (tiflags & TH_ACK) 1685085Swnj goto dropwithreset; 1695065Swnj if ((tiflags & TH_SYN) == 0) 1705065Swnj goto drop; 171*5244Sroot tcp_in.sin_addr = ti->ti_src; 172*5244Sroot tcp_in.sin_port = ti->ti_sport; 173*5244Sroot if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in)) 174*5244Sroot goto drop; 175*5244Sroot tp->t_template = tcp_template(tp); 176*5244Sroot if (tp->t_template == 0) { 177*5244Sroot in_pcbdisconnect(inp); 178*5244Sroot goto drop; 179*5244Sroot } 1805085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1815065Swnj tp->irs = ti->ti_seq; 1825085Swnj tcp_sendseqinit(tp); 1835085Swnj tcp_rcvseqinit(tp); 1845065Swnj tp->t_state = TCPS_SYN_RECEIVED; 185*5244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 186*5244Sroot printf("tcp_input: out of LISTEN: "); 1875085Swnj goto trimthenstep6; 1884601Swnj 1895065Swnj /* 1905065Swnj * If the state is SYN_SENT: 1915065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 1925065Swnj * if seg contains a RST, then drop the connection. 1935065Swnj * if seg does not contain SYN, then drop it. 1945065Swnj * Otherwise this is an acceptable SYN segment 1955065Swnj * initialize tp->rcv_nxt and tp->irs 1965065Swnj * if seg contains ack then advance tp->snd_una 1975065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1985065Swnj * arrange for segment to be acked (eventually) 1995065Swnj * continue processing rest of data/controls, beginning with URG 2005065Swnj */ 2015065Swnj case TCPS_SYN_SENT: 2025065Swnj if ((tiflags & TH_ACK) && 2035065Swnj (SEQ_LEQ(ti->ti_ack, tp->iss) || 2045231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2055085Swnj goto dropwithreset; 2065065Swnj if (tiflags & TH_RST) { 2075065Swnj if (tiflags & TH_ACK) 2085085Swnj tcp_drop(tp, ECONNRESET); 2095065Swnj goto drop; 2104601Swnj } 2115065Swnj if ((tiflags & TH_SYN) == 0) 2125065Swnj goto drop; 2135231Swnj tp->snd_una = ti->ti_ack; 214*5244Sroot tp->t_timer[TCPT_REXMT] = 0; 2155065Swnj tp->irs = ti->ti_seq; 2165085Swnj tcp_rcvseqinit(tp); 2175085Swnj tp->t_flags |= TF_ACKNOW; 2185162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 219*5244Sroot soisconnected(so); 2205065Swnj tp->t_state = TCPS_ESTABLISHED; 2215162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2225231Swnj tp->snd_wl1 = ti->ti_seq; 2235162Swnj } else 2245085Swnj tp->t_state = TCPS_SYN_RECEIVED; 225*5244Sroot printf("tcp_input: out of SYN_SENT: "); 2265085Swnj goto trimthenstep6; 2275085Swnj 2285085Swnj trimthenstep6: 2295085Swnj /* 2305231Swnj * Advance ti->ti_seq to correspond to first data byte. 2315085Swnj * If data, trim to stay within window, 2325085Swnj * dropping FIN if necessary. 2335085Swnj */ 2345231Swnj ti->ti_seq++; 2355085Swnj if (ti->ti_len > tp->rcv_wnd) { 2365085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2375085Swnj m_adj(m, -todrop); 2385085Swnj ti->ti_len = tp->rcv_wnd; 2395085Swnj ti->ti_flags &= ~TH_FIN; 2405065Swnj } 241*5244Sroot printf("ti->ti_len %d\n", ti->ti_len); 242*5244Sroot pseqno(tp); 2435085Swnj goto step6; 2445065Swnj } 2454601Swnj 2465065Swnj /* 2475065Swnj * States other than LISTEN or SYN_SENT. 2485065Swnj * First check that at least some bytes of segment are within 2495065Swnj * receive window. 2505065Swnj */ 2515065Swnj if (tp->rcv_wnd == 0) { 2525065Swnj /* 2535065Swnj * If window is closed can only take segments at 2545231Swnj * window edge, and have to drop data and PUSH from 2555065Swnj * incoming segments. 2565065Swnj */ 2575065Swnj if (tp->rcv_nxt != ti->ti_seq) 2585065Swnj goto dropafterack; 2595085Swnj if (ti->ti_len > 0) { 2605085Swnj ti->ti_len = 0; 2615085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2625065Swnj } 263*5244Sroot printf("tcp_input %x: window 0, drop text and FIN\n", tp); 2645065Swnj } else { 2655065Swnj /* 2665231Swnj * If segment begins before rcv_nxt, drop leading 2675065Swnj * data (and SYN); if nothing left, just ack. 2685065Swnj */ 2695065Swnj if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 2705085Swnj todrop = tp->rcv_nxt - ti->ti_seq; 271*5244Sroot printf("tcp_input %x: drop %d dup bytes\n", tp, todrop); 2725085Swnj if (tiflags & TH_SYN) { 2735085Swnj ti->ti_seq++; 2745085Swnj if (ti->ti_urp > 1) 2755085Swnj ti->ti_urp--; 2765085Swnj else 2775085Swnj tiflags &= ~TH_URG; 2785085Swnj todrop--; 2795085Swnj } 2805065Swnj if (todrop > ti->ti_len) 2815065Swnj goto dropafterack; 2825065Swnj m_adj(m, todrop); 2835065Swnj ti->ti_seq += todrop; 2845065Swnj ti->ti_len -= todrop; 2855085Swnj if (ti->ti_urp > todrop) 2865085Swnj ti->ti_urp -= todrop; 2875085Swnj else { 2885085Swnj tiflags &= ~TH_URG; 2895085Swnj /* ti->ti_flags &= ~TH_URG; */ 2905085Swnj /* ti->ti_urp = 0; */ 2915085Swnj } 2925085Swnj /* tiflags &= ~TH_SYN; */ 2935085Swnj /* ti->ti_flags &= ~TH_SYN; */ 2945065Swnj } 2955065Swnj /* 2965065Swnj * If segment ends after window, drop trailing data 2975085Swnj * (and PUSH and FIN); if nothing left, just ACK. 2985065Swnj */ 2995065Swnj if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 3005085Swnj todrop = 3015065Swnj ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 3025065Swnj if (todrop > ti->ti_len) 3035065Swnj goto dropafterack; 3045065Swnj m_adj(m, -todrop); 3055065Swnj ti->ti_len -= todrop; 3065085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3075065Swnj } 3085065Swnj } 3094601Swnj 3105065Swnj /* 3115065Swnj * If the RST bit is set examine the state: 3125065Swnj * SYN_RECEIVED STATE: 3135065Swnj * If passive open, return to LISTEN state. 3145065Swnj * If active open, inform user that connection was refused. 3155065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3165065Swnj * Inform user that connection was reset, and close tcb. 3175065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3185065Swnj * Close the tcb. 3195065Swnj */ 3205065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3215065Swnj 3225065Swnj case TCPS_SYN_RECEIVED: 3235065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3245085Swnj tp->t_state = TCPS_LISTEN; 325*5244Sroot tp->t_timer[TCPT_KEEP] = 0; 326*5244Sroot (void) m_free(dtom(tp->t_template)); 327*5244Sroot tp->t_template = 0; 3285231Swnj in_pcbdisconnect(inp); 3295065Swnj goto drop; 3304601Swnj } 3315085Swnj tcp_drop(tp, ECONNREFUSED); 3325065Swnj goto drop; 3334601Swnj 3345065Swnj case TCPS_ESTABLISHED: 3355065Swnj case TCPS_FIN_WAIT_1: 3365065Swnj case TCPS_FIN_WAIT_2: 3375065Swnj case TCPS_CLOSE_WAIT: 3385065Swnj tcp_drop(tp, ECONNRESET); 3395065Swnj goto drop; 3405065Swnj 3415065Swnj case TCPS_CLOSING: 3425065Swnj case TCPS_LAST_ACK: 3435065Swnj case TCPS_TIME_WAIT: 3445065Swnj tcp_close(tp); 3455065Swnj goto drop; 3464601Swnj } 3474601Swnj 3484601Swnj /* 3495065Swnj * If a SYN is in the window, then this is an 3505065Swnj * error and we send an RST and drop the connection. 3514601Swnj */ 3525065Swnj if (tiflags & TH_SYN) { 3535231Swnj tcp_drop(tp, ECONNRESET); 3545085Swnj goto dropwithreset; 3554601Swnj } 3564601Swnj 3574601Swnj /* 3585065Swnj * If the ACK bit is off we drop the segment and return. 3594601Swnj */ 3605085Swnj if ((tiflags & TH_ACK) == 0) 3615065Swnj goto drop; 3625065Swnj 3635065Swnj /* 3645065Swnj * Ack processing. 3655065Swnj */ 3664601Swnj switch (tp->t_state) { 3674601Swnj 3685065Swnj /* 3695065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3705065Swnj * ESTABLISHED state and continue processing, othewise 3715065Swnj * send an RST. 3725065Swnj */ 3735065Swnj case TCPS_SYN_RECEIVED: 3745085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 3755231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 3765085Swnj goto dropwithreset; 377*5244Sroot tp->snd_una++; /* SYN acked */ 378*5244Sroot tp->t_timer[TCPT_REXMT] = 0; 3795085Swnj soisconnected(so); 3805085Swnj tp->t_state = TCPS_ESTABLISHED; 3815162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 382*5244Sroot tp->snd_wl1 = ti->ti_seq - 1; 383*5244Sroot printf("tcp_input: to ESTAB:\n"); pseqno(tp); 3845085Swnj /* fall into ... */ 3854601Swnj 3865065Swnj /* 3875065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 3885065Swnj * ACKs. If the ack is in the range 3895231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 3905065Swnj * then advance tp->snd_una to ti->ti_ack and drop 3915065Swnj * data from the retransmission queue. If this ACK reflects 3925065Swnj * more up to date window information we update our window information. 3935065Swnj */ 3945065Swnj case TCPS_ESTABLISHED: 3955065Swnj case TCPS_FIN_WAIT_1: 3965065Swnj case TCPS_FIN_WAIT_2: 3975065Swnj case TCPS_CLOSE_WAIT: 3985065Swnj case TCPS_CLOSING: 399*5244Sroot case TCPS_LAST_ACK: 400*5244Sroot case TCPS_TIME_WAIT: 4015085Swnj #define ourfinisacked (acked > 0) 4025085Swnj 403*5244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4045065Swnj break; 4055231Swnj if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4065065Swnj goto dropafterack; 4075085Swnj acked = ti->ti_ack - tp->snd_una; 408*5244Sroot printf("tcp_input: got ack of %d bytes\n", acked); 409*5244Sroot if (acked >= so->so_snd.sb_cc) { 4105085Swnj acked -= so->so_snd.sb_cc; 411*5244Sroot /* if acked > 0 our FIN is acked */ 412*5244Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 413*5244Sroot tp->t_timer[TCPT_REXMT] = 0; 4145085Swnj } else { 4155085Swnj sbdrop(&so->so_snd, acked); 4165085Swnj acked = 0; 417*5244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 418*5244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 4195085Swnj } 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)) { 427*5244Sroot if (tp->t_srtt == 0) 428*5244Sroot tp->t_srtt = tp->t_rtt; 429*5244Sroot else 430*5244Sroot tp->t_srtt = 431*5244Sroot tcp_alpha * tp->t_srtt + 432*5244Sroot (1 - tcp_alpha) * tp->t_rtt; 433*5244Sroot printf("tcp_input: rtt sampled %d, srtt now %d\n", tp->t_rtt, (int)(100* tp->t_srtt)); 4345162Swnj tp->t_rtt = 0; 4355162Swnj } 4365162Swnj 4374601Swnj switch (tp->t_state) { 4384601Swnj 4395065Swnj /* 4405065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4415065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4425085Swnj * then enter FIN_WAIT_2. 4435065Swnj */ 4445065Swnj case TCPS_FIN_WAIT_1: 4455085Swnj if (ourfinisacked) 4465085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4474601Swnj break; 4484601Swnj 4495065Swnj /* 4505065Swnj * In CLOSING STATE in addition to the processing for 4515065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4525065Swnj * then enter the TIME-WAIT state, otherwise ignore 4535065Swnj * the segment. 4545065Swnj */ 4555065Swnj case TCPS_CLOSING: 456*5244Sroot if (ourfinisacked) { 4575065Swnj tp->t_state = TCPS_TIME_WAIT; 458*5244Sroot tcp_canceltimers(tp); 459*5244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 460*5244Sroot soisdisconnected(so); 461*5244Sroot } 462*5244Sroot break; 4634601Swnj 4645065Swnj /* 4655085Swnj * The only thing that can arrive in LAST_ACK state 4665085Swnj * is an acknowledgment of our FIN. If our FIN is now 4675085Swnj * acknowledged, delete the TCB, enter the closed state 4685085Swnj * and return. 4695065Swnj */ 4705065Swnj case TCPS_LAST_ACK: 471*5244Sroot if (ourfinisacked) { 472*5244Sroot printf("tcp_input: LAST ACK close\n"); 4735065Swnj tcp_close(tp); 474*5244Sroot } 4755065Swnj goto drop; 4764601Swnj 4775065Swnj /* 4785065Swnj * In TIME_WAIT state the only thing that should arrive 4795065Swnj * is a retransmission of the remote FIN. Acknowledge 4805065Swnj * it and restart the finack timer. 4815065Swnj */ 4825065Swnj case TCPS_TIME_WAIT: 483*5244Sroot printf("tcp_input: TIME_WAIT restart timer\n"); 4845162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 4855065Swnj goto dropafterack; 4864601Swnj } 4875085Swnj #undef ourfinisacked 4885085Swnj } 4894601Swnj 4905065Swnj step6: 4915065Swnj /* 492*5244Sroot * Update window information. 493*5244Sroot */ 494*5244Sroot printf("update win wl1 %x ti->ti_seq %x wl2 %x?", tp->snd_wl1, ti->ti_seq, tp->snd_wl2); 495*5244Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 496*5244Sroot tp->snd_wl1==ti->ti_seq && SEQ_LEQ(tp->snd_wl2,ti->ti_seq)) { 497*5244Sroot printf("yes\n"); 498*5244Sroot tp->snd_wnd = ti->ti_win; 499*5244Sroot tp->snd_wl1 = ti->ti_seq; 500*5244Sroot tp->snd_wl2 = ti->ti_ack; 501*5244Sroot if (tp->snd_wnd > 0) 502*5244Sroot tp->t_timer[TCPT_PERSIST] = 0; 503*5244Sroot } 504*5244Sroot else printf("no\n"); 505*5244Sroot 506*5244Sroot /* 5075065Swnj * If an URG bit is set in the segment and is greater than the 5085065Swnj * current known urgent pointer, then signal the user that the 509*5244Sroot * remote side has out of band data. This should not happen 5105065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 5115065Swnj * a FIN has been received from the remote side. In these states 5125065Swnj * we ignore the URG. 5135065Swnj */ 5145085Swnj if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 5155085Swnj if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 5165065Swnj tp->rcv_up = ti->ti_urp; 5175085Swnj #if 0 518*5244Sroot sohasoutofband(so); /* XXX */ 5195085Swnj #endif 5204601Swnj } 5214601Swnj 5224601Swnj /* 5235065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5245065Swnj * and arranging for acknowledgment of receipt if necessary. 5255065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5265065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5275065Swnj * case PRU_RCVD). If a FIN has already been received on this 5285065Swnj * connection then we just ignore the text. 5294601Swnj */ 530*5244Sroot if (ti->ti_len && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5315085Swnj off += sizeof (struct ip); /* drop IP header */ 5325085Swnj m->m_off += off; 5335085Swnj m->m_len -= off; 5345065Swnj tiflags = tcp_reass(tp, ti); 5355085Swnj tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 536*5244Sroot } else { 5374924Swnj m_freem(m); 538*5244Sroot } 5394601Swnj 5404601Swnj /* 5415065Swnj * If FIN is received then if we haven't received SYN and 5425065Swnj * therefore can't validate drop the segment. Otherwise ACK 5435065Swnj * the FIN and let the user know that the connection is closing. 5444601Swnj */ 5455085Swnj if ((tiflags & TH_FIN)) { 5465074Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 5475074Swnj goto drop; 548*5244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 549*5244Sroot socantrcvmore(so); 550*5244Sroot tp->t_flags |= TF_ACKNOW; 551*5244Sroot tp->rcv_nxt++; 552*5244Sroot } 553*5244Sroot printf("tcp_input: %x got FIN\n", tp); 5545065Swnj switch (tp->t_state) { 5554601Swnj 5565065Swnj /* 5575065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 5585065Swnj * enter the CLOSE_WAIT state. 5594884Swnj */ 5605065Swnj case TCPS_SYN_RECEIVED: 5615065Swnj case TCPS_ESTABLISHED: 5625065Swnj tp->t_state = TCPS_CLOSE_WAIT; 5635065Swnj break; 5644884Swnj 5655065Swnj /* 5665085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 5675085Swnj * enter the CLOSING state. 5684884Swnj */ 5695065Swnj case TCPS_FIN_WAIT_1: 5705085Swnj tp->t_state = TCPS_CLOSING; 5715065Swnj break; 5724601Swnj 5735065Swnj /* 5745065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 5755065Swnj * starting the time-wait timer, turning off the other 5765065Swnj * standard timers. 5775065Swnj */ 5785065Swnj case TCPS_FIN_WAIT_2: 579*5244Sroot tp->t_state = TCPS_TIME_WAIT; 5805074Swnj tcp_canceltimers(tp); 5815162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 582*5244Sroot soisdisconnected(so); 5835065Swnj break; 5845065Swnj 5854884Swnj /* 5865065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 5874884Swnj */ 5885065Swnj case TCPS_TIME_WAIT: 5895162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5905065Swnj break; 5915085Swnj } 5924601Swnj } 5935085Swnj 5945085Swnj /* 5955085Swnj * Return any desired output. 5965085Swnj */ 5975085Swnj tcp_output(tp); 5985065Swnj return; 5995085Swnj 6005065Swnj dropafterack: 6015085Swnj /* 602*5244Sroot * Generate an ACK dropping incoming segment. 6035085Swnj * Make ACK reflect our state. 6045085Swnj */ 605*5244Sroot printf("tcp_input: dropafterack\n"); 6065085Swnj if (tiflags & TH_RST) 6075085Swnj goto drop; 6085085Swnj tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6095231Swnj return; 6105085Swnj 6115085Swnj dropwithreset: 612*5244Sroot printf("tcp_input: dropwithreset\n"); 6135085Swnj /* 614*5244Sroot * Generate a RST, dropping incoming segment. 6155085Swnj * Make ACK acceptable to originator of segment. 6165085Swnj */ 6175085Swnj if (tiflags & TH_RST) 6185085Swnj goto drop; 6195085Swnj if (tiflags & TH_ACK) 6205109Swnj tcp_respond(ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6215085Swnj else { 6225085Swnj if (tiflags & TH_SYN) 6235085Swnj ti->ti_len++; 6245109Swnj tcp_respond(ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6255085Swnj } 6265231Swnj return; 6275085Swnj 6285065Swnj drop: 629*5244Sroot printf("tcp_input: drop\n"); 6305085Swnj /* 6315085Swnj * Drop space held by incoming segment and return. 6325085Swnj */ 6335065Swnj m_freem(m); 6345065Swnj } 6355065Swnj 6365065Swnj /* 6375065Swnj * Insert segment ti into reassembly queue of tcp with 6385065Swnj * control block tp. Return TH_FIN if reassembly now includes 6395065Swnj * a segment with FIN. 6405065Swnj */ 6415109Swnj tcp_reass(tp, ti) 6425065Swnj register struct tcpcb *tp; 6435065Swnj register struct tcpiphdr *ti; 6445065Swnj { 6455065Swnj register struct tcpiphdr *q; 6465085Swnj struct socket *so = tp->t_inpcb->inp_socket; 6475065Swnj int flags = 0; /* no FIN */ 6485085Swnj COUNT(TCP_REASS); 6495065Swnj 6505065Swnj /* 6515162Swnj * Call with ti==0 after become established to 6525162Swnj * force pre-ESTABLISHED data up to user socket. 6535065Swnj */ 6545162Swnj if (ti == 0) 6555065Swnj goto present; 6564601Swnj 6575065Swnj /* 6585065Swnj * Find a segment which begins after this one does. 6595065Swnj */ 6605065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 6615065Swnj q = (struct tcpiphdr *)q->ti_next) 6625065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 6635065Swnj break; 6644601Swnj 6655065Swnj /* 6665065Swnj * If there is a preceding segment, it may provide some of 6675065Swnj * our data already. If so, drop the data from the incoming 6685065Swnj * segment. If it provides all of our data, drop us. 6695065Swnj */ 6705065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 6715065Swnj register int i; 6725065Swnj q = (struct tcpiphdr *)(q->ti_prev); 6735065Swnj /* conversion to int (in i) handles seq wraparound */ 6745065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 6755065Swnj if (i > 0) { 6764924Swnj if (i >= ti->ti_len) 6775065Swnj goto drop; 6785065Swnj m_adj(dtom(tp), i); 6795065Swnj ti->ti_len -= i; 6804924Swnj ti->ti_seq += i; 6814601Swnj } 6825065Swnj q = (struct tcpiphdr *)(q->ti_next); 6835065Swnj } 6844601Swnj 6855065Swnj /* 6865065Swnj * While we overlap succeeding segments trim them or, 6875065Swnj * if they are completely covered, dequeue them. 6885065Swnj */ 6895065Swnj while (q != (struct tcpiphdr *)tp && 6905065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 6915065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 6925065Swnj if (i < q->ti_len) { 6935065Swnj q->ti_len -= i; 6945065Swnj m_adj(dtom(q), i); 6955065Swnj break; 6964601Swnj } 6975065Swnj q = (struct tcpiphdr *)q->ti_next; 6985065Swnj m_freem(dtom(q->ti_prev)); 6995065Swnj remque(q->ti_prev); 7005065Swnj } 7014601Swnj 7025065Swnj /* 7035065Swnj * Stick new segment in its place. 7045065Swnj */ 7055065Swnj insque(ti, q->ti_prev); 7064601Swnj 7075065Swnj present: 7085065Swnj /* 709*5244Sroot * Present data to user, advancing rcv_nxt through 710*5244Sroot * completed sequence space. 7115065Swnj */ 7125085Swnj if (tp->t_state < TCPS_ESTABLISHED) 713*5244Sroot return (0); 7144924Swnj ti = tp->seg_next; 715*5244Sroot while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt) { 716*5244Sroot tp->rcv_nxt += ti->ti_len; 717*5244Sroot flags = ti->ti_flags & TH_FIN; 718*5244Sroot printf("move %d bytes to user; rcv_nxt now %x\n", ti->ti_len, tp->rcv_nxt); 7194924Swnj remque(ti); 7204924Swnj sbappend(&so->so_rcv, dtom(ti)); 7214924Swnj ti = (struct tcpiphdr *)ti->ti_next; 7224601Swnj } 7235074Swnj if (so->so_state & SS_CANTRCVMORE) 7245074Swnj sbflush(&so->so_rcv); 7255074Swnj else 7265074Swnj sorwakeup(so); 7275065Swnj return (flags); 7285065Swnj drop: 729*5244Sroot printf("tcp_reass drop\n"); 7305065Swnj m_freem(dtom(ti)); 7315065Swnj return (flags); 7324601Swnj } 733