1*12441Ssam /* tcp_input.c 1.94 83/05/14 */ 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" 910894Ssam #include "../h/errno.h" 1010894Ssam 1110894Ssam #include "../net/if.h" 1210894Ssam #include "../net/route.h" 1310894Ssam 148401Swnj #include "../netinet/in.h" 158401Swnj #include "../netinet/in_pcb.h" 168401Swnj #include "../netinet/in_systm.h" 178401Swnj #include "../netinet/ip.h" 188401Swnj #include "../netinet/ip_var.h" 198401Swnj #include "../netinet/tcp.h" 208401Swnj #include "../netinet/tcp_fsm.h" 218401Swnj #include "../netinet/tcp_seq.h" 228401Swnj #include "../netinet/tcp_timer.h" 238401Swnj #include "../netinet/tcp_var.h" 248401Swnj #include "../netinet/tcpip.h" 258401Swnj #include "../netinet/tcp_debug.h" 264601Swnj 275300Sroot int tcpprintfs = 0; 284679Swnj int tcpcksum = 1; 295267Sroot struct tcpiphdr tcp_saveti; 305440Swnj extern tcpnodelack; 314601Swnj 325267Sroot struct tcpcb *tcp_newtcpcb(); 335065Swnj /* 345065Swnj * TCP input routine, follows pages 65-76 of the 355065Swnj * protocol specification dated September, 1981 very closely. 365065Swnj */ 374924Swnj tcp_input(m0) 384924Swnj struct mbuf *m0; 394601Swnj { 404924Swnj register struct tcpiphdr *ti; 414924Swnj struct inpcb *inp; 424924Swnj register struct mbuf *m; 435440Swnj struct mbuf *om = 0; 444924Swnj int len, tlen, off; 455391Swnj register struct tcpcb *tp = 0; 464924Swnj register int tiflags; 474803Swnj struct socket *so; 485109Swnj int todrop, acked; 495267Sroot short ostate; 506028Sroot struct in_addr laddr; 5110769Ssam int dropsocket = 0; 524924Swnj 534924Swnj /* 545244Sroot * Get IP and TCP header together in first mbuf. 555244Sroot * Note: IP leaves IP header in first mbuf. 564924Swnj */ 574924Swnj m = m0; 585020Sroot ti = mtod(m, struct tcpiphdr *); 595244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 605208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 615307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 625307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 635085Swnj tcpstat.tcps_hdrops++; 645307Sroot return; 655085Swnj } 665085Swnj ti = mtod(m, struct tcpiphdr *); 675085Swnj } 684601Swnj 694601Swnj /* 705244Sroot * Checksum extended TCP header and data. 714601Swnj */ 724924Swnj tlen = ((struct ip *)ti)->ip_len; 734924Swnj len = sizeof (struct ip) + tlen; 744679Swnj if (tcpcksum) { 754924Swnj ti->ti_next = ti->ti_prev = 0; 764924Swnj ti->ti_x1 = 0; 775223Swnj ti->ti_len = (u_short)tlen; 786161Ssam ti->ti_len = htons((u_short)ti->ti_len); 795231Swnj if (ti->ti_sum = in_cksum(m, len)) { 8011830Ssam if (tcpprintfs) 8111830Ssam printf("tcp sum: src %x\n", ti->ti_src); 824924Swnj tcpstat.tcps_badsum++; 835085Swnj goto drop; 844601Swnj } 854601Swnj } 864601Swnj 874601Swnj /* 885244Sroot * Check that TCP offset makes sense, 895440Swnj * pull out TCP options and adjust length. 904601Swnj */ 914924Swnj off = ti->ti_off << 2; 925231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 9311830Ssam if (tcpprintfs) 9411830Ssam printf("tcp off: src %x off %d\n", ti->ti_src, off); 954924Swnj tcpstat.tcps_badoff++; 965085Swnj goto drop; 974924Swnj } 986211Swnj tlen -= off; 996211Swnj ti->ti_len = tlen; 1005440Swnj if (off > sizeof (struct tcphdr)) { 1015440Swnj if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 1025440Swnj tcpstat.tcps_hdrops++; 10311730Ssam return; 1045440Swnj } 1055440Swnj ti = mtod(m, struct tcpiphdr *); 1069642Ssam om = m_get(M_DONTWAIT, MT_DATA); 1075440Swnj if (om == 0) 1085440Swnj goto drop; 1095440Swnj om->m_len = off - sizeof (struct tcphdr); 1105440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 1116161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 1125440Swnj m->m_len -= om->m_len; 1136161Ssam bcopy(op+om->m_len, op, 1146161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 1155440Swnj } 1165440Swnj } 1175065Swnj tiflags = ti->ti_flags; 1184924Swnj 1196093Sroot /* 1206211Swnj * Drop TCP and IP headers. 1216093Sroot */ 1226093Sroot off += sizeof (struct ip); 1236093Sroot m->m_off += off; 1246093Sroot m->m_len -= off; 1256093Sroot 1264924Swnj /* 1275244Sroot * Convert TCP protocol specific fields to host format. 1285085Swnj */ 1295085Swnj ti->ti_seq = ntohl(ti->ti_seq); 1305085Swnj ti->ti_ack = ntohl(ti->ti_ack); 1315085Swnj ti->ti_win = ntohs(ti->ti_win); 1325085Swnj ti->ti_urp = ntohs(ti->ti_urp); 1335085Swnj 1345085Swnj /* 1358271Sroot * Locate pcb for segment. 1364924Swnj */ 1375065Swnj inp = in_pcblookup 1386028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 1396028Sroot INPLOOKUP_WILDCARD); 1405065Swnj 1415065Swnj /* 1425065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1435244Sroot * all data in the incoming segment is discarded. 1445065Swnj */ 1455300Sroot if (inp == 0) 1465085Swnj goto dropwithreset; 1475065Swnj tp = intotcpcb(inp); 1485300Sroot if (tp == 0) 1495085Swnj goto dropwithreset; 1505109Swnj so = inp->inp_socket; 1515267Sroot if (so->so_options & SO_DEBUG) { 1525267Sroot ostate = tp->t_state; 1535267Sroot tcp_saveti = *ti; 1545267Sroot } 1557510Sroot if (so->so_options & SO_ACCEPTCONN) { 1567510Sroot so = sonewconn(so); 1577510Sroot if (so == 0) 1587510Sroot goto drop; 15910769Ssam /* 16010769Ssam * This is ugly, but .... 16110769Ssam * 16210769Ssam * Mark socket as temporary until we're 16310769Ssam * committed to keeping it. The code at 16410769Ssam * ``drop'' and ``dropwithreset'' check the 16510769Ssam * flag dropsocket to see if the temporary 16610769Ssam * socket created here should be discarded. 16710769Ssam * We mark the socket as discardable until 16810769Ssam * we're committed to it below in TCPS_LISTEN. 16910769Ssam */ 17010769Ssam dropsocket++; 1717510Sroot inp = (struct inpcb *)so->so_pcb; 1727510Sroot inp->inp_laddr = ti->ti_dst; 1737510Sroot inp->inp_lport = ti->ti_dport; 1747510Sroot tp = intotcpcb(inp); 1757510Sroot tp->t_state = TCPS_LISTEN; 1767510Sroot } 1774601Swnj 1784601Swnj /* 1795162Swnj * Segment received on connection. 1805162Swnj * Reset idle time and keep-alive timer. 1815162Swnj */ 1825162Swnj tp->t_idle = 0; 1835162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1845162Swnj 1855162Swnj /* 1865440Swnj * Process options. 1875440Swnj */ 1885440Swnj if (om) { 1895440Swnj tcp_dooptions(tp, om); 1905440Swnj om = 0; 1915440Swnj } 1925440Swnj 1935440Swnj /* 1945085Swnj * Calculate amount of space in receive window, 1955085Swnj * and then do TCP input processing. 1964601Swnj */ 1975085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1985231Swnj if (tp->rcv_wnd < 0) 1995231Swnj tp->rcv_wnd = 0; 2004601Swnj 2014601Swnj switch (tp->t_state) { 2024601Swnj 2035065Swnj /* 2045065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 2055065Swnj * If the segment contains an ACK then it is bad and send a RST. 2065065Swnj * If it does not contain a SYN then it is not interesting; drop it. 2075085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 2085065Swnj * tp->iss, and send a segment: 2095085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 2105065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 2115065Swnj * Fill in remote peer address fields if not previously specified. 2125065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 2135244Sroot * segment in this state. 2145065Swnj */ 2158271Sroot case TCPS_LISTEN: { 21610145Ssam struct mbuf *am; 2178271Sroot register struct sockaddr_in *sin; 2188271Sroot 2195065Swnj if (tiflags & TH_RST) 2205065Swnj goto drop; 2215300Sroot if (tiflags & TH_ACK) 2225085Swnj goto dropwithreset; 2235300Sroot if ((tiflags & TH_SYN) == 0) 2245065Swnj goto drop; 22510145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 22610145Ssam if (am == NULL) 22710145Ssam goto drop; 22810145Ssam am->m_len = sizeof (struct sockaddr_in); 2298599Sroot sin = mtod(am, struct sockaddr_in *); 2308271Sroot sin->sin_family = AF_INET; 2318271Sroot sin->sin_addr = ti->ti_src; 2328271Sroot sin->sin_port = ti->ti_sport; 2336028Sroot laddr = inp->inp_laddr; 23410145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 2356028Sroot inp->inp_laddr = ti->ti_dst; 2368599Sroot if (in_pcbconnect(inp, am)) { 2376028Sroot inp->inp_laddr = laddr; 2388716Sroot (void) m_free(am); 2395244Sroot goto drop; 2406028Sroot } 2418716Sroot (void) m_free(am); 2425244Sroot tp->t_template = tcp_template(tp); 2435244Sroot if (tp->t_template == 0) { 2445244Sroot in_pcbdisconnect(inp); 2456028Sroot inp->inp_laddr = laddr; 2466320Swnj tp = 0; 2475244Sroot goto drop; 2485244Sroot } 2495085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 2505065Swnj tp->irs = ti->ti_seq; 2515085Swnj tcp_sendseqinit(tp); 2525085Swnj tcp_rcvseqinit(tp); 2535065Swnj tp->t_state = TCPS_SYN_RECEIVED; 2545244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 25510769Ssam dropsocket = 0; /* committed to socket */ 2565085Swnj goto trimthenstep6; 2578271Sroot } 2584601Swnj 2595065Swnj /* 2605065Swnj * If the state is SYN_SENT: 2615065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 2625065Swnj * if seg contains a RST, then drop the connection. 2635065Swnj * if seg does not contain SYN, then drop it. 2645065Swnj * Otherwise this is an acceptable SYN segment 2655065Swnj * initialize tp->rcv_nxt and tp->irs 2665065Swnj * if seg contains ack then advance tp->snd_una 2675065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2685065Swnj * arrange for segment to be acked (eventually) 2695065Swnj * continue processing rest of data/controls, beginning with URG 2705065Swnj */ 2715065Swnj case TCPS_SYN_SENT: 2725065Swnj if ((tiflags & TH_ACK) && 2735300Sroot /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */ 2745300Sroot (SEQ_LT(ti->ti_ack, tp->iss) || 2755231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 2765085Swnj goto dropwithreset; 2775065Swnj if (tiflags & TH_RST) { 27810394Ssam if (tiflags & TH_ACK) 27910394Ssam tp = tcp_drop(tp, ECONNREFUSED); 2805065Swnj goto drop; 2814601Swnj } 2825065Swnj if ((tiflags & TH_SYN) == 0) 2835065Swnj goto drop; 2845231Swnj tp->snd_una = ti->ti_ack; 2855357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2865357Sroot tp->snd_nxt = tp->snd_una; 2875244Sroot tp->t_timer[TCPT_REXMT] = 0; 2885065Swnj tp->irs = ti->ti_seq; 2895085Swnj tcp_rcvseqinit(tp); 2905085Swnj tp->t_flags |= TF_ACKNOW; 2915162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 2925244Sroot soisconnected(so); 2935065Swnj tp->t_state = TCPS_ESTABLISHED; 2945162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 2955162Swnj } else 2965085Swnj tp->t_state = TCPS_SYN_RECEIVED; 2975085Swnj goto trimthenstep6; 2985085Swnj 2995085Swnj trimthenstep6: 3005085Swnj /* 3015231Swnj * Advance ti->ti_seq to correspond to first data byte. 3025085Swnj * If data, trim to stay within window, 3035085Swnj * dropping FIN if necessary. 3045085Swnj */ 3055231Swnj ti->ti_seq++; 3065085Swnj if (ti->ti_len > tp->rcv_wnd) { 3075085Swnj todrop = ti->ti_len - tp->rcv_wnd; 3085085Swnj m_adj(m, -todrop); 3095085Swnj ti->ti_len = tp->rcv_wnd; 3105085Swnj ti->ti_flags &= ~TH_FIN; 3115065Swnj } 3125263Swnj tp->snd_wl1 = ti->ti_seq - 1; 3135085Swnj goto step6; 3145065Swnj } 3154601Swnj 3165065Swnj /* 3175065Swnj * States other than LISTEN or SYN_SENT. 3185065Swnj * First check that at least some bytes of segment are within 3195065Swnj * receive window. 3205065Swnj */ 3215065Swnj if (tp->rcv_wnd == 0) { 3225065Swnj /* 3235065Swnj * If window is closed can only take segments at 3245231Swnj * window edge, and have to drop data and PUSH from 3255065Swnj * incoming segments. 3265065Swnj */ 3275300Sroot if (tp->rcv_nxt != ti->ti_seq) 3285065Swnj goto dropafterack; 3295085Swnj if (ti->ti_len > 0) { 3305690Swnj m_adj(m, ti->ti_len); 3315085Swnj ti->ti_len = 0; 3325085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3335065Swnj } 3345065Swnj } else { 3355065Swnj /* 3365231Swnj * If segment begins before rcv_nxt, drop leading 3375065Swnj * data (and SYN); if nothing left, just ack. 3385065Swnj */ 3395690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 3405690Swnj if (todrop > 0) { 3415085Swnj if (tiflags & TH_SYN) { 3425300Sroot tiflags &= ~TH_SYN; 3435690Swnj ti->ti_flags &= ~TH_SYN; 3445085Swnj ti->ti_seq++; 3455085Swnj if (ti->ti_urp > 1) 3465085Swnj ti->ti_urp--; 3475085Swnj else 3485085Swnj tiflags &= ~TH_URG; 3495085Swnj todrop--; 3505085Swnj } 3516211Swnj if (todrop > ti->ti_len || 3526211Swnj todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 3535065Swnj goto dropafterack; 3545065Swnj m_adj(m, todrop); 3555065Swnj ti->ti_seq += todrop; 3565065Swnj ti->ti_len -= todrop; 3575085Swnj if (ti->ti_urp > todrop) 3585085Swnj ti->ti_urp -= todrop; 3595085Swnj else { 3605085Swnj tiflags &= ~TH_URG; 3615690Swnj ti->ti_flags &= ~TH_URG; 3625690Swnj ti->ti_urp = 0; 3635085Swnj } 3645065Swnj } 3655065Swnj /* 3665065Swnj * If segment ends after window, drop trailing data 3675085Swnj * (and PUSH and FIN); if nothing left, just ACK. 3685065Swnj */ 3695690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 3705690Swnj if (todrop > 0) { 3716211Swnj if (todrop >= ti->ti_len) 3725065Swnj goto dropafterack; 3735065Swnj m_adj(m, -todrop); 3745065Swnj ti->ti_len -= todrop; 3755085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 3765065Swnj } 3775065Swnj } 3784601Swnj 3795065Swnj /* 38010013Ssam * If data is received on a connection after the 3815951Swnj * user processes are gone, then RST the other end. 3825951Swnj */ 38310394Ssam if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT && 38410203Ssam ti->ti_len) { 38510394Ssam tp = tcp_close(tp); 3865951Swnj goto dropwithreset; 3875951Swnj } 3885951Swnj 3895951Swnj /* 3905065Swnj * If the RST bit is set examine the state: 3915065Swnj * SYN_RECEIVED STATE: 3925065Swnj * If passive open, return to LISTEN state. 3935065Swnj * If active open, inform user that connection was refused. 3945065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 3955065Swnj * Inform user that connection was reset, and close tcb. 3965065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 3975065Swnj * Close the tcb. 3985065Swnj */ 3995065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 4005267Sroot 4015065Swnj case TCPS_SYN_RECEIVED: 40210394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4035065Swnj goto drop; 4044601Swnj 4055065Swnj case TCPS_ESTABLISHED: 4065065Swnj case TCPS_FIN_WAIT_1: 4075065Swnj case TCPS_FIN_WAIT_2: 4085065Swnj case TCPS_CLOSE_WAIT: 40910394Ssam tp = tcp_drop(tp, ECONNRESET); 4105065Swnj goto drop; 4115065Swnj 4125065Swnj case TCPS_CLOSING: 4135065Swnj case TCPS_LAST_ACK: 4145065Swnj case TCPS_TIME_WAIT: 41510394Ssam tp = tcp_close(tp); 4165065Swnj goto drop; 4174601Swnj } 4184601Swnj 4194601Swnj /* 4205065Swnj * If a SYN is in the window, then this is an 4215065Swnj * error and we send an RST and drop the connection. 4224601Swnj */ 4235065Swnj if (tiflags & TH_SYN) { 42410394Ssam tp = tcp_drop(tp, ECONNRESET); 4255085Swnj goto dropwithreset; 4264601Swnj } 4274601Swnj 4284601Swnj /* 4295065Swnj * If the ACK bit is off we drop the segment and return. 4304601Swnj */ 4315085Swnj if ((tiflags & TH_ACK) == 0) 4325065Swnj goto drop; 4335065Swnj 4345065Swnj /* 4355065Swnj * Ack processing. 4365065Swnj */ 4374601Swnj switch (tp->t_state) { 4384601Swnj 4395065Swnj /* 4405065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 4415065Swnj * ESTABLISHED state and continue processing, othewise 4425065Swnj * send an RST. 4435065Swnj */ 4445065Swnj case TCPS_SYN_RECEIVED: 4455085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 4465231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 4475085Swnj goto dropwithreset; 4485244Sroot tp->snd_una++; /* SYN acked */ 4495357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4505357Sroot tp->snd_nxt = tp->snd_una; 4515244Sroot tp->t_timer[TCPT_REXMT] = 0; 4525085Swnj soisconnected(so); 4535085Swnj tp->t_state = TCPS_ESTABLISHED; 4545162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4555244Sroot tp->snd_wl1 = ti->ti_seq - 1; 4565085Swnj /* fall into ... */ 4574601Swnj 4585065Swnj /* 4595065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 4605065Swnj * ACKs. If the ack is in the range 4615231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 4625065Swnj * then advance tp->snd_una to ti->ti_ack and drop 4635065Swnj * data from the retransmission queue. If this ACK reflects 4645065Swnj * more up to date window information we update our window information. 4655065Swnj */ 4665065Swnj case TCPS_ESTABLISHED: 4675065Swnj case TCPS_FIN_WAIT_1: 4685065Swnj case TCPS_FIN_WAIT_2: 4695065Swnj case TCPS_CLOSE_WAIT: 4705065Swnj case TCPS_CLOSING: 4715244Sroot case TCPS_LAST_ACK: 4725244Sroot case TCPS_TIME_WAIT: 4735085Swnj #define ourfinisacked (acked > 0) 4745085Swnj 4755244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 4765065Swnj break; 4775300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 4785065Swnj goto dropafterack; 4795085Swnj acked = ti->ti_ack - tp->snd_una; 4805951Swnj 4815951Swnj /* 4825951Swnj * If transmit timer is running and timed sequence 4835951Swnj * number was acked, update smoothed round trip time. 4845951Swnj */ 4855951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 4865951Swnj if (tp->t_srtt == 0) 4875951Swnj tp->t_srtt = tp->t_rtt; 4885951Swnj else 4895951Swnj tp->t_srtt = 4905951Swnj tcp_alpha * tp->t_srtt + 4915951Swnj (1 - tcp_alpha) * tp->t_rtt; 4925951Swnj tp->t_rtt = 0; 4935951Swnj } 4945951Swnj 4955307Sroot if (ti->ti_ack == tp->snd_max) 4965244Sroot tp->t_timer[TCPT_REXMT] = 0; 4975307Sroot else { 4985244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 4995244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 5005951Swnj tp->t_rtt = 1; 5015300Sroot tp->t_rxtshift = 0; 5025085Swnj } 5035307Sroot if (acked > so->so_snd.sb_cc) { 5045307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 5055307Sroot tp->snd_wnd -= so->so_snd.sb_cc; 5065307Sroot } else { 5076161Ssam sbdrop(&so->so_snd, acked); 5085307Sroot tp->snd_wnd -= acked; 5095307Sroot acked = 0; 5105307Sroot } 5116434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 5125300Sroot sowwakeup(so); 5135231Swnj tp->snd_una = ti->ti_ack; 5145357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 5155357Sroot tp->snd_nxt = tp->snd_una; 5165162Swnj 5174601Swnj switch (tp->t_state) { 5184601Swnj 5195065Swnj /* 5205065Swnj * In FIN_WAIT_1 STATE in addition to the processing 5215065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 5225085Swnj * then enter FIN_WAIT_2. 5235065Swnj */ 5245065Swnj case TCPS_FIN_WAIT_1: 5255896Swnj if (ourfinisacked) { 5265896Swnj /* 5275896Swnj * If we can't receive any more 5285896Swnj * data, then closing user can proceed. 5295896Swnj */ 5305896Swnj if (so->so_state & SS_CANTRCVMORE) 5315896Swnj soisdisconnected(so); 5325085Swnj tp->t_state = TCPS_FIN_WAIT_2; 5335896Swnj } 5344601Swnj break; 5354601Swnj 5365065Swnj /* 5375065Swnj * In CLOSING STATE in addition to the processing for 5385065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 5395065Swnj * then enter the TIME-WAIT state, otherwise ignore 5405065Swnj * the segment. 5415065Swnj */ 5425065Swnj case TCPS_CLOSING: 5435244Sroot if (ourfinisacked) { 5445065Swnj tp->t_state = TCPS_TIME_WAIT; 5455244Sroot tcp_canceltimers(tp); 5465244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5475244Sroot soisdisconnected(so); 5485244Sroot } 5495244Sroot break; 5504601Swnj 5515065Swnj /* 5525085Swnj * The only thing that can arrive in LAST_ACK state 5535085Swnj * is an acknowledgment of our FIN. If our FIN is now 5545085Swnj * acknowledged, delete the TCB, enter the closed state 5555085Swnj * and return. 5565065Swnj */ 5575065Swnj case TCPS_LAST_ACK: 55810394Ssam if (ourfinisacked) 55910394Ssam tp = tcp_close(tp); 5605065Swnj goto drop; 5614601Swnj 5625065Swnj /* 5635065Swnj * In TIME_WAIT state the only thing that should arrive 5645065Swnj * is a retransmission of the remote FIN. Acknowledge 5655065Swnj * it and restart the finack timer. 5665065Swnj */ 5675065Swnj case TCPS_TIME_WAIT: 5685162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 5695065Swnj goto dropafterack; 5704601Swnj } 5715085Swnj #undef ourfinisacked 5725085Swnj } 5734601Swnj 5745065Swnj step6: 5755065Swnj /* 5765244Sroot * Update window information. 5775244Sroot */ 5785300Sroot if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 5795391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 5805300Sroot tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) { 5815244Sroot tp->snd_wnd = ti->ti_win; 5825244Sroot tp->snd_wl1 = ti->ti_seq; 5835244Sroot tp->snd_wl2 = ti->ti_ack; 5848599Sroot if (tp->snd_wnd != 0) 5855244Sroot tp->t_timer[TCPT_PERSIST] = 0; 5865244Sroot } 5875244Sroot 5885244Sroot /* 5895547Swnj * Process segments with URG. 5905065Swnj */ 5917267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 5927267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 5935547Swnj /* 594*12441Ssam * This is a kludge, but the BBN C70 TCP 595*12441Ssam * randomly sends packets with the urgent flag 596*12441Ssam * set and random ti_urp values. This crashes 597*12441Ssam * the system because so_oobmark ends up being 598*12441Ssam * interpreted as a negative number in soreceive. 599*12441Ssam */ 600*12441Ssam if (ti->ti_urp > tp->t_maxseg) { /* XXX */ 601*12441Ssam ti->ti_urp = 0; /* XXX */ 602*12441Ssam tiflags &= ~TH_URG; /* XXX */ 603*12441Ssam ti->ti_flags &= ~TH_URG; /* XXX */ 604*12441Ssam goto bbnurp; /* XXX */ 605*12441Ssam } 606*12441Ssam /* 6075547Swnj * If this segment advances the known urgent pointer, 6085547Swnj * then mark the data stream. This should not happen 6095547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 6105547Swnj * a FIN has been received from the remote side. 6115547Swnj * In these states we ignore the URG. 6125547Swnj */ 6135547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 6145547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 6155547Swnj so->so_oobmark = so->so_rcv.sb_cc + 6165547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 6175547Swnj if (so->so_oobmark == 0) 6185547Swnj so->so_state |= SS_RCVATMARK; 6198313Sroot sohasoutofband(so); 6205547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 6215440Swnj } 6225547Swnj /* 6235547Swnj * Remove out of band data so doesn't get presented to user. 6245547Swnj * This can happen independent of advancing the URG pointer, 6255547Swnj * but if two URG's are pending at once, some out-of-band 6265547Swnj * data may creep in... ick. 6275547Swnj */ 6287510Sroot if (ti->ti_urp <= ti->ti_len) 6295547Swnj tcp_pulloutofband(so, ti); 6305419Swnj } 631*12441Ssam bbnurp: /* XXX */ 6324601Swnj 6334601Swnj /* 6345065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6355065Swnj * and arranging for acknowledgment of receipt if necessary. 6365065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6375065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6385065Swnj * case PRU_RCVD). If a FIN has already been received on this 6395065Swnj * connection then we just ignore the text. 6404601Swnj */ 6415263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6425263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6435065Swnj tiflags = tcp_reass(tp, ti); 6445440Swnj if (tcpnodelack == 0) 6455440Swnj tp->t_flags |= TF_DELACK; 6465440Swnj else 6475440Swnj tp->t_flags |= TF_ACKNOW; 6485244Sroot } else { 6494924Swnj m_freem(m); 6505263Swnj tiflags &= ~TH_FIN; 6515244Sroot } 6524601Swnj 6534601Swnj /* 6545263Swnj * If FIN is received ACK the FIN and let the user know 6555263Swnj * that the connection is closing. 6564601Swnj */ 6575263Swnj if (tiflags & TH_FIN) { 6585244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6595244Sroot socantrcvmore(so); 6605244Sroot tp->t_flags |= TF_ACKNOW; 6615244Sroot tp->rcv_nxt++; 6625244Sroot } 6635065Swnj switch (tp->t_state) { 6644601Swnj 6655065Swnj /* 6665065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6675065Swnj * enter the CLOSE_WAIT state. 6684884Swnj */ 6695065Swnj case TCPS_SYN_RECEIVED: 6705065Swnj case TCPS_ESTABLISHED: 6715065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6725065Swnj break; 6734884Swnj 6745065Swnj /* 6755085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6765085Swnj * enter the CLOSING state. 6774884Swnj */ 6785065Swnj case TCPS_FIN_WAIT_1: 6795085Swnj tp->t_state = TCPS_CLOSING; 6805065Swnj break; 6814601Swnj 6825065Swnj /* 6835065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6845065Swnj * starting the time-wait timer, turning off the other 6855065Swnj * standard timers. 6865065Swnj */ 6875065Swnj case TCPS_FIN_WAIT_2: 6885244Sroot tp->t_state = TCPS_TIME_WAIT; 6895074Swnj tcp_canceltimers(tp); 6905162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6915244Sroot soisdisconnected(so); 6925065Swnj break; 6935065Swnj 6944884Swnj /* 6955065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6964884Swnj */ 6975065Swnj case TCPS_TIME_WAIT: 6985162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6995065Swnj break; 7005085Swnj } 7014601Swnj } 7025267Sroot if (so->so_options & SO_DEBUG) 7035267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 7045085Swnj 7055085Swnj /* 7065085Swnj * Return any desired output. 7075085Swnj */ 7086161Ssam (void) tcp_output(tp); 7095065Swnj return; 7105085Swnj 7115065Swnj dropafterack: 7125085Swnj /* 7136211Swnj * Generate an ACK dropping incoming segment if it occupies 7146211Swnj * sequence space, where the ACK reflects our state. 7155085Swnj */ 7166211Swnj if ((tiflags&TH_RST) || 7176211Swnj tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 7185085Swnj goto drop; 7196303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 7206303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 7215391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 7225231Swnj return; 7235085Swnj 7245085Swnj dropwithreset: 72511731Ssam if (om) { 7266161Ssam (void) m_free(om); 72711731Ssam om = 0; 72811731Ssam } 7295085Swnj /* 7305244Sroot * Generate a RST, dropping incoming segment. 7315085Swnj * Make ACK acceptable to originator of segment. 7325085Swnj */ 7335085Swnj if (tiflags & TH_RST) 7345085Swnj goto drop; 7355085Swnj if (tiflags & TH_ACK) 7365391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7375085Swnj else { 7385085Swnj if (tiflags & TH_SYN) 7395085Swnj ti->ti_len++; 7406211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 7416211Swnj TH_RST|TH_ACK); 7425085Swnj } 74310769Ssam /* destroy temporarily created socket */ 74410769Ssam if (dropsocket) 74510769Ssam (void) soabort(so); 7465231Swnj return; 7475085Swnj 7485065Swnj drop: 74911730Ssam if (om) 75011730Ssam (void) m_free(om); 7515085Swnj /* 7525085Swnj * Drop space held by incoming segment and return. 7535085Swnj */ 7546303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 7556303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 7565065Swnj m_freem(m); 75710769Ssam /* destroy temporarily created socket */ 75810769Ssam if (dropsocket) 75910769Ssam (void) soabort(so); 7605267Sroot return; 7615065Swnj } 7625065Swnj 7635440Swnj tcp_dooptions(tp, om) 7645440Swnj struct tcpcb *tp; 7655440Swnj struct mbuf *om; 7665419Swnj { 7675440Swnj register u_char *cp; 7685440Swnj int opt, optlen, cnt; 7695419Swnj 7705440Swnj cp = mtod(om, u_char *); 7715440Swnj cnt = om->m_len; 7725440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7735440Swnj opt = cp[0]; 7745440Swnj if (opt == TCPOPT_EOL) 7755440Swnj break; 7765440Swnj if (opt == TCPOPT_NOP) 7775440Swnj optlen = 1; 77812169Ssam else { 7795440Swnj optlen = cp[1]; 78012169Ssam if (optlen <= 0) 78112169Ssam break; 78212169Ssam } 7835440Swnj switch (opt) { 7845440Swnj 7855440Swnj default: 7865440Swnj break; 7875440Swnj 7885440Swnj case TCPOPT_MAXSEG: 7895440Swnj if (optlen != 4) 7905440Swnj continue; 7915440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7926161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7935440Swnj break; 7945419Swnj } 7955419Swnj } 7966161Ssam (void) m_free(om); 7975419Swnj } 7985419Swnj 7995419Swnj /* 8005547Swnj * Pull out of band byte out of a segment so 8015547Swnj * it doesn't appear in the user's data queue. 8025547Swnj * It is still reflected in the segment length for 8035547Swnj * sequencing purposes. 8045547Swnj */ 8055547Swnj tcp_pulloutofband(so, ti) 8065547Swnj struct socket *so; 8075547Swnj struct tcpiphdr *ti; 8085547Swnj { 8095547Swnj register struct mbuf *m; 8106116Swnj int cnt = ti->ti_urp - 1; 8115547Swnj 8125547Swnj m = dtom(ti); 8135547Swnj while (cnt >= 0) { 8145547Swnj if (m->m_len > cnt) { 8155547Swnj char *cp = mtod(m, caddr_t) + cnt; 8165547Swnj struct tcpcb *tp = sototcpcb(so); 8175547Swnj 8185547Swnj tp->t_iobc = *cp; 8195547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 8206161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 8215547Swnj m->m_len--; 8225547Swnj return; 8235547Swnj } 8245547Swnj cnt -= m->m_len; 8255547Swnj m = m->m_next; 8265547Swnj if (m == 0) 8275547Swnj break; 8285547Swnj } 8295547Swnj panic("tcp_pulloutofband"); 8305547Swnj } 8315547Swnj 8325547Swnj /* 8335065Swnj * Insert segment ti into reassembly queue of tcp with 8345065Swnj * control block tp. Return TH_FIN if reassembly now includes 8355065Swnj * a segment with FIN. 8365065Swnj */ 8375109Swnj tcp_reass(tp, ti) 8385065Swnj register struct tcpcb *tp; 8395065Swnj register struct tcpiphdr *ti; 8405065Swnj { 8415065Swnj register struct tcpiphdr *q; 8425085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8435263Swnj struct mbuf *m; 8445263Swnj int flags; 8455065Swnj 8465065Swnj /* 8475162Swnj * Call with ti==0 after become established to 8485162Swnj * force pre-ESTABLISHED data up to user socket. 8495065Swnj */ 8505162Swnj if (ti == 0) 8515065Swnj goto present; 8524601Swnj 8535065Swnj /* 8545065Swnj * Find a segment which begins after this one does. 8555065Swnj */ 8565065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8575065Swnj q = (struct tcpiphdr *)q->ti_next) 8585065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8595065Swnj break; 8604601Swnj 8615065Swnj /* 8625065Swnj * If there is a preceding segment, it may provide some of 8635065Swnj * our data already. If so, drop the data from the incoming 8645065Swnj * segment. If it provides all of our data, drop us. 8655065Swnj */ 8665065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8675065Swnj register int i; 8685690Swnj q = (struct tcpiphdr *)q->ti_prev; 8695065Swnj /* conversion to int (in i) handles seq wraparound */ 8705065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8715065Swnj if (i > 0) { 8724924Swnj if (i >= ti->ti_len) 8735065Swnj goto drop; 8747338Swnj m_adj(dtom(ti), i); 8755065Swnj ti->ti_len -= i; 8764924Swnj ti->ti_seq += i; 8774601Swnj } 8785065Swnj q = (struct tcpiphdr *)(q->ti_next); 8795065Swnj } 8804601Swnj 8815065Swnj /* 8825065Swnj * While we overlap succeeding segments trim them or, 8835065Swnj * if they are completely covered, dequeue them. 8845065Swnj */ 8855690Swnj while (q != (struct tcpiphdr *)tp) { 8865065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8875690Swnj if (i <= 0) 8885690Swnj break; 8895065Swnj if (i < q->ti_len) { 8905690Swnj q->ti_seq += i; 8915065Swnj q->ti_len -= i; 8925065Swnj m_adj(dtom(q), i); 8935065Swnj break; 8944601Swnj } 8955065Swnj q = (struct tcpiphdr *)q->ti_next; 8965623Swnj m = dtom(q->ti_prev); 8975065Swnj remque(q->ti_prev); 8985623Swnj m_freem(m); 8995065Swnj } 9004601Swnj 9015065Swnj /* 9025065Swnj * Stick new segment in its place. 9035065Swnj */ 9045065Swnj insque(ti, q->ti_prev); 9054601Swnj 9065065Swnj present: 9075065Swnj /* 9085244Sroot * Present data to user, advancing rcv_nxt through 9095244Sroot * completed sequence space. 9105065Swnj */ 9115263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 9125244Sroot return (0); 9134924Swnj ti = tp->seg_next; 9145263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 9155263Swnj return (0); 9165263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9175263Swnj return (0); 9185263Swnj do { 9195244Sroot tp->rcv_nxt += ti->ti_len; 9205244Sroot flags = ti->ti_flags & TH_FIN; 9214924Swnj remque(ti); 9225263Swnj m = dtom(ti); 9234924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9245263Swnj if (so->so_state & SS_CANTRCVMORE) 9256161Ssam m_freem(m); 92610145Ssam else 9275263Swnj sbappend(&so->so_rcv, m); 9285263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9295263Swnj sorwakeup(so); 9305065Swnj return (flags); 9315065Swnj drop: 9325065Swnj m_freem(dtom(ti)); 9335263Swnj return (0); 9344601Swnj } 935