1*5085Swnj /* tcp_input.c 1.32 81/11/26 */ 24601Swnj 34601Swnj #include "../h/param.h" 44601Swnj #include "../h/systm.h" 54663Swnj #include "../h/mbuf.h" 6*5085Swnj #include "../h/protosw.h" 74663Swnj #include "../h/socket.h" 84803Swnj #include "../h/socketvar.h" 9*5085Swnj #include "../net/in.h" 10*5085Swnj #include "../net/in_pcb.h" 11*5085Swnj #include "../net/in_systm.h" 12*5085Swnj #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" 17*5085Swnj #include "../net/tcp_seq.h" 18*5085Swnj #include "../net/tcp_timer.h" 194803Swnj #include "../net/tcp_var.h" 20*5085Swnj #include "../net/tcpip.h" 214803Swnj #include "/usr/include/errno.h" 224601Swnj 234679Swnj int tcpcksum = 1; 244601Swnj 254953Swnj struct sockaddr_in tcp_sockaddr = { AF_INET }; 264953Swnj 275065Swnj /* 285065Swnj * TCP input routine, follows pages 65-76 of the 295065Swnj * protocol specification dated September, 1981 very closely. 305065Swnj */ 314924Swnj tcp_input(m0) 324924Swnj struct mbuf *m0; 334601Swnj { 344924Swnj register struct tcpiphdr *ti; 354924Swnj struct inpcb *inp; 364924Swnj register struct mbuf *m; 374924Swnj int len, tlen, off; 384924Swnj register struct tcpcb *tp; 394924Swnj register int tiflags; 404803Swnj struct socket *so; 415065Swnj int acceptable; 42*5085Swnj tcp_seq todrop, acked; 434924Swnj 444601Swnj COUNT(TCP_INPUT); 454924Swnj /* 464924Swnj * Get ip and tcp header together in first mbuf. 474924Swnj */ 484924Swnj m = m0; 495020Sroot ti = mtod(m, struct tcpiphdr *); 505020Sroot if (ti->ti_len > sizeof (struct ip)) 515020Sroot ip_stripoptions((struct ip *)ti, (char *)0); 52*5085Swnj if (m->m_len < sizeof (struct tcpiphdr)) { 53*5085Swnj if (m_pullup(m, sizeof (struct tcpiphdr)) == 0) { 54*5085Swnj tcpstat.tcps_hdrops++; 55*5085Swnj goto drop; 56*5085Swnj } 57*5085Swnj ti = mtod(m, struct tcpiphdr *); 58*5085Swnj } 594601Swnj 604601Swnj /* 614924Swnj * Checksum extended tcp header and data. 624601Swnj */ 634924Swnj tlen = ((struct ip *)ti)->ip_len; 644924Swnj len = sizeof (struct ip) + tlen; 654679Swnj if (tcpcksum) { 664924Swnj ti->ti_next = ti->ti_prev = 0; 674924Swnj ti->ti_x1 = 0; 684953Swnj ti->ti_len = htons((u_short)tlen); 69*5085Swnj if ((ti->ti_sum = in_cksum(m, len)) != 0xffff) { 704924Swnj tcpstat.tcps_badsum++; 715065Swnj printf("tcp cksum %x\n", ti->ti_sum); 72*5085Swnj goto drop; 734601Swnj } 744601Swnj } 754601Swnj 764601Swnj /* 774924Swnj * Check that tcp offset makes sense, 784924Swnj * process tcp options and adjust length. 794601Swnj */ 804924Swnj off = ti->ti_off << 2; 814924Swnj if (off < sizeof (struct tcphdr) || off > ti->ti_len) { 824924Swnj tcpstat.tcps_badoff++; 83*5085Swnj goto drop; 844924Swnj } 854924Swnj ti->ti_len = tlen - off; 86*5085Swnj #if 0 87*5085Swnj if (off > sizeof (struct tcphdr) >> 2) 88*5085Swnj tcp_options(ti); 89*5085Swnj #endif 905065Swnj tiflags = ti->ti_flags; 914924Swnj 924924Swnj /* 93*5085Swnj * Convert tcp protocol specific fields to host format. 94*5085Swnj */ 95*5085Swnj ti->ti_seq = ntohl(ti->ti_seq); 96*5085Swnj ti->ti_ack = ntohl(ti->ti_ack); 97*5085Swnj ti->ti_win = ntohs(ti->ti_win); 98*5085Swnj ti->ti_urp = ntohs(ti->ti_urp); 99*5085Swnj 100*5085Swnj /* 1014924Swnj * Locate pcb for segment. 1024924Swnj */ 1035065Swnj inp = in_pcblookup 1045065Swnj (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport); 1055065Swnj 1065065Swnj /* 1075065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 1085065Swnj * all data in the incoming segment is discarded. (p. 65). 1095065Swnj */ 1104884Swnj if (inp == 0) 111*5085Swnj goto dropwithreset; 1125065Swnj tp = intotcpcb(inp); 1135065Swnj if (tp == 0) 114*5085Swnj goto dropwithreset; 1154601Swnj 1164601Swnj /* 117*5085Swnj * Calculate amount of space in receive window, 118*5085Swnj * and then do TCP input processing. 1194601Swnj */ 120*5085Swnj tp->rcv_wnd = sbspace(&so->so_rcv); 1214601Swnj 1224601Swnj switch (tp->t_state) { 1234601Swnj 1245065Swnj /* 1255065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 1265065Swnj * If the segment contains an ACK then it is bad and send a RST. 1275065Swnj * If it does not contain a SYN then it is not interesting; drop it. 128*5085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1295065Swnj * tp->iss, and send a segment: 130*5085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1315065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1325065Swnj * Fill in remote peer address fields if not previously specified. 1335065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 1345065Swnj * segment in this state. (p. 65) 1355065Swnj */ 1365065Swnj case TCPS_LISTEN: 1375065Swnj if (tiflags & TH_RST) 1385065Swnj goto drop; 1395065Swnj if (tiflags & TH_ACK) 140*5085Swnj goto dropwithreset; 1415065Swnj if ((tiflags & TH_SYN) == 0) 1425065Swnj goto drop; 143*5085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 1445065Swnj tp->irs = ti->ti_seq; 145*5085Swnj tcp_sendseqinit(tp); 146*5085Swnj tcp_rcvseqinit(tp); 1475065Swnj tp->t_state = TCPS_SYN_RECEIVED; 1485065Swnj if (inp->inp_faddr.s_addr == 0) { 1495065Swnj inp->inp_faddr = ti->ti_src; 1505065Swnj inp->inp_fport = ti->ti_sport; 1514601Swnj } 152*5085Swnj goto trimthenstep6; 1534601Swnj 1545065Swnj /* 1555065Swnj * If the state is SYN_SENT: 1565065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 1575065Swnj * if seg contains a RST, then drop the connection. 1585065Swnj * if seg does not contain SYN, then drop it. 1595065Swnj * Otherwise this is an acceptable SYN segment 1605065Swnj * initialize tp->rcv_nxt and tp->irs 1615065Swnj * if seg contains ack then advance tp->snd_una 1625065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1635065Swnj * arrange for segment to be acked (eventually) 1645065Swnj * continue processing rest of data/controls, beginning with URG 1655065Swnj */ 1665065Swnj case TCPS_SYN_SENT: 1675065Swnj if ((tiflags & TH_ACK) && 1685065Swnj (SEQ_LEQ(ti->ti_ack, tp->iss) || 169*5085Swnj SEQ_GT(ti->ti_ack, tp->snd_nxt))) 170*5085Swnj goto dropwithreset; 1715065Swnj if (tiflags & TH_RST) { 1725065Swnj if (tiflags & TH_ACK) 173*5085Swnj tcp_drop(tp, ECONNRESET); 1745065Swnj goto drop; 1754601Swnj } 1765065Swnj if ((tiflags & TH_SYN) == 0) 1775065Swnj goto drop; 178*5085Swnj tp->iss = ti->ti_ack; 179*5085Swnj tcp_sendseqinit(tp); 1805065Swnj tp->irs = ti->ti_seq; 181*5085Swnj tcp_rcvseqinit(tp); 182*5085Swnj tp->t_flags |= TF_ACKNOW; 183*5085Swnj if (SEQ_GT(tp->snd_una, tp->iss)) 1845065Swnj tp->t_state = TCPS_ESTABLISHED; 185*5085Swnj else 186*5085Swnj tp->t_state = TCPS_SYN_RECEIVED; 187*5085Swnj goto trimthenstep6; 188*5085Swnj 189*5085Swnj trimthenstep6: 190*5085Swnj /* 191*5085Swnj * If had syn, advance ti->ti_seq to correspond 192*5085Swnj * to first data byte. 193*5085Swnj */ 194*5085Swnj if (tiflags & TH_SYN) 195*5085Swnj ti->ti_seq++; 196*5085Swnj 197*5085Swnj /* 198*5085Swnj * If data, trim to stay within window, 199*5085Swnj * dropping FIN if necessary. 200*5085Swnj */ 201*5085Swnj if (ti->ti_len > tp->rcv_wnd) { 202*5085Swnj todrop = ti->ti_len - tp->rcv_wnd; 203*5085Swnj m_adj(m, -todrop); 204*5085Swnj ti->ti_len = tp->rcv_wnd; 205*5085Swnj ti->ti_flags &= ~TH_FIN; 2065065Swnj } 207*5085Swnj goto step6; 2085065Swnj } 2094601Swnj 2105065Swnj /* 2115065Swnj * States other than LISTEN or SYN_SENT. 2125065Swnj * First check that at least some bytes of segment are within 2135065Swnj * receive window. 2145065Swnj */ 2155065Swnj if (tp->rcv_wnd == 0) { 2165065Swnj /* 2175065Swnj * If window is closed can only take segments at 2185065Swnj * window edge, and have to drop data and EOL from 2195065Swnj * incoming segments. 2205065Swnj */ 2215065Swnj if (tp->rcv_nxt != ti->ti_seq) 2225065Swnj goto dropafterack; 223*5085Swnj if (ti->ti_len > 0) { 224*5085Swnj ti->ti_len = 0; 225*5085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2265065Swnj } 2275065Swnj } else { 2285065Swnj /* 2295065Swnj * If segment begins before rcv_next, drop leading 2305065Swnj * data (and SYN); if nothing left, just ack. 2315065Swnj */ 2325065Swnj if (SEQ_GT(tp->rcv_nxt, ti->ti_seq)) { 233*5085Swnj todrop = tp->rcv_nxt - ti->ti_seq; 234*5085Swnj if (tiflags & TH_SYN) { 235*5085Swnj ti->ti_seq++; 236*5085Swnj if (ti->ti_urp > 1) 237*5085Swnj ti->ti_urp--; 238*5085Swnj else 239*5085Swnj tiflags &= ~TH_URG; 240*5085Swnj todrop--; 241*5085Swnj } 2425065Swnj if (todrop > ti->ti_len) 2435065Swnj goto dropafterack; 2445065Swnj m_adj(m, todrop); 2455065Swnj ti->ti_seq += todrop; 2465065Swnj ti->ti_len -= todrop; 247*5085Swnj if (ti->ti_urp > todrop) 248*5085Swnj ti->ti_urp -= todrop; 249*5085Swnj else { 250*5085Swnj tiflags &= ~TH_URG; 251*5085Swnj /* ti->ti_flags &= ~TH_URG; */ 252*5085Swnj /* ti->ti_urp = 0; */ 253*5085Swnj } 254*5085Swnj /* tiflags &= ~TH_SYN; */ 255*5085Swnj /* ti->ti_flags &= ~TH_SYN; */ 2565065Swnj } 2575065Swnj /* 2585065Swnj * If segment ends after window, drop trailing data 259*5085Swnj * (and PUSH and FIN); if nothing left, just ACK. 2605065Swnj */ 2615065Swnj if (SEQ_GT(ti->ti_seq+ti->ti_len, tp->rcv_nxt+tp->rcv_wnd)) { 262*5085Swnj todrop = 2635065Swnj ti->ti_seq+ti->ti_len - (tp->rcv_nxt+tp->rcv_wnd); 2645065Swnj if (todrop > ti->ti_len) 2655065Swnj goto dropafterack; 2665065Swnj m_adj(m, -todrop); 2675065Swnj ti->ti_len -= todrop; 268*5085Swnj ti->ti_flags &= ~(TH_PUSH|TH_FIN); 2695065Swnj } 2705065Swnj } 2714601Swnj 2725065Swnj /* 2735065Swnj * If the RST bit is set examine the state: 2745065Swnj * SYN_RECEIVED STATE: 2755065Swnj * If passive open, return to LISTEN state. 2765065Swnj * If active open, inform user that connection was refused. 2775065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 2785065Swnj * Inform user that connection was reset, and close tcb. 2795065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 2805065Swnj * Close the tcb. 2815065Swnj */ 2825065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 2835065Swnj 2845065Swnj case TCPS_SYN_RECEIVED: 2855065Swnj if (inp->inp_socket->so_options & SO_ACCEPTCONN) { 286*5085Swnj tp->t_state = TCPS_LISTEN; 287*5085Swnj inp->inp_faddr.s_addr = 0; 2885065Swnj goto drop; 2894601Swnj } 290*5085Swnj tcp_drop(tp, ECONNREFUSED); 2915065Swnj goto drop; 2924601Swnj 2935065Swnj case TCPS_ESTABLISHED: 2945065Swnj case TCPS_FIN_WAIT_1: 2955065Swnj case TCPS_FIN_WAIT_2: 2965065Swnj case TCPS_CLOSE_WAIT: 2975065Swnj tcp_drop(tp, ECONNRESET); 2985065Swnj goto drop; 2995065Swnj 3005065Swnj case TCPS_CLOSING: 3015065Swnj case TCPS_LAST_ACK: 3025065Swnj case TCPS_TIME_WAIT: 3035065Swnj tcp_close(tp); 3045065Swnj goto drop; 3054601Swnj } 3064601Swnj 3074601Swnj /* 3085065Swnj * If a SYN is in the window, then this is an 3095065Swnj * error and we send an RST and drop the connection. 3104601Swnj */ 3115065Swnj if (tiflags & TH_SYN) { 312*5085Swnj tcp_drop(tp, ECONNABORTED); 313*5085Swnj goto dropwithreset; 3144601Swnj } 3154601Swnj 3164601Swnj /* 3175065Swnj * If the ACK bit is off we drop the segment and return. 3184601Swnj */ 319*5085Swnj if ((tiflags & TH_ACK) == 0) 3205065Swnj goto drop; 3215065Swnj 3225065Swnj /* 3235065Swnj * Ack processing. 3245065Swnj */ 3254601Swnj switch (tp->t_state) { 3264601Swnj 3275065Swnj /* 3285065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 3295065Swnj * ESTABLISHED state and continue processing, othewise 3305065Swnj * send an RST. 3315065Swnj */ 3325065Swnj case TCPS_SYN_RECEIVED: 333*5085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 334*5085Swnj SEQ_GT(ti->ti_ack, tp->snd_nxt)) 335*5085Swnj goto dropwithreset; 336*5085Swnj soisconnected(so); 337*5085Swnj tp->t_state = TCPS_ESTABLISHED; 338*5085Swnj /* fall into ... */ 3394601Swnj 3405065Swnj /* 3415065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 3425065Swnj * ACKs. If the ack is in the range 3435065Swnj * tp->snd_una < ti->ti_ack <= tp->snd_nxt 3445065Swnj * then advance tp->snd_una to ti->ti_ack and drop 3455065Swnj * data from the retransmission queue. If this ACK reflects 3465065Swnj * more up to date window information we update our window information. 3475065Swnj */ 3485065Swnj case TCPS_ESTABLISHED: 3495065Swnj case TCPS_FIN_WAIT_1: 3505065Swnj case TCPS_FIN_WAIT_2: 3515065Swnj case TCPS_CLOSE_WAIT: 3525065Swnj case TCPS_CLOSING: 353*5085Swnj #define ourfinisacked (acked > 0) 354*5085Swnj 3555065Swnj if (SEQ_LT(ti->ti_ack, tp->snd_una)) 3565065Swnj break; 3575065Swnj if (SEQ_GT(ti->ti_ack, tp->snd_nxt)) 3585065Swnj goto dropafterack; 359*5085Swnj acked = ti->ti_ack - tp->snd_una; 360*5085Swnj if (acked > so->so_snd.sb_cc) { 361*5085Swnj sbflush(&so->so_snd); 362*5085Swnj acked -= so->so_snd.sb_cc; 363*5085Swnj } else { 364*5085Swnj sbdrop(&so->so_snd, acked); 365*5085Swnj acked = 0; 366*5085Swnj } 367*5085Swnj /* if acked our FIN is acked */ 3685065Swnj tp->snd_una = ti->ti_ack; 369*5085Swnj 370*5085Swnj /* 371*5085Swnj * Update window information. 372*5085Swnj */ 3735065Swnj if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 374*5085Swnj tp->snd_wl1==ti->ti_seq && SEQ_LEQ(tp->snd_wl2,ti->ti_seq)) { 3755065Swnj tp->snd_wnd = ti->ti_win; 3765065Swnj tp->snd_wl1 = ti->ti_seq; 3775065Swnj tp->snd_wl2 = ti->ti_ack; 3784601Swnj } 3794601Swnj 3804601Swnj switch (tp->t_state) { 3814601Swnj 3825065Swnj /* 3835065Swnj * In FIN_WAIT_1 STATE in addition to the processing 3845065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 385*5085Swnj * then enter FIN_WAIT_2. 3865065Swnj */ 3875065Swnj case TCPS_FIN_WAIT_1: 388*5085Swnj if (ourfinisacked) 389*5085Swnj tp->t_state = TCPS_FIN_WAIT_2; 3904601Swnj break; 3914601Swnj 3925065Swnj /* 3935065Swnj * In CLOSING STATE in addition to the processing for 3945065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 3955065Swnj * then enter the TIME-WAIT state, otherwise ignore 3965065Swnj * the segment. 3975065Swnj */ 3985065Swnj case TCPS_CLOSING: 399*5085Swnj if (ourfinisacked) 4005065Swnj tp->t_state = TCPS_TIME_WAIT; 401*5085Swnj goto drop; 4024601Swnj 4035065Swnj /* 404*5085Swnj * The only thing that can arrive in LAST_ACK state 405*5085Swnj * is an acknowledgment of our FIN. If our FIN is now 406*5085Swnj * acknowledged, delete the TCB, enter the closed state 407*5085Swnj * and return. 4085065Swnj */ 4095065Swnj case TCPS_LAST_ACK: 410*5085Swnj if (ourfinisacked) 4115065Swnj tcp_close(tp); 4125065Swnj goto drop; 4134601Swnj 4145065Swnj /* 4155065Swnj * In TIME_WAIT state the only thing that should arrive 4165065Swnj * is a retransmission of the remote FIN. Acknowledge 4175065Swnj * it and restart the finack timer. 4185065Swnj */ 4195065Swnj case TCPS_TIME_WAIT: 420*5085Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPSC_MSL; 4215065Swnj goto dropafterack; 4224601Swnj } 423*5085Swnj #undef ourfinisacked 424*5085Swnj } 4254601Swnj 4265065Swnj step6: 4275065Swnj /* 4285065Swnj * If an URG bit is set in the segment and is greater than the 4295065Swnj * current known urgent pointer, then signal the user that the 4305065Swnj * remote side has urgent data. This should not happen 4315065Swnj * in CLOSE_WAIT, CLOSING, LAST-ACK or TIME_WAIT STATES since 4325065Swnj * a FIN has been received from the remote side. In these states 4335065Swnj * we ignore the URG. 4345065Swnj */ 435*5085Swnj if ((tiflags & TH_URG) == 0 && TCPS_HAVERCVDFIN(tp->t_state) == 0) 436*5085Swnj if (SEQ_GT(ti->ti_urp, tp->rcv_up)) { 4375065Swnj tp->rcv_up = ti->ti_urp; 438*5085Swnj #if 0 4395065Swnj soisurgendata(so); /* XXX */ 440*5085Swnj #endif 4414601Swnj } 4424601Swnj 4434601Swnj /* 4445065Swnj * Process the segment text, merging it into the TCP sequencing queue, 4455065Swnj * and arranging for acknowledgment of receipt if necessary. 4465065Swnj * This process logically involves adjusting tp->rcv_wnd as data 4475065Swnj * is presented to the user (this happens in tcp_usrreq.c, 4485065Swnj * case PRU_RCVD). If a FIN has already been received on this 4495065Swnj * connection then we just ignore the text. 4504601Swnj */ 4515065Swnj if (ti->ti_len) { 452*5085Swnj if (TCPS_HAVERCVDFIN(tp->t_state)) 4535065Swnj goto drop; 454*5085Swnj off += sizeof (struct ip); /* drop IP header */ 455*5085Swnj m->m_off += off; 456*5085Swnj m->m_len -= off; 4575065Swnj tiflags = tcp_reass(tp, ti); 458*5085Swnj tp->t_flags |= TF_ACKNOW; /* XXX TF_DELACK */ 459*5085Swnj } else 4604924Swnj m_freem(m); 4614601Swnj 4624601Swnj /* 4635065Swnj * If FIN is received then if we haven't received SYN and 4645065Swnj * therefore can't validate drop the segment. Otherwise ACK 4655065Swnj * the FIN and let the user know that the connection is closing. 4664601Swnj */ 467*5085Swnj if ((tiflags & TH_FIN)) { 4685074Swnj if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 4695074Swnj goto drop; 4705074Swnj socantrcvmore(so); 4715065Swnj tp->t_flags |= TF_ACKNOW; 4725065Swnj tp->rcv_nxt++; 4735065Swnj switch (tp->t_state) { 4744601Swnj 4755065Swnj /* 4765065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 4775065Swnj * enter the CLOSE_WAIT state. 4784884Swnj */ 4795065Swnj case TCPS_SYN_RECEIVED: 4805065Swnj case TCPS_ESTABLISHED: 4815065Swnj tp->t_state = TCPS_CLOSE_WAIT; 4825065Swnj break; 4834884Swnj 4845065Swnj /* 485*5085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 486*5085Swnj * enter the CLOSING state. 4874884Swnj */ 4885065Swnj case TCPS_FIN_WAIT_1: 489*5085Swnj tp->t_state = TCPS_CLOSING; 4905065Swnj break; 4914601Swnj 4925065Swnj /* 4935065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 4945065Swnj * starting the time-wait timer, turning off the other 4955065Swnj * standard timers. 4965065Swnj */ 4975065Swnj case TCPS_FIN_WAIT_2: 498*5085Swnj tp->t_state = TCPS_TIME_WAIT;; 4995074Swnj tcp_canceltimers(tp); 500*5085Swnj tp->t_timer[TCPT_2MSL] = TCPSC_2MSL; 5015065Swnj break; 5025065Swnj 5034884Swnj /* 5045065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 5054884Swnj */ 5065065Swnj case TCPS_TIME_WAIT: 507*5085Swnj tp->t_timer[TCPT_2MSL] = TCPSC_2MSL; 5085065Swnj break; 509*5085Swnj } 5104601Swnj } 511*5085Swnj 512*5085Swnj /* 513*5085Swnj * Return any desired output. 514*5085Swnj */ 515*5085Swnj tcp_output(tp); 5165065Swnj return; 517*5085Swnj 5185065Swnj dropafterack: 519*5085Swnj /* 520*5085Swnj * Generate an ACK, then drop incoming segment. 521*5085Swnj * Make ACK reflect our state. 522*5085Swnj */ 523*5085Swnj if (tiflags & TH_RST) 524*5085Swnj goto drop; 525*5085Swnj tcp_respond(ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 526*5085Swnj goto drop; 527*5085Swnj 528*5085Swnj dropwithreset: 529*5085Swnj /* 530*5085Swnj * Generate a RST, then drop incoming segment. 531*5085Swnj * Make ACK acceptable to originator of segment. 532*5085Swnj */ 533*5085Swnj if (tiflags & TH_RST) 534*5085Swnj goto drop; 535*5085Swnj if (tiflags & TH_ACK) 536*5085Swnj tcp_respond(ti, 0, ti->ti_ack, TH_RST); 537*5085Swnj else { 538*5085Swnj if (tiflags & TH_SYN) 539*5085Swnj ti->ti_len++; 540*5085Swnj tcp_respond(ti, ti->ti_seq+ti->ti_len, 0, TH_RST|TH_ACK); 541*5085Swnj } 542*5085Swnj goto drop; 543*5085Swnj 5445065Swnj drop: 545*5085Swnj /* 546*5085Swnj * Drop space held by incoming segment and return. 547*5085Swnj */ 5485065Swnj m_freem(m); 5495065Swnj } 5505065Swnj 5515065Swnj /* 5525065Swnj * Insert segment ti into reassembly queue of tcp with 5535065Swnj * control block tp. Return TH_FIN if reassembly now includes 5545065Swnj * a segment with FIN. 5555065Swnj */ 5565065Swnj tcp_reass(tp, ti, endp) 5575065Swnj register struct tcpcb *tp; 5585065Swnj register struct tcpiphdr *ti; 5595065Swnj int *endp; 5605065Swnj { 5615065Swnj register struct tcpiphdr *q; 562*5085Swnj struct socket *so = tp->t_inpcb->inp_socket; 5635065Swnj int flags = 0; /* no FIN */ 5645065Swnj int overage; 565*5085Swnj COUNT(TCP_REASS); 5665065Swnj 5675065Swnj /* 5685065Swnj * If no data in this segment may want 5695065Swnj * to move data up to socket structure (if 5705065Swnj * connection is now established). 5715065Swnj */ 5725065Swnj if (ti->ti_len == 0) { 5735065Swnj m_freem(dtom(ti)); 5745065Swnj goto present; 5754601Swnj } 5764601Swnj 5775065Swnj /* 5785065Swnj * Find a segment which begins after this one does. 5795065Swnj */ 5805065Swnj for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 5815065Swnj q = (struct tcpiphdr *)q->ti_next) 5825065Swnj if (SEQ_GT(q->ti_seq, ti->ti_seq)) 5835065Swnj break; 5844601Swnj 5855065Swnj /* 5865065Swnj * If there is a preceding segment, it may provide some of 5875065Swnj * our data already. If so, drop the data from the incoming 5885065Swnj * segment. If it provides all of our data, drop us. 5895065Swnj */ 5905065Swnj if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 5915065Swnj register int i; 5925065Swnj q = (struct tcpiphdr *)(q->ti_prev); 5935065Swnj /* conversion to int (in i) handles seq wraparound */ 5945065Swnj i = q->ti_seq + q->ti_len - ti->ti_seq; 5955065Swnj if (i > 0) { 5964924Swnj if (i >= ti->ti_len) 5975065Swnj goto drop; 5985065Swnj m_adj(dtom(tp), i); 5995065Swnj ti->ti_len -= i; 6004924Swnj ti->ti_seq += i; 6014601Swnj } 6025065Swnj q = (struct tcpiphdr *)(q->ti_next); 6035065Swnj } 6044601Swnj 6055065Swnj /* 6065065Swnj * While we overlap succeeding segments trim them or, 6075065Swnj * if they are completely covered, dequeue them. 6085065Swnj */ 6095065Swnj while (q != (struct tcpiphdr *)tp && 6105065Swnj SEQ_GT(ti->ti_seq + ti->ti_len, q->ti_seq)) { 6115065Swnj register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 6125065Swnj if (i < q->ti_len) { 6135065Swnj q->ti_len -= i; 6145065Swnj m_adj(dtom(q), i); 6155065Swnj break; 6164601Swnj } 6175065Swnj q = (struct tcpiphdr *)q->ti_next; 6185065Swnj m_freem(dtom(q->ti_prev)); 6195065Swnj remque(q->ti_prev); 6205065Swnj } 6214601Swnj 6225065Swnj /* 6235065Swnj * Stick new segment in its place. 6245065Swnj */ 6255065Swnj insque(ti, q->ti_prev); 626*5085Swnj tp->t_seqcnt += ti->ti_len; 6274601Swnj 6285065Swnj /* 6295065Swnj * Calculate available space and discard segments for 6305065Swnj * which there is too much. 6315065Swnj */ 6325065Swnj overage = 633*5085Swnj (so->so_rcv.sb_cc + tp->t_seqcnt) - so->so_rcv.sb_hiwat; 6345065Swnj if (overage > 0) { 6355065Swnj q = tp->seg_prev; 6365065Swnj for (;;) { 6375065Swnj register int i = MIN(q->ti_len, overage); 6385065Swnj overage -= i; 639*5085Swnj tp->t_seqcnt -= i; 6405065Swnj q->ti_len -= i; 6415065Swnj m_adj(dtom(q), -i); 6425065Swnj if (q->ti_len) 6434645Swnj break; 6445065Swnj if (q == ti) 6455074Swnj panic("tcp_reass dropall"); 6465065Swnj q = (struct tcpiphdr *)q->ti_prev; 6475065Swnj remque(q->ti_next); 6484645Swnj } 6494884Swnj } 6505065Swnj 6515065Swnj /* 6525065Swnj * Advance rcv_next through newly completed sequence space. 6535065Swnj */ 6545065Swnj while (ti->ti_seq == tp->rcv_nxt) { 6555065Swnj tp->rcv_nxt += ti->ti_len; 6565065Swnj flags = ti->ti_flags & TH_FIN; 6575065Swnj ti = (struct tcpiphdr *)ti->ti_next; 6585065Swnj if (ti == (struct tcpiphdr *)tp) 6595065Swnj break; 6604679Swnj } 6614679Swnj 6625065Swnj present: 6635065Swnj /* 6645065Swnj * Present data to user. 6655065Swnj */ 666*5085Swnj if (tp->t_state < TCPS_ESTABLISHED) 6675065Swnj return (flags); 6684924Swnj ti = tp->seg_next; 6694924Swnj while (ti != (struct tcpiphdr *)tp && ti->ti_seq < tp->rcv_nxt) { 6704924Swnj remque(ti); 6714924Swnj sbappend(&so->so_rcv, dtom(ti)); 672*5085Swnj tp->t_seqcnt -= ti->ti_len; 673*5085Swnj if (tp->t_seqcnt < 0) 6745065Swnj panic("tcp_reass"); 6754924Swnj ti = (struct tcpiphdr *)ti->ti_next; 6764601Swnj } 6775074Swnj if (so->so_state & SS_CANTRCVMORE) 6785074Swnj sbflush(&so->so_rcv); 6795074Swnj else 6805074Swnj sorwakeup(so); 6815065Swnj return (flags); 6825065Swnj drop: 6835065Swnj m_freem(dtom(ti)); 6845065Swnj return (flags); 6854601Swnj } 686