1*11830Ssam /* tcp_input.c 1.92 83/04/03 */ 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)) { 80*11830Ssam if (tcpprintfs) 81*11830Ssam 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) { 93*11830Ssam if (tcpprintfs) 94*11830Ssam 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 /* 5945547Swnj * If this segment advances the known urgent pointer, 5955547Swnj * then mark the data stream. This should not happen 5965547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 5975547Swnj * a FIN has been received from the remote side. 5985547Swnj * In these states we ignore the URG. 5995547Swnj */ 6005547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 6015547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 6025547Swnj so->so_oobmark = so->so_rcv.sb_cc + 6035547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 6045547Swnj if (so->so_oobmark == 0) 6055547Swnj so->so_state |= SS_RCVATMARK; 6068313Sroot sohasoutofband(so); 6075547Swnj tp->t_oobflags &= ~TCPOOB_HAVEDATA; 6085440Swnj } 6095547Swnj /* 6105547Swnj * Remove out of band data so doesn't get presented to user. 6115547Swnj * This can happen independent of advancing the URG pointer, 6125547Swnj * but if two URG's are pending at once, some out-of-band 6135547Swnj * data may creep in... ick. 6145547Swnj */ 6157510Sroot if (ti->ti_urp <= ti->ti_len) 6165547Swnj tcp_pulloutofband(so, ti); 6175419Swnj } 6184601Swnj 6194601Swnj /* 6205065Swnj * Process the segment text, merging it into the TCP sequencing queue, 6215065Swnj * and arranging for acknowledgment of receipt if necessary. 6225065Swnj * This process logically involves adjusting tp->rcv_wnd as data 6235065Swnj * is presented to the user (this happens in tcp_usrreq.c, 6245065Swnj * case PRU_RCVD). If a FIN has already been received on this 6255065Swnj * connection then we just ignore the text. 6264601Swnj */ 6275263Swnj if ((ti->ti_len || (tiflags&TH_FIN)) && 6285263Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6295065Swnj tiflags = tcp_reass(tp, ti); 6305440Swnj if (tcpnodelack == 0) 6315440Swnj tp->t_flags |= TF_DELACK; 6325440Swnj else 6335440Swnj tp->t_flags |= TF_ACKNOW; 6345244Sroot } else { 6354924Swnj m_freem(m); 6365263Swnj tiflags &= ~TH_FIN; 6375244Sroot } 6384601Swnj 6394601Swnj /* 6405263Swnj * If FIN is received ACK the FIN and let the user know 6415263Swnj * that the connection is closing. 6424601Swnj */ 6435263Swnj if (tiflags & TH_FIN) { 6445244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 6455244Sroot socantrcvmore(so); 6465244Sroot tp->t_flags |= TF_ACKNOW; 6475244Sroot tp->rcv_nxt++; 6485244Sroot } 6495065Swnj switch (tp->t_state) { 6504601Swnj 6515065Swnj /* 6525065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 6535065Swnj * enter the CLOSE_WAIT state. 6544884Swnj */ 6555065Swnj case TCPS_SYN_RECEIVED: 6565065Swnj case TCPS_ESTABLISHED: 6575065Swnj tp->t_state = TCPS_CLOSE_WAIT; 6585065Swnj break; 6594884Swnj 6605065Swnj /* 6615085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 6625085Swnj * enter the CLOSING state. 6634884Swnj */ 6645065Swnj case TCPS_FIN_WAIT_1: 6655085Swnj tp->t_state = TCPS_CLOSING; 6665065Swnj break; 6674601Swnj 6685065Swnj /* 6695065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 6705065Swnj * starting the time-wait timer, turning off the other 6715065Swnj * standard timers. 6725065Swnj */ 6735065Swnj case TCPS_FIN_WAIT_2: 6745244Sroot tp->t_state = TCPS_TIME_WAIT; 6755074Swnj tcp_canceltimers(tp); 6765162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6775244Sroot soisdisconnected(so); 6785065Swnj break; 6795065Swnj 6804884Swnj /* 6815065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 6824884Swnj */ 6835065Swnj case TCPS_TIME_WAIT: 6845162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6855065Swnj break; 6865085Swnj } 6874601Swnj } 6885267Sroot if (so->so_options & SO_DEBUG) 6895267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 6905085Swnj 6915085Swnj /* 6925085Swnj * Return any desired output. 6935085Swnj */ 6946161Ssam (void) tcp_output(tp); 6955065Swnj return; 6965085Swnj 6975065Swnj dropafterack: 6985085Swnj /* 6996211Swnj * Generate an ACK dropping incoming segment if it occupies 7006211Swnj * sequence space, where the ACK reflects our state. 7015085Swnj */ 7026211Swnj if ((tiflags&TH_RST) || 7036211Swnj tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0) 7045085Swnj goto drop; 7056303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 7066303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 7075391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 7085231Swnj return; 7095085Swnj 7105085Swnj dropwithreset: 71111731Ssam if (om) { 7126161Ssam (void) m_free(om); 71311731Ssam om = 0; 71411731Ssam } 7155085Swnj /* 7165244Sroot * Generate a RST, dropping incoming segment. 7175085Swnj * Make ACK acceptable to originator of segment. 7185085Swnj */ 7195085Swnj if (tiflags & TH_RST) 7205085Swnj goto drop; 7215085Swnj if (tiflags & TH_ACK) 7225391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 7235085Swnj else { 7245085Swnj if (tiflags & TH_SYN) 7255085Swnj ti->ti_len++; 7266211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 7276211Swnj TH_RST|TH_ACK); 7285085Swnj } 72910769Ssam /* destroy temporarily created socket */ 73010769Ssam if (dropsocket) 73110769Ssam (void) soabort(so); 7325231Swnj return; 7335085Swnj 7345065Swnj drop: 73511730Ssam if (om) 73611730Ssam (void) m_free(om); 7375085Swnj /* 7385085Swnj * Drop space held by incoming segment and return. 7395085Swnj */ 7406303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 7416303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 7425065Swnj m_freem(m); 74310769Ssam /* destroy temporarily created socket */ 74410769Ssam if (dropsocket) 74510769Ssam (void) soabort(so); 7465267Sroot return; 7475065Swnj } 7485065Swnj 7495440Swnj tcp_dooptions(tp, om) 7505440Swnj struct tcpcb *tp; 7515440Swnj struct mbuf *om; 7525419Swnj { 7535440Swnj register u_char *cp; 7545440Swnj int opt, optlen, cnt; 7555419Swnj 7565440Swnj cp = mtod(om, u_char *); 7575440Swnj cnt = om->m_len; 7585440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 7595440Swnj opt = cp[0]; 7605440Swnj if (opt == TCPOPT_EOL) 7615440Swnj break; 7625440Swnj if (opt == TCPOPT_NOP) 7635440Swnj optlen = 1; 7645440Swnj else 7655440Swnj optlen = cp[1]; 7665440Swnj switch (opt) { 7675440Swnj 7685440Swnj default: 7695440Swnj break; 7705440Swnj 7715440Swnj case TCPOPT_MAXSEG: 7725440Swnj if (optlen != 4) 7735440Swnj continue; 7745440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 7756161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 7765440Swnj break; 7775419Swnj } 7785419Swnj } 7796161Ssam (void) m_free(om); 7805419Swnj } 7815419Swnj 7825419Swnj /* 7835547Swnj * Pull out of band byte out of a segment so 7845547Swnj * it doesn't appear in the user's data queue. 7855547Swnj * It is still reflected in the segment length for 7865547Swnj * sequencing purposes. 7875547Swnj */ 7885547Swnj tcp_pulloutofband(so, ti) 7895547Swnj struct socket *so; 7905547Swnj struct tcpiphdr *ti; 7915547Swnj { 7925547Swnj register struct mbuf *m; 7936116Swnj int cnt = ti->ti_urp - 1; 7945547Swnj 7955547Swnj m = dtom(ti); 7965547Swnj while (cnt >= 0) { 7975547Swnj if (m->m_len > cnt) { 7985547Swnj char *cp = mtod(m, caddr_t) + cnt; 7995547Swnj struct tcpcb *tp = sototcpcb(so); 8005547Swnj 8015547Swnj tp->t_iobc = *cp; 8025547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 8036161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 8045547Swnj m->m_len--; 8055547Swnj return; 8065547Swnj } 8075547Swnj cnt -= m->m_len; 8085547Swnj m = m->m_next; 8095547Swnj if (m == 0) 8105547Swnj break; 8115547Swnj } 8125547Swnj panic("tcp_pulloutofband"); 8135547Swnj } 8145547Swnj 8155547Swnj /* 8165065Swnj * Insert segment ti into reassembly queue of tcp with 8175065Swnj * control block tp. Return TH_FIN if reassembly now includes 8185065Swnj * a segment with FIN. 8195065Swnj */ 8205109Swnj tcp_reass(tp, ti) 8215065Swnj register struct tcpcb *tp; 8225065Swnj register struct tcpiphdr *ti; 8235065Swnj { 8245065Swnj register struct tcpiphdr *q; 8255085Swnj struct socket *so = tp->t_inpcb->inp_socket; 8265263Swnj struct mbuf *m; 8275263Swnj int flags; 8285065Swnj 8295065Swnj /* 8305162Swnj * Call with ti==0 after become established to 8315162Swnj * force pre-ESTABLISHED data up to user socket. 8325065Swnj */ 8335162Swnj if (ti == 0) 8345065Swnj goto present; 8354601Swnj 8365065Swnj /* 8375065Swnj * Find a segment which begins after this one does. 8385065Swnj */ 8395065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8405065Swnj q = (struct tcpiphdr *)q->ti_next) 8415065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8425065Swnj break; 8434601Swnj 8445065Swnj /* 8455065Swnj * If there is a preceding segment, it may provide some of 8465065Swnj * our data already. If so, drop the data from the incoming 8475065Swnj * segment. If it provides all of our data, drop us. 8485065Swnj */ 8495065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 8505065Swnj register int i; 8515690Swnj q = (struct tcpiphdr *)q->ti_prev; 8525065Swnj /* conversion to int (in i) handles seq wraparound */ 8535065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 8545065Swnj if (i > 0) { 8554924Swnj if (i >= ti->ti_len) 8565065Swnj goto drop; 8577338Swnj m_adj(dtom(ti), i); 8585065Swnj ti->ti_len -= i; 8594924Swnj ti->ti_seq += i; 8604601Swnj } 8615065Swnj q = (struct tcpiphdr *)(q->ti_next); 8625065Swnj } 8634601Swnj 8645065Swnj /* 8655065Swnj * While we overlap succeeding segments trim them or, 8665065Swnj * if they are completely covered, dequeue them. 8675065Swnj */ 8685690Swnj while (q != (struct tcpiphdr *)tp) { 8695065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 8705690Swnj if (i <= 0) 8715690Swnj break; 8725065Swnj if (i < q->ti_len) { 8735690Swnj q->ti_seq += i; 8745065Swnj q->ti_len -= i; 8755065Swnj m_adj(dtom(q), i); 8765065Swnj break; 8774601Swnj } 8785065Swnj q = (struct tcpiphdr *)q->ti_next; 8795623Swnj m = dtom(q->ti_prev); 8805065Swnj remque(q->ti_prev); 8815623Swnj m_freem(m); 8825065Swnj } 8834601Swnj 8845065Swnj /* 8855065Swnj * Stick new segment in its place. 8865065Swnj */ 8875065Swnj insque(ti, q->ti_prev); 8884601Swnj 8895065Swnj present: 8905065Swnj /* 8915244Sroot * Present data to user, advancing rcv_nxt through 8925244Sroot * completed sequence space. 8935065Swnj */ 8945263Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 8955244Sroot return (0); 8964924Swnj ti = tp->seg_next; 8975263Swnj if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 8985263Swnj return (0); 8995263Swnj if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 9005263Swnj return (0); 9015263Swnj do { 9025244Sroot tp->rcv_nxt += ti->ti_len; 9035244Sroot flags = ti->ti_flags & TH_FIN; 9044924Swnj remque(ti); 9055263Swnj m = dtom(ti); 9064924Swnj ti = (struct tcpiphdr *)ti->ti_next; 9075263Swnj if (so->so_state & SS_CANTRCVMORE) 9086161Ssam m_freem(m); 90910145Ssam else 9105263Swnj sbappend(&so->so_rcv, m); 9115263Swnj } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 9125263Swnj sorwakeup(so); 9135065Swnj return (flags); 9145065Swnj drop: 9155065Swnj m_freem(dtom(ti)); 9165263Swnj return (0); 9174601Swnj } 918