1*5951Swnj /* tcp_input.c 1.54 82/02/25 */ 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; 285440Swnj extern tcpnodelack; 294601Swnj 305267Sroot struct tcpcb *tcp_newtcpcb(); 315065Swnj /* 325065Swnj * TCP input routine, follows pages 65-76 of the 335065Swnj * protocol specification dated September, 1981 very closely. 345065Swnj */ 354924Swnj tcp_input(m0) 364924Swnj struct mbuf *m0; 374601Swnj { 384924Swnj register struct tcpiphdr *ti; 394924Swnj struct inpcb *inp; 404924Swnj register struct mbuf *m; 415440Swnj struct mbuf *om = 0; 424924Swnj int len, tlen, off; 435391Swnj register struct tcpcb *tp = 0; 444924Swnj register int tiflags; 454803Swnj struct socket *so; 465109Swnj int todrop, acked; 475267Sroot short ostate; 484924Swnj 494601Swnj COUNT(TCP_INPUT); 504924Swnj /* 515244Sroot * Get IP and TCP header together in first mbuf. 525244Sroot * Note: IP leaves IP header in first mbuf. 534924Swnj */ 544924Swnj m = m0; 555020Sroot ti = mtod(m, struct tcpiphdr *); 565244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 575208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 585307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 595307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 605085Swnj tcpstat.tcps_hdrops++; 615307Sroot return; 625085Swnj } 635085Swnj ti = mtod(m, struct tcpiphdr *); 645085Swnj } 654601Swnj 664601Swnj /* 675244Sroot * Checksum extended TCP header and data. 684601Swnj */ 694924Swnj tlen = ((struct ip *)ti)->ip_len; 704924Swnj len = sizeof (struct ip) + tlen; 714679Swnj if (tcpcksum) { 724924Swnj ti->ti_next = ti->ti_prev = 0; 734924Swnj ti->ti_x1 = 0; 745223Swnj ti->ti_len = (u_short)tlen; 755223Swnj #if vax 765223Swnj ti->ti_len = htons(ti->ti_len); 775223Swnj #endif 785231Swnj if (ti->ti_sum = in_cksum(m, len)) { 794924Swnj tcpstat.tcps_badsum++; 805065Swnj printf("tcp cksum %x\n", ti->ti_sum); 815085Swnj goto drop; 824601Swnj } 834601Swnj } 844601Swnj 854601Swnj /* 865244Sroot * Check that TCP offset makes sense, 875440Swnj * pull out TCP options and adjust length. 884601Swnj */ 894924Swnj off = ti->ti_off << 2; 905231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 914924Swnj tcpstat.tcps_badoff++; 925085Swnj goto drop; 934924Swnj } 944924Swnj ti->ti_len = tlen - off; 955440Swnj if (off > sizeof (struct tcphdr)) { 965440Swnj if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 975440Swnj tcpstat.tcps_hdrops++; 985440Swnj goto drop; 995440Swnj } 1005440Swnj ti = mtod(m, struct tcpiphdr *); 1015440Swnj om = m_get(M_DONTWAIT); 1025440Swnj if (om == 0) 1035440Swnj goto drop; 1045440Swnj om->m_off = MMINOFF; 1055440Swnj om->m_len = off - sizeof (struct tcphdr); 1065440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 1075440Swnj bcopy(op, mtod(om, caddr_t), om->m_len); 1085440Swnj m->m_len -= om->m_len; 1095440Swnj bcopy(op+om->m_len, op, m->m_len-sizeof (struct tcpiphdr)); 1105440Swnj } 1115440Swnj } 1125065Swnj tiflags = ti->ti_flags; 1134924Swnj 1145231Swnj #if vax 1154924Swnj /* 1165244Sroot * Convert TCP protocol specific fields to host format. 1175085Swnj */ 1185085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1195085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1205085Swnj ti->ti_win = ntohs(ti->ti_win); 1215085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1225231Swnj #endif 1235085Swnj 1245085Swnj /* 1254924Swnj * Locate pcb for segment. 1264924Swnj */ 1275065Swnj inp = in_pcblookup 1285065Swnj (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 1295065Swnj 1305065Swnj /* 1315065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1325244Sroot * all data in the incoming segment is discarded. 1335065Swnj */ 1345300Sroot if (inp == 0) 1355085Swnj goto dropwithreset; 1365065Swnj tp = intotcpcb(inp); 1375300Sroot if (tp == 0) 1385085Swnj goto dropwithreset; 1395109Swnj so = inp->inp_socket; 1405267Sroot if (so->so_options & SO_DEBUG) { 1415267Sroot ostate = tp->t_state; 1425267Sroot tcp_saveti = *ti; 1435267Sroot } 1444601Swnj 1454601Swnj /* 1465162Swnj * Segment received on connection. 1475162Swnj * Reset idle time and keep-alive timer. 1485162Swnj */ 1495162Swnj tp->t_idle = 0; 1505162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1515162Swnj 1525162Swnj /* 1535440Swnj * Process options. 1545440Swnj */ 1555440Swnj if (om) { 1565440Swnj tcp_dooptions(tp, om); 1575440Swnj om = 0; 1585440Swnj } 1595440Swnj 1605440Swnj /* 1615085Swnj * Calculate amount of space in receive window, 1625085Swnj * and then do TCP input processing. 1634601Swnj */ 1645085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1655231Swnj if (tp->rcv_wnd < 0) 1665231Swnj tp->rcv_wnd = 0; 1674601Swnj 1684601Swnj switch (tp->t_state) { 1694601Swnj 1705065Swnj /* 1715065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1725065Swnj * If the segment contains an ACK then it is bad and send a RST. 1735065Swnj * If it does not contain a SYN then it is not interesting; drop it. 1745085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1755065Swnj * tp->iss, and send a segment: 1765085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1775065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1785065Swnj * Fill in remote peer address fields if not previously specified. 1795065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1805244Sroot * segment in this state. 1815065Swnj */ 1825065Swnj case TCPS_LISTEN: 1835065Swnj if (tiflags & TH_RST) 1845065Swnj goto drop; 1855300Sroot if (tiflags & TH_ACK) 1865085Swnj goto dropwithreset; 1875300Sroot if ((tiflags & TH_SYN) == 0) 1885065Swnj goto drop; 1895244Sroot tcp_in.sin_addr = ti->ti_src; 1905244Sroot tcp_in.sin_port = ti->ti_sport; 1915300Sroot if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in)) 1925244Sroot goto drop; 1935244Sroot tp->t_template = tcp_template(tp); 1945244Sroot if (tp->t_template == 0) { 1955244Sroot in_pcbdisconnect(inp); 1965244Sroot goto drop; 1975244Sroot } 1985085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1995065Swnj tp->irs = ti->ti_seq; 2005085Swnj tcp_sendseqinit(tp); 2015085Swnj tcp_rcvseqinit(tp); 2025065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2035244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 2045085Swnj goto trimthenstep6; 2054601Swnj 2065065Swnj /* 2075065Swnj * If the state is SYN_SENT: 2085065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2095065Swnj * if seg contains a RST, then drop the connection. 2105065Swnj * if seg does not contain SYN, then drop it. 2115065Swnj * Otherwise this is an acceptable SYN segment 2125065Swnj * initialize tp->rcv_nxt and tp->irs 2135065Swnj * if seg contains ack then advance tp->snd_una 2145065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2155065Swnj * arrange for segment to be acked (eventually) 2165065Swnj * continue processing rest of data/controls, beginning with URG 2175065Swnj */ 2185065Swnj case TCPS_SYN_SENT: 2195065Swnj if ((tiflags & TH_ACK) && 2205300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2215300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2225231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2235085Swnj goto dropwithreset; 2245065Swnj if (tiflags & TH_RST) { 2255065Swnj if (tiflags & TH_ACK) 2265267Sroot tcp_drop(tp, ECONNREFUSED); 2275065Swnj goto drop; 2284601Swnj } 2295065Swnj if ((tiflags & TH_SYN) == 0) 2305065Swnj goto drop; 2315231Swnj tp->snd_una = ti->ti_ack; 2325357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2335357Sroot tp->snd_nxt = tp->snd_una; 2345244Sroot tp->t_timer[TCPT_REXMT] = 0; 2355065Swnj tp->irs = ti->ti_seq; 2365085Swnj tcp_rcvseqinit(tp); 2375085Swnj tp->t_flags |= TF_ACKNOW; 2385162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2395391Swnj if (so->so_options & SO_ACCEPTCONN) 2405391Swnj so->so_state |= SS_CONNAWAITING; 2415244Sroot soisconnected(so); 2425065Swnj tp->t_state = TCPS_ESTABLISHED; 2435162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2445162Swnj } else 2455085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2465085Swnj goto trimthenstep6; 2475085Swnj 2485085Swnj trimthenstep6: 2495085Swnj /* 2505231Swnj * Advance ti->ti_seq to correspond to first data byte. 2515085Swnj * If data, trim to stay within window, 2525085Swnj * dropping FIN if necessary. 2535085Swnj */ 2545231Swnj ti->ti_seq++; 2555085Swnj if (ti->ti_len > tp->rcv_wnd) { 2565085Swnj todrop = ti->ti_len - tp->rcv_wnd; 2575085Swnj m_adj(m, -todrop); 2585085Swnj ti->ti_len = tp->rcv_wnd; 2595085Swnj ti->ti_flags &= ~TH_FIN; 2605065Swnj } 2615263Swnj tp->snd_wl1 = ti->ti_seq - 1; 2625085Swnj goto step6; 2635065Swnj } 2644601Swnj 2655065Swnj /* 2665065Swnj * States other than LISTEN or SYN_SENT. 2675065Swnj * First check that at least some bytes of segment are within 2685065Swnj * receive window. 2695065Swnj */ 2705065Swnj if (tp->rcv_wnd == 0) { 2715065Swnj /* 2725065Swnj * If window is closed can only take segments at 2735231Swnj * window edge, and have to drop data and PUSH from 2745065Swnj * incoming segments. 2755065Swnj */ 2765300Sroot if (tp->rcv_nxt != ti->ti_seq) 2775065Swnj goto dropafterack; 2785085Swnj if (ti->ti_len > 0) { 2795690Swnj m_adj(m, ti->ti_len); 2805085Swnj ti->ti_len = 0; 2815085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2825065Swnj } 2835065Swnj } else { 2845065Swnj /* 2855231Swnj * If segment begins before rcv_nxt, drop leading 2865065Swnj * data (and SYN); if nothing left, just ack. 2875065Swnj */ 2885690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 2895690Swnj if (todrop > 0) { 2905085Swnj if (tiflags & TH_SYN) { 2915300Sroot tiflags &= ~TH_SYN; 2925690Swnj ti->ti_flags &= ~TH_SYN; 2935085Swnj ti->ti_seq++; 2945085Swnj if (ti->ti_urp > 1) 2955085Swnj ti->ti_urp--; 2965085Swnj else 2975085Swnj tiflags &= ~TH_URG; 2985085Swnj todrop--; 2995085Swnj } 3005300Sroot if (todrop > ti->ti_len) 3015065Swnj goto dropafterack; 3025065Swnj m_adj(m, todrop); 3035065Swnj ti->ti_seq += todrop; 3045065Swnj ti->ti_len -= todrop; 3055085Swnj if (ti->ti_urp > todrop) 3065085Swnj ti->ti_urp -= todrop; 3075085Swnj else { 3085085Swnj tiflags &= ~TH_URG; 3095690Swnj ti->ti_flags &= ~TH_URG; 3105690Swnj ti->ti_urp = 0; 3115085Swnj } 3125065Swnj } 3135065Swnj /* 3145065Swnj * If segment ends after window, drop trailing data 3155085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3165065Swnj */ 3175690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3185690Swnj if (todrop > 0) { 3195300Sroot if (todrop > ti->ti_len) 3205065Swnj goto dropafterack; 3215065Swnj m_adj(m, -todrop); 3225065Swnj ti->ti_len -= todrop; 3235085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3245065Swnj } 3255065Swnj } 3264601Swnj 3275065Swnj /* 328*5951Swnj * If a segment is received on a connection after the 329*5951Swnj * user processes are gone, then RST the other end. 330*5951Swnj */ 331*5951Swnj if (so->so_state & SS_USERGONE) { 332*5951Swnj tcp_close(tp); 333*5951Swnj goto dropwithreset; 334*5951Swnj } 335*5951Swnj 336*5951Swnj /* 3375065Swnj * If the RST bit is set examine the state: 3385065Swnj * SYN_RECEIVED STATE: 3395065Swnj * If passive open, return to LISTEN state. 3405065Swnj * If active open, inform user that connection was refused. 3415065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3425065Swnj * Inform user that connection was reset, and close tcb. 3435065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3445065Swnj * Close the tcb. 3455065Swnj */ 3465065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 3475267Sroot 3485065Swnj case TCPS_SYN_RECEIVED: 3495065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 3505267Sroot /* a miniature tcp_close, but invisible to user */ 3515267Sroot (void) m_free(dtom(tp->t_template)); 3525267Sroot (void) m_free(dtom(tp)); 3535267Sroot inp->inp_ppcb = 0; 3545267Sroot tp = tcp_newtcpcb(inp); 3555085Swnj tp->t_state = TCPS_LISTEN; 3565065Swnj goto drop; 3574601Swnj } 3585085Swnj tcp_drop(tp, ECONNREFUSED); 3595065Swnj goto drop; 3604601Swnj 3615065Swnj case TCPS_ESTABLISHED: 3625065Swnj case TCPS_FIN_WAIT_1: 3635065Swnj case TCPS_FIN_WAIT_2: 3645065Swnj case TCPS_CLOSE_WAIT: 3655065Swnj tcp_drop(tp, ECONNRESET); 3665065Swnj goto drop; 3675065Swnj 3685065Swnj case TCPS_CLOSING: 3695065Swnj case TCPS_LAST_ACK: 3705065Swnj case TCPS_TIME_WAIT: 3715065Swnj tcp_close(tp); 3725065Swnj goto drop; 3734601Swnj } 3744601Swnj 3754601Swnj /* 3765065Swnj * If a SYN is in the window, then this is an 3775065Swnj * error and we send an RST and drop the connection. 3784601Swnj */ 3795065Swnj if (tiflags & TH_SYN) { 3805231Swnj tcp_drop(tp, ECONNRESET); 3815085Swnj goto dropwithreset; 3824601Swnj } 3834601Swnj 3844601Swnj /* 3855065Swnj * If the ACK bit is off we drop the segment and return. 3864601Swnj */ 3875085Swnj if ((tiflags & TH_ACK) == 0) 3885065Swnj goto drop; 3895065Swnj 3905065Swnj /* 3915065Swnj * Ack processing. 3925065Swnj */ 3934601Swnj switch (tp->t_state) { 3944601Swnj 3955065Swnj /* 3965065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3975065Swnj * ESTABLISHED state and continue processing, othewise 3985065Swnj * send an RST. 3995065Swnj */ 4005065Swnj case TCPS_SYN_RECEIVED: 4015085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4025231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4035085Swnj goto dropwithreset; 4045244Sroot tp->snd_una++; /* SYN acked */ 4055357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4065357Sroot tp->snd_nxt = tp->snd_una; 4075244Sroot tp->t_timer[TCPT_REXMT] = 0; 4085391Swnj if (so->so_options & SO_ACCEPTCONN) 4095391Swnj so->so_state |= SS_CONNAWAITING; 4105085Swnj soisconnected(so); 4115085Swnj tp->t_state = TCPS_ESTABLISHED; 4125162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4135244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4145085Swnj /* fall into ... */ 4154601Swnj 4165065Swnj /* 4175065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4185065Swnj * ACKs. If the ack is in the range 4195231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4205065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4215065Swnj * data from the retransmission queue. If this ACK reflects 4225065Swnj * more up to date window information we update our window information. 4235065Swnj */ 4245065Swnj case TCPS_ESTABLISHED: 4255065Swnj case TCPS_FIN_WAIT_1: 4265065Swnj case TCPS_FIN_WAIT_2: 4275065Swnj case TCPS_CLOSE_WAIT: 4285065Swnj case TCPS_CLOSING: 4295244Sroot case TCPS_LAST_ACK: 4305244Sroot case TCPS_TIME_WAIT: 4315085Swnj #define ourfinisacked (acked > 0) 4325085Swnj 4335244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4345065Swnj break; 4355300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4365065Swnj goto dropafterack; 4375085Swnj acked = ti->ti_ack - tp->snd_una; 438*5951Swnj 439*5951Swnj /* 440*5951Swnj * If transmit timer is running and timed sequence 441*5951Swnj * number was acked, update smoothed round trip time. 442*5951Swnj */ 443*5951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 444*5951Swnj if (tp->t_srtt == 0) 445*5951Swnj tp->t_srtt = tp->t_rtt; 446*5951Swnj else 447*5951Swnj tp->t_srtt = 448*5951Swnj tcp_alpha * tp->t_srtt + 449*5951Swnj (1 - tcp_alpha) * tp->t_rtt; 450*5951Swnj /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */ 451*5951Swnj tp->t_rtt = 0; 452*5951Swnj } 453*5951Swnj 4545307Sroot if (ti->ti_ack == tp->snd_max) 4555244Sroot tp->t_timer[TCPT_REXMT] = 0; 4565307Sroot else { 4575244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4585244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 459*5951Swnj tp->t_rtt = 1; 4605300Sroot tp->t_rxtshift = 0; 4615085Swnj } 4625307Sroot if (acked > so->so_snd.sb_cc) { 4635307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 4645307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 4655307Sroot } else { 4665307Sroot sbdrop(&so->so_snd.sb_cc, acked); 4675307Sroot tp->snd_wnd -= acked; 4685307Sroot acked = 0; 4695307Sroot } 4705300Sroot if (so->so_snd.sb_flags & SB_WAIT) 4715300Sroot sowwakeup(so); 4725231Swnj tp->snd_una = ti->ti_ack; 4735357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4745357Sroot tp->snd_nxt = tp->snd_una; 4755162Swnj 4764601Swnj switch (tp->t_state) { 4774601Swnj 4785065Swnj /* 4795065Swnj * In FIN_WAIT_1 STATE in addition to the processing 4805065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 4815085Swnj * then enter FIN_WAIT_2. 4825065Swnj */ 4835065Swnj case TCPS_FIN_WAIT_1: 4845896Swnj if (ourfinisacked) { 4855896Swnj /* 4865896Swnj * If we can't receive any more 4875896Swnj * data, then closing user can proceed. 4885896Swnj */ 4895896Swnj if (so->so_state & SS_CANTRCVMORE) 4905896Swnj soisdisconnected(so); 4915085Swnj tp->t_state = TCPS_FIN_WAIT_2; 4925896Swnj } 4934601Swnj break; 4944601Swnj 4955065Swnj /* 4965065Swnj * In CLOSING STATE in addition to the processing for 4975065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 4985065Swnj * then enter the TIME-WAIT state, otherwise ignore 4995065Swnj * the segment. 5005065Swnj */ 5015065Swnj case TCPS_CLOSING: 5025244Sroot if (ourfinisacked) { 5035065Swnj tp->t_state = TCPS_TIME_WAIT; 5045244Sroot tcp_canceltimers(tp); 5055244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5065244Sroot soisdisconnected(so); 5075244Sroot } 5085244Sroot break; 5094601Swnj 5105065Swnj /* 5115085Swnj * The only thing that can arrive in LAST_ACK state 5125085Swnj * is an acknowledgment of our FIN. If our FIN is now 5135085Swnj * acknowledged, delete the TCB, enter the closed state 5145085Swnj * and return. 5155065Swnj */ 5165065Swnj case TCPS_LAST_ACK: 5175251Sroot if (ourfinisacked) 5185065Swnj tcp_close(tp); 5195065Swnj goto drop; 5204601Swnj 5215065Swnj /* 5225065Swnj * In TIME_WAIT state the only thing that should arrive 5235065Swnj * is a retransmission of the remote FIN. Acknowledge 5245065Swnj * it and restart the finack timer. 5255065Swnj */ 5265065Swnj case TCPS_TIME_WAIT: 5275162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5285065Swnj goto dropafterack; 5294601Swnj } 5305085Swnj #undef ourfinisacked 5315085Swnj } 5324601Swnj 5335065Swnj step6: 5345065Swnj /* 5355244Sroot * Update window information. 5365244Sroot */ 5375300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5385391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5395300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5405244Sroot tp->snd_wnd = ti->ti_win; 5415244Sroot tp->snd_wl1 = ti->ti_seq; 5425244Sroot tp->snd_wl2 = ti->ti_ack; 5435244Sroot if (tp->snd_wnd > 0) 5445244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5455244Sroot } 5465244Sroot 5475244Sroot /* 5485547Swnj * Process segments with URG. 5495065Swnj */ 5505547Swnj if ((tiflags & TH_URG) && TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5515547Swnj /* 5525547Swnj * If this segment advances the known urgent pointer, 5535547Swnj * then mark the data stream. This should not happen 5545547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5555547Swnj * a FIN has been received from the remote side. 5565547Swnj * In these states we ignore the URG. 5575547Swnj */ 5585547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 5595547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 5605547Swnj so->so_oobmark = so->so_rcv.sb_cc + 5615547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 5625547Swnj if (so->so_oobmark == 0) 5635547Swnj so->so_state |= SS_RCVATMARK; 5645440Swnj #ifdef TCPTRUEOOB 5655547Swnj if ((tp->t_flags & TF_DOOOB) == 0) 5665440Swnj #endif 5675547Swnj sohasoutofband(so); 5685547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 5695440Swnj } 5705547Swnj /* 5715547Swnj * Remove out of band data so doesn't get presented to user. 5725547Swnj * This can happen independent of advancing the URG pointer, 5735547Swnj * but if two URG's are pending at once, some out-of-band 5745547Swnj * data may creep in... ick. 5755547Swnj */ 5765547Swnj if (ti->ti_urp <= ti->ti_len) { 5775547Swnj tcp_pulloutofband(so, ti); 5785547Swnj } 5795419Swnj } 5804601Swnj 5814601Swnj /* 5825065Swnj * Process the segment text, merging it into the TCP sequencing queue, 5835065Swnj * and arranging for acknowledgment of receipt if necessary. 5845065Swnj * This process logically involves adjusting tp->rcv_wnd as data 5855065Swnj * is presented to the user (this happens in tcp_usrreq.c, 5865065Swnj * case PRU_RCVD). If a FIN has already been received on this 5875065Swnj * connection then we just ignore the text. 5884601Swnj */ 5895263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 5905263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5915085Swnj off += sizeof (struct ip); /* drop IP header */ 5925085Swnj m->m_off += off; 5935085Swnj m->m_len -= off; 5945065Swnj tiflags = tcp_reass(tp, ti); 5955440Swnj if (tcpnodelack == 0) 5965440Swnj tp->t_flags |= TF_DELACK; 5975440Swnj else 5985440Swnj tp->t_flags |= TF_ACKNOW; 5995244Sroot } else { 6004924Swnj m_freem(m); 6015263Swnj tiflags &= ~TH_FIN; 6025244Sroot } 6034601Swnj 6044601Swnj /* 6055263Swnj * If FIN is received ACK the FIN and let the user know 6065263Swnj * that the connection is closing. 6074601Swnj */ 6085263Swnj if (tiflags & TH_FIN) { 6095244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6105244Sroot socantrcvmore(so); 6115244Sroot tp->t_flags |= TF_ACKNOW; 6125244Sroot tp->rcv_nxt++; 6135244Sroot } 6145065Swnj switch (tp->t_state) { 6154601Swnj 6165065Swnj /* 6175065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6185065Swnj * enter the CLOSE_WAIT state. 6194884Swnj */ 6205065Swnj case TCPS_SYN_RECEIVED: 6215065Swnj case TCPS_ESTABLISHED: 6225065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6235065Swnj break; 6244884Swnj 6255065Swnj /* 6265085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6275085Swnj * enter the CLOSING state. 6284884Swnj */ 6295065Swnj case TCPS_FIN_WAIT_1: 6305085Swnj tp->t_state = TCPS_CLOSING; 6315065Swnj break; 6324601Swnj 6335065Swnj /* 6345065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6355065Swnj * starting the time-wait timer, turning off the other 6365065Swnj * standard timers. 6375065Swnj */ 6385065Swnj case TCPS_FIN_WAIT_2: 6395244Sroot tp->t_state = TCPS_TIME_WAIT; 6405074Swnj tcp_canceltimers(tp); 6415162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6425244Sroot soisdisconnected(so); 6435065Swnj break; 6445065Swnj 6454884Swnj /* 6465065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6474884Swnj */ 6485065Swnj case TCPS_TIME_WAIT: 6495162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6505065Swnj break; 6515085Swnj } 6524601Swnj } 6535267Sroot if (so->so_options & SO_DEBUG) 6545267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6555085Swnj 6565085Swnj /* 6575085Swnj * Return any desired output. 6585085Swnj */ 6595085Swnj tcp_output(tp); 6605065Swnj return; 6615085Swnj 6625065Swnj dropafterack: 6635085Swnj /* 6645244Sroot * Generate an ACK dropping incoming segment. 6655085Swnj * Make ACK reflect our state. 6665085Swnj */ 6675085Swnj if (tiflags & TH_RST) 6685085Swnj goto drop; 6695391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 6705231Swnj return; 6715085Swnj 6725085Swnj dropwithreset: 6735440Swnj if (om) 6745440Swnj m_free(om); 6755085Swnj /* 6765244Sroot * Generate a RST, dropping incoming segment. 6775085Swnj * Make ACK acceptable to originator of segment. 6785085Swnj */ 6795085Swnj if (tiflags & TH_RST) 6805085Swnj goto drop; 6815085Swnj if (tiflags & TH_ACK) 6825391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 6835085Swnj else { 6845085Swnj if (tiflags & TH_SYN) 6855085Swnj ti->ti_len++; 6865391Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, TH_RST|TH_ACK); 6875085Swnj } 6885231Swnj return; 6895085Swnj 6905065Swnj drop: 6915085Swnj /* 6925085Swnj * Drop space held by incoming segment and return. 6935085Swnj */ 6945065Swnj m_freem(m); 6955267Sroot return; 6965065Swnj } 6975065Swnj 6985440Swnj tcp_dooptions(tp, om) 6995440Swnj struct tcpcb *tp; 7005440Swnj struct mbuf *om; 7015419Swnj { 7025440Swnj register u_char *cp; 7035440Swnj int opt, optlen, cnt; 7045419Swnj 7055440Swnj cp = mtod(om, u_char *); 7065440Swnj cnt = om->m_len; 7075440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7085440Swnj opt = cp[0]; 7095440Swnj if (opt == TCPOPT_EOL) 7105440Swnj break; 7115440Swnj if (opt == TCPOPT_NOP) 7125440Swnj optlen = 1; 7135440Swnj else 7145440Swnj optlen = cp[1]; 7155440Swnj switch (opt) { 7165440Swnj 7175440Swnj default: 7185440Swnj break; 7195440Swnj 7205440Swnj case TCPOPT_MAXSEG: 7215440Swnj if (optlen != 4) 7225440Swnj continue; 7235440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7245440Swnj #if vax 7255440Swnj tp->t_maxseg = ntohs(tp->t_maxseg); 7265440Swnj #endif 7275440Swnj break; 7285440Swnj 7295440Swnj #ifdef TCPTRUEOOB 7305440Swnj case TCPOPT_WILLOOB: 7315440Swnj tp->t_flags |= TF_DOOOB; 7325440Swnj printf("tp %x dooob\n", tp); 7335440Swnj break; 7345440Swnj 7355440Swnj case TCPOPT_OOBDATA: { 7365440Swnj int seq; 7375547Swnj register struct socket *so = tp->t_inpcb->inp_socket; 7385547Swnj tcp_seq mark; 7395440Swnj 7405547Swnj if (optlen != 8) 7415440Swnj continue; 7425440Swnj seq = cp[2]; 7435440Swnj if (seq < tp->t_iobseq) 7445440Swnj seq += 256; 7455440Swnj printf("oobdata cp[2] %d iobseq %d seq %d\n", cp[2], tp->t_iobseq, seq); 7465440Swnj if (seq - tp->t_iobseq > 128) { 7475440Swnj printf("bad seq\n"); 7485440Swnj tp->t_oobflags |= TCPOOB_OWEACK; 7495440Swnj break; 7505440Swnj } 7515440Swnj tp->t_iobseq = cp[2]; 7525440Swnj tp->t_iobc = cp[3]; 7535547Swnj mark = *(tcp_seq *)(cp + 4); 7545547Swnj #if vax 7555547Swnj mark = ntohl(mark); 7565547Swnj #endif 7575547Swnj so->so_oobmark = so->so_rcv.sb_cc + (mark-tp->rcv_nxt); 7585547Swnj if (so->so_oobmark == 0) 7595547Swnj so->so_state |= SS_RCVATMARK; 7605440Swnj printf("take oob data %x input iobseq now %x\n", tp->t_iobc, tp->t_iobseq); 7615547Swnj sohasoutofband(so); 7625440Swnj break; 7635419Swnj } 7645440Swnj 7655440Swnj case TCPOPT_OOBACK: { 7665440Swnj int seq; 7675440Swnj 7685440Swnj if (optlen != 4) 7695440Swnj continue; 7705440Swnj if (tp->t_oobseq != cp[2]) { 7715440Swnj printf("wrong ack\n"); 7725440Swnj break; 7735440Swnj } 7745440Swnj printf("take oob ack %x and cancel rexmt\n", cp[2]); 7755440Swnj tp->t_oobflags &= ~TCPOOB_NEEDACK; 7765440Swnj tp->t_timer[TCPT_OOBREXMT] = 0; 7775419Swnj break; 7785440Swnj } 7795440Swnj #endif TCPTRUEOOB 7805440Swnj } 7815419Swnj } 7825440Swnj m_free(om); 7835419Swnj } 7845419Swnj 7855419Swnj /* 7865547Swnj * Pull out of band byte out of a segment so 7875547Swnj * it doesn't appear in the user's data queue. 7885547Swnj * It is still reflected in the segment length for 7895547Swnj * sequencing purposes. 7905547Swnj */ 7915547Swnj tcp_pulloutofband(so, ti) 7925547Swnj struct socket *so; 7935547Swnj struct tcpiphdr *ti; 7945547Swnj { 7955547Swnj register struct mbuf *m; 7965547Swnj int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1; 7975547Swnj 7985547Swnj m = dtom(ti); 7995547Swnj while (cnt >= 0) { 8005547Swnj if (m->m_len > cnt) { 8015547Swnj char *cp = mtod(m, caddr_t) + cnt; 8025547Swnj struct tcpcb *tp = sototcpcb(so); 8035547Swnj 8045547Swnj tp->t_iobc = *cp; 8055547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 8065547Swnj bcopy(cp+1, cp, m->m_len - cnt - 1); 8075547Swnj m->m_len--; 8085547Swnj return; 8095547Swnj } 8105547Swnj cnt -= m->m_len; 8115547Swnj m = m->m_next; 8125547Swnj if (m == 0) 8135547Swnj break; 8145547Swnj } 8155547Swnj panic("tcp_pulloutofband"); 8165547Swnj } 8175547Swnj 8185547Swnj /* 8195065Swnj * Insert segment ti into reassembly queue of tcp with 8205065Swnj * control block tp. Return TH_FIN if reassembly now includes 8215065Swnj * a segment with FIN. 8225065Swnj */ 8235109Swnj tcp_reass(tp, ti) 8245065Swnj register struct tcpcb *tp; 8255065Swnj register struct tcpiphdr *ti; 8265065Swnj { 8275065Swnj register struct tcpiphdr *q; 8285085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8295263Swnj struct mbuf *m; 8305263Swnj int flags; 8315085Swnj COUNT(TCP_REASS); 8325065Swnj 8335065Swnj /* 8345162Swnj * Call with ti==0 after become established to 8355162Swnj * force pre-ESTABLISHED data up to user socket. 8365065Swnj */ 8375162Swnj if (ti == 0) 8385065Swnj goto present; 8394601Swnj 8405065Swnj /* 8415065Swnj * Find a segment which begins after this one does. 8425065Swnj */ 8435065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8445065Swnj q = (struct tcpiphdr *)q->ti_next) 8455065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8465065Swnj break; 8474601Swnj 8485065Swnj /* 8495065Swnj * If there is a preceding segment, it may provide some of 8505065Swnj * our data already. If so, drop the data from the incoming 8515065Swnj * segment. If it provides all of our data, drop us. 8525065Swnj */ 8535065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8545065Swnj register int i; 8555690Swnj q = (struct tcpiphdr *)q->ti_prev; 8565065Swnj /* conversion to int (in i) handles seq wraparound */ 8575065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8585065Swnj if (i > 0) { 8594924Swnj if (i >= ti->ti_len) 8605065Swnj goto drop; 8615065Swnj m_adj(dtom(tp), i); 8625065Swnj ti->ti_len -= i; 8634924Swnj ti->ti_seq += i; 8644601Swnj } 8655065Swnj q = (struct tcpiphdr *)(q->ti_next); 8665065Swnj } 8674601Swnj 8685065Swnj /* 8695065Swnj * While we overlap succeeding segments trim them or, 8705065Swnj * if they are completely covered, dequeue them. 8715065Swnj */ 8725690Swnj while (q != (struct tcpiphdr *)tp) { 8735065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8745690Swnj if (i <= 0) 8755690Swnj break; 8765065Swnj if (i < q->ti_len) { 8775690Swnj q->ti_seq += i; 8785065Swnj q->ti_len -= i; 8795065Swnj m_adj(dtom(q), i); 8805065Swnj break; 8814601Swnj } 8825065Swnj q = (struct tcpiphdr *)q->ti_next; 8835623Swnj m = dtom(q->ti_prev); 8845065Swnj remque(q->ti_prev); 8855623Swnj m_freem(m); 8865065Swnj } 8874601Swnj 8885065Swnj /* 8895065Swnj * Stick new segment in its place. 8905065Swnj */ 8915065Swnj insque(ti, q->ti_prev); 8924601Swnj 8935065Swnj present: 8945065Swnj /* 8955244Sroot * Present data to user, advancing rcv_nxt through 8965244Sroot * completed sequence space. 8975065Swnj */ 8985263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 8995244Sroot return (0); 9004924Swnj ti = tp->seg_next; 9015263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 9025263Swnj return (0); 9035263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9045263Swnj return (0); 9055263Swnj do { 9065244Sroot tp->rcv_nxt += ti->ti_len; 9075244Sroot flags = ti->ti_flags & TH_FIN; 9084924Swnj remque(ti); 9095263Swnj m = dtom(ti); 9104924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9115263Swnj if (so->so_state & SS_CANTRCVMORE) 9125263Swnj (void) m_freem(m); 9135263Swnj else 9145263Swnj sbappend(&so->so_rcv, m); 9155263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9165263Swnj sorwakeup(so); 9175065Swnj return (flags); 9185065Swnj drop: 9195065Swnj m_freem(dtom(ti)); 9205263Swnj return (0); 9214601Swnj } 922