123190Smckusick /* 244375Skarels * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. 332787Sbostic * All rights reserved. 423190Smckusick * 544485Sbostic * %sccs.include.redist.c% 632787Sbostic * 7*52199Skarels * @(#)tcp_input.c 7.26 (Berkeley) 01/14/92 823190Smckusick */ 94601Swnj 1017062Sbloom #include "param.h" 1117062Sbloom #include "systm.h" 1237320Skarels #include "malloc.h" 1317062Sbloom #include "mbuf.h" 1417062Sbloom #include "protosw.h" 1517062Sbloom #include "socket.h" 1617062Sbloom #include "socketvar.h" 1717062Sbloom #include "errno.h" 1810894Ssam 1910894Ssam #include "../net/if.h" 2010894Ssam #include "../net/route.h" 2110894Ssam 2217062Sbloom #include "in.h" 2317062Sbloom #include "in_systm.h" 2417062Sbloom #include "ip.h" 2540687Skarels #include "in_pcb.h" 2617062Sbloom #include "ip_var.h" 2717062Sbloom #include "tcp.h" 2817062Sbloom #include "tcp_fsm.h" 2917062Sbloom #include "tcp_seq.h" 3017062Sbloom #include "tcp_timer.h" 3117062Sbloom #include "tcp_var.h" 3217062Sbloom #include "tcpip.h" 3317062Sbloom #include "tcp_debug.h" 344601Swnj 3532098Skarels int tcprexmtthresh = 3; 3644375Skarels int tcppredack; /* XXX debugging: times hdr predict ok for acks */ 3744375Skarels int tcppreddat; /* XXX # times header prediction ok for data packets */ 3844375Skarels int tcppcbcachemiss; 395267Sroot struct tcpiphdr tcp_saveti; 4044375Skarels struct inpcb *tcp_last_inpcb = &tcb; 414601Swnj 425267Sroot struct tcpcb *tcp_newtcpcb(); 4324816Skarels 445065Swnj /* 4524816Skarels * Insert segment ti into reassembly queue of tcp with 4624816Skarels * control block tp. Return TH_FIN if reassembly now includes 4724816Skarels * a segment with FIN. The macro form does the common case inline 4824816Skarels * (segment is the next to be received on an established connection, 4924816Skarels * and the queue is empty), avoiding linkage into and removal 5024816Skarels * from the queue and repetition of various conversions. 5134278Skarels * Set DELACK for segments received in order, but ack immediately 5234278Skarels * when segments are out of order (so fast retransmit can work). 5324816Skarels */ 5424816Skarels #define TCP_REASS(tp, ti, m, so, flags) { \ 5524816Skarels if ((ti)->ti_seq == (tp)->rcv_nxt && \ 5624816Skarels (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 5724816Skarels (tp)->t_state == TCPS_ESTABLISHED) { \ 5834278Skarels tp->t_flags |= TF_DELACK; \ 5924816Skarels (tp)->rcv_nxt += (ti)->ti_len; \ 6024816Skarels flags = (ti)->ti_flags & TH_FIN; \ 6130525Skarels tcpstat.tcps_rcvpack++;\ 6230525Skarels tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 6324816Skarels sbappend(&(so)->so_rcv, (m)); \ 6424816Skarels sorwakeup(so); \ 6534278Skarels } else { \ 6644375Skarels (flags) = tcp_reass((tp), (ti), (m)); \ 6734278Skarels tp->t_flags |= TF_ACKNOW; \ 6834278Skarels } \ 6924816Skarels } 7024816Skarels 7144375Skarels tcp_reass(tp, ti, m) 7224816Skarels register struct tcpcb *tp; 7324816Skarels register struct tcpiphdr *ti; 7444375Skarels struct mbuf *m; 7524816Skarels { 7624816Skarels register struct tcpiphdr *q; 7724816Skarels struct socket *so = tp->t_inpcb->inp_socket; 7824816Skarels int flags; 7924816Skarels 8024816Skarels /* 8124816Skarels * Call with ti==0 after become established to 8224816Skarels * force pre-ESTABLISHED data up to user socket. 8324816Skarels */ 8424816Skarels if (ti == 0) 8524816Skarels goto present; 8624816Skarels 8724816Skarels /* 8824816Skarels * Find a segment which begins after this one does. 8924816Skarels */ 9024816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 9124816Skarels q = (struct tcpiphdr *)q->ti_next) 9224816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq)) 9324816Skarels break; 9424816Skarels 9524816Skarels /* 9624816Skarels * If there is a preceding segment, it may provide some of 9724816Skarels * our data already. If so, drop the data from the incoming 9824816Skarels * segment. If it provides all of our data, drop us. 9924816Skarels */ 10024816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 10124816Skarels register int i; 10224816Skarels q = (struct tcpiphdr *)q->ti_prev; 10324816Skarels /* conversion to int (in i) handles seq wraparound */ 10424816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq; 10524816Skarels if (i > 0) { 10630525Skarels if (i >= ti->ti_len) { 10730525Skarels tcpstat.tcps_rcvduppack++; 10830525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 10944375Skarels m_freem(m); 11044375Skarels return (0); 11130525Skarels } 11244375Skarels m_adj(m, i); 11324816Skarels ti->ti_len -= i; 11424816Skarels ti->ti_seq += i; 11524816Skarels } 11624816Skarels q = (struct tcpiphdr *)(q->ti_next); 11724816Skarels } 11830525Skarels tcpstat.tcps_rcvoopack++; 11930525Skarels tcpstat.tcps_rcvoobyte += ti->ti_len; 12044375Skarels REASS_MBUF(ti) = m; /* XXX */ 12124816Skarels 12224816Skarels /* 12324816Skarels * While we overlap succeeding segments trim them or, 12424816Skarels * if they are completely covered, dequeue them. 12524816Skarels */ 12624816Skarels while (q != (struct tcpiphdr *)tp) { 12724816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 12824816Skarels if (i <= 0) 12924816Skarels break; 13024816Skarels if (i < q->ti_len) { 13124816Skarels q->ti_seq += i; 13224816Skarels q->ti_len -= i; 13344375Skarels m_adj(REASS_MBUF(q), i); 13424816Skarels break; 13524816Skarels } 13624816Skarels q = (struct tcpiphdr *)q->ti_next; 13744375Skarels m = REASS_MBUF((struct tcpiphdr *)q->ti_prev); 13824816Skarels remque(q->ti_prev); 13924816Skarels m_freem(m); 14024816Skarels } 14124816Skarels 14224816Skarels /* 14324816Skarels * Stick new segment in its place. 14424816Skarels */ 14524816Skarels insque(ti, q->ti_prev); 14624816Skarels 14724816Skarels present: 14824816Skarels /* 14924816Skarels * Present data to user, advancing rcv_nxt through 15024816Skarels * completed sequence space. 15124816Skarels */ 15224816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 15324816Skarels return (0); 15424816Skarels ti = tp->seg_next; 15524816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 15624816Skarels return (0); 15724816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 15824816Skarels return (0); 15924816Skarels do { 16024816Skarels tp->rcv_nxt += ti->ti_len; 16124816Skarels flags = ti->ti_flags & TH_FIN; 16224816Skarels remque(ti); 16344375Skarels m = REASS_MBUF(ti); 16424816Skarels ti = (struct tcpiphdr *)ti->ti_next; 16524816Skarels if (so->so_state & SS_CANTRCVMORE) 16624816Skarels m_freem(m); 16724816Skarels else 16824816Skarels sbappend(&so->so_rcv, m); 16924816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 17024816Skarels sorwakeup(so); 17124816Skarels return (flags); 17224816Skarels } 17324816Skarels 17424816Skarels /* 1755065Swnj * TCP input routine, follows pages 65-76 of the 1765065Swnj * protocol specification dated September, 1981 very closely. 1775065Swnj */ 17837320Skarels tcp_input(m, iphlen) 17937320Skarels register struct mbuf *m; 18037320Skarels int iphlen; 1814601Swnj { 1824924Swnj register struct tcpiphdr *ti; 18344375Skarels register struct inpcb *inp; 1845440Swnj struct mbuf *om = 0; 1854924Swnj int len, tlen, off; 1865391Swnj register struct tcpcb *tp = 0; 1874924Swnj register int tiflags; 1884803Swnj struct socket *so; 18931721Skarels int todrop, acked, ourfinisacked, needoutput = 0; 1905267Sroot short ostate; 1916028Sroot struct in_addr laddr; 19210769Ssam int dropsocket = 0; 19330525Skarels int iss = 0; 1944924Swnj 19530525Skarels tcpstat.tcps_rcvtotal++; 1964924Swnj /* 1975244Sroot * Get IP and TCP header together in first mbuf. 1985244Sroot * Note: IP leaves IP header in first mbuf. 1994924Swnj */ 2005020Sroot ti = mtod(m, struct tcpiphdr *); 20137320Skarels if (iphlen > sizeof (struct ip)) 20237320Skarels ip_stripoptions(m, (struct mbuf *)0); 20344375Skarels if (m->m_len < sizeof (struct tcpiphdr)) { 2045307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 20530525Skarels tcpstat.tcps_rcvshort++; 2065307Sroot return; 2075085Swnj } 2085085Swnj ti = mtod(m, struct tcpiphdr *); 2095085Swnj } 2104601Swnj 2114601Swnj /* 2125244Sroot * Checksum extended TCP header and data. 2134601Swnj */ 2144924Swnj tlen = ((struct ip *)ti)->ip_len; 2154924Swnj len = sizeof (struct ip) + tlen; 21637320Skarels ti->ti_next = ti->ti_prev = 0; 21737320Skarels ti->ti_x1 = 0; 21837320Skarels ti->ti_len = (u_short)tlen; 21944375Skarels HTONS(ti->ti_len); 22037320Skarels if (ti->ti_sum = in_cksum(m, len)) { 22137320Skarels tcpstat.tcps_rcvbadsum++; 22237320Skarels goto drop; 2234601Swnj } 2244601Swnj 2254601Swnj /* 2265244Sroot * Check that TCP offset makes sense, 22744375Skarels * pull out TCP options and adjust length. XXX 2284601Swnj */ 2294924Swnj off = ti->ti_off << 2; 2305231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 23130525Skarels tcpstat.tcps_rcvbadoff++; 2325085Swnj goto drop; 2334924Swnj } 2346211Swnj tlen -= off; 2356211Swnj ti->ti_len = tlen; 2365440Swnj if (off > sizeof (struct tcphdr)) { 23724816Skarels if (m->m_len < sizeof(struct ip) + off) { 23824816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 23930525Skarels tcpstat.tcps_rcvshort++; 24024816Skarels return; 24124816Skarels } 24224816Skarels ti = mtod(m, struct tcpiphdr *); 2435440Swnj } 2449642Ssam om = m_get(M_DONTWAIT, MT_DATA); 2455440Swnj if (om == 0) 2465440Swnj goto drop; 2475440Swnj om->m_len = off - sizeof (struct tcphdr); 2485440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 2496161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 2505440Swnj m->m_len -= om->m_len; 25137320Skarels m->m_pkthdr.len -= om->m_len; 2526161Ssam bcopy(op+om->m_len, op, 2536161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 2545440Swnj } 2555440Swnj } 2565065Swnj tiflags = ti->ti_flags; 2574924Swnj 2586093Sroot /* 2595244Sroot * Convert TCP protocol specific fields to host format. 2605085Swnj */ 26144375Skarels NTOHL(ti->ti_seq); 26244375Skarels NTOHL(ti->ti_ack); 26344375Skarels NTOHS(ti->ti_win); 26444375Skarels NTOHS(ti->ti_urp); 2655085Swnj 2665085Swnj /* 2678271Sroot * Locate pcb for segment. 2684924Swnj */ 26930525Skarels findpcb: 27044375Skarels inp = tcp_last_inpcb; 27144375Skarels if (inp->inp_lport != ti->ti_dport || 27244375Skarels inp->inp_fport != ti->ti_sport || 27344375Skarels inp->inp_faddr.s_addr != ti->ti_src.s_addr || 27444375Skarels inp->inp_laddr.s_addr != ti->ti_dst.s_addr) { 27544375Skarels inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport, 27644375Skarels ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD); 27744375Skarels if (inp) 27844375Skarels tcp_last_inpcb = inp; 27944375Skarels ++tcppcbcachemiss; 28044375Skarels } 2815065Swnj 2825065Swnj /* 2835065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 2845244Sroot * all data in the incoming segment is discarded. 28532098Skarels * If the TCB exists but is in CLOSED state, it is embryonic, 28632098Skarels * but should either do a listen or a connect soon. 2875065Swnj */ 2885300Sroot if (inp == 0) 2895085Swnj goto dropwithreset; 2905065Swnj tp = intotcpcb(inp); 2915300Sroot if (tp == 0) 2925085Swnj goto dropwithreset; 29332098Skarels if (tp->t_state == TCPS_CLOSED) 29432098Skarels goto drop; 2955109Swnj so = inp->inp_socket; 29644375Skarels if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 29744375Skarels if (so->so_options & SO_DEBUG) { 29844375Skarels ostate = tp->t_state; 29944375Skarels tcp_saveti = *ti; 30044375Skarels } 30144375Skarels if (so->so_options & SO_ACCEPTCONN) { 30244375Skarels so = sonewconn(so, 0); 30344375Skarels if (so == 0) 30444375Skarels goto drop; 30544375Skarels /* 30644375Skarels * This is ugly, but .... 30744375Skarels * 30844375Skarels * Mark socket as temporary until we're 30944375Skarels * committed to keeping it. The code at 31044375Skarels * ``drop'' and ``dropwithreset'' check the 31144375Skarels * flag dropsocket to see if the temporary 31244375Skarels * socket created here should be discarded. 31344375Skarels * We mark the socket as discardable until 31444375Skarels * we're committed to it below in TCPS_LISTEN. 31544375Skarels */ 31644375Skarels dropsocket++; 31744375Skarels inp = (struct inpcb *)so->so_pcb; 31844375Skarels inp->inp_laddr = ti->ti_dst; 31944375Skarels inp->inp_lport = ti->ti_dport; 32044375Skarels #if BSD>=43 32144375Skarels inp->inp_options = ip_srcroute(); 32244375Skarels #endif 32344375Skarels tp = intotcpcb(inp); 32444375Skarels tp->t_state = TCPS_LISTEN; 32544375Skarels } 3265267Sroot } 3274601Swnj 3284601Swnj /* 3295162Swnj * Segment received on connection. 3305162Swnj * Reset idle time and keep-alive timer. 3315162Swnj */ 3325162Swnj tp->t_idle = 0; 33333745Skarels tp->t_timer[TCPT_KEEP] = tcp_keepidle; 3345162Swnj 3355162Swnj /* 33617272Skarels * Process options if not in LISTEN state, 33717272Skarels * else do it below (after getting remote address). 3385440Swnj */ 33917272Skarels if (om && tp->t_state != TCPS_LISTEN) { 34017272Skarels tcp_dooptions(tp, om, ti); 3415440Swnj om = 0; 3425440Swnj } 34344375Skarels /* 34444375Skarels * Header prediction: check for the two common cases 34544375Skarels * of a uni-directional data xfer. If the packet has 34644375Skarels * no control flags, is in-sequence, the window didn't 34744375Skarels * change and we're not retransmitting, it's a 34844375Skarels * candidate. If the length is zero and the ack moved 34944375Skarels * forward, we're the sender side of the xfer. Just 35044375Skarels * free the data acked & wake any higher level process 35144375Skarels * that was blocked waiting for space. If the length 35244375Skarels * is non-zero and the ack didn't move, we're the 35344375Skarels * receiver side. If we're getting packets in-order 35444375Skarels * (the reassembly queue is empty), add the data to 35544375Skarels * the socket buffer and note that we need a delayed ack. 35644375Skarels */ 35744375Skarels if (tp->t_state == TCPS_ESTABLISHED && 35844375Skarels (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 35944375Skarels ti->ti_seq == tp->rcv_nxt && 36044375Skarels ti->ti_win && ti->ti_win == tp->snd_wnd && 36144375Skarels tp->snd_nxt == tp->snd_max) { 36244375Skarels if (ti->ti_len == 0) { 36344375Skarels if (SEQ_GT(ti->ti_ack, tp->snd_una) && 36444375Skarels SEQ_LEQ(ti->ti_ack, tp->snd_max) && 36544375Skarels tp->snd_cwnd >= tp->snd_wnd) { 36644375Skarels /* 36744375Skarels * this is a pure ack for outstanding data. 36844375Skarels */ 36944375Skarels ++tcppredack; 37044375Skarels if (tp->t_rtt && SEQ_GT(ti->ti_ack,tp->t_rtseq)) 37144375Skarels tcp_xmit_timer(tp); 37244375Skarels acked = ti->ti_ack - tp->snd_una; 37344375Skarels tcpstat.tcps_rcvackpack++; 37444375Skarels tcpstat.tcps_rcvackbyte += acked; 37544375Skarels sbdrop(&so->so_snd, acked); 37644375Skarels tp->snd_una = ti->ti_ack; 37744375Skarels m_freem(m); 3785440Swnj 37944375Skarels /* 38044375Skarels * If all outstanding data are acked, stop 38144375Skarels * retransmit timer, otherwise restart timer 38244375Skarels * using current (possibly backed-off) value. 38344375Skarels * If process is waiting for space, 38444375Skarels * wakeup/selwakeup/signal. If data 38544375Skarels * are ready to send, let tcp_output 38644375Skarels * decide between more output or persist. 38744375Skarels */ 38844375Skarels if (tp->snd_una == tp->snd_max) 38944375Skarels tp->t_timer[TCPT_REXMT] = 0; 39044375Skarels else if (tp->t_timer[TCPT_PERSIST] == 0) 39144375Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 39244375Skarels 39344375Skarels if (so->so_snd.sb_flags & SB_NOTIFY) 39444375Skarels sowwakeup(so); 39544375Skarels if (so->so_snd.sb_cc) 39644375Skarels (void) tcp_output(tp); 39744375Skarels return; 39844375Skarels } 39944375Skarels } else if (ti->ti_ack == tp->snd_una && 40044375Skarels tp->seg_next == (struct tcpiphdr *)tp && 40144375Skarels ti->ti_len <= sbspace(&so->so_rcv)) { 40244375Skarels /* 40344375Skarels * this is a pure, in-sequence data packet 40444375Skarels * with nothing on the reassembly queue and 40544375Skarels * we have enough buffer space to take it. 40644375Skarels */ 40744375Skarels ++tcppreddat; 40844375Skarels tp->rcv_nxt += ti->ti_len; 40944375Skarels tcpstat.tcps_rcvpack++; 41044375Skarels tcpstat.tcps_rcvbyte += ti->ti_len; 41144375Skarels /* 41244375Skarels * Drop TCP and IP headers then add data 41344375Skarels * to socket buffer 41444375Skarels */ 41544375Skarels m->m_data += sizeof(struct tcpiphdr); 41644375Skarels m->m_len -= sizeof(struct tcpiphdr); 41744375Skarels sbappend(&so->so_rcv, m); 41844375Skarels sorwakeup(so); 41944375Skarels tp->t_flags |= TF_DELACK; 42044375Skarels return; 42144375Skarels } 42244375Skarels } 42344375Skarels 4245440Swnj /* 42544375Skarels * Drop TCP and IP headers; TCP options were dropped above. 42644375Skarels */ 42744375Skarels m->m_data += sizeof(struct tcpiphdr); 42844375Skarels m->m_len -= sizeof(struct tcpiphdr); 42944375Skarels 43044375Skarels /* 4315085Swnj * Calculate amount of space in receive window, 4325085Swnj * and then do TCP input processing. 43324816Skarels * Receive window is amount of space in rcv queue, 43424816Skarels * but not less than advertised window. 4354601Swnj */ 43625939Skarels { int win; 4374601Swnj 43825939Skarels win = sbspace(&so->so_rcv); 43925939Skarels if (win < 0) 44025939Skarels win = 0; 44137320Skarels tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 44225939Skarels } 44325939Skarels 4444601Swnj switch (tp->t_state) { 4454601Swnj 4465065Swnj /* 4475065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 4485065Swnj * If the segment contains an ACK then it is bad and send a RST. 4495065Swnj * If it does not contain a SYN then it is not interesting; drop it. 45025197Skarels * Don't bother responding if the destination was a broadcast. 4515085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 4525065Swnj * tp->iss, and send a segment: 4535085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 4545065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 4555065Swnj * Fill in remote peer address fields if not previously specified. 4565065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 4575244Sroot * segment in this state. 4585065Swnj */ 4598271Sroot case TCPS_LISTEN: { 46010145Ssam struct mbuf *am; 4618271Sroot register struct sockaddr_in *sin; 4628271Sroot 4635065Swnj if (tiflags & TH_RST) 4645065Swnj goto drop; 4655300Sroot if (tiflags & TH_ACK) 4665085Swnj goto dropwithreset; 4675300Sroot if ((tiflags & TH_SYN) == 0) 4685065Swnj goto drop; 46937320Skarels if (m->m_flags & M_BCAST) 47025197Skarels goto drop; 47144375Skarels am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 47210145Ssam if (am == NULL) 47310145Ssam goto drop; 47410145Ssam am->m_len = sizeof (struct sockaddr_in); 4758599Sroot sin = mtod(am, struct sockaddr_in *); 4768271Sroot sin->sin_family = AF_INET; 47737320Skarels sin->sin_len = sizeof(*sin); 4788271Sroot sin->sin_addr = ti->ti_src; 4798271Sroot sin->sin_port = ti->ti_sport; 4806028Sroot laddr = inp->inp_laddr; 48110145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 4826028Sroot inp->inp_laddr = ti->ti_dst; 4838599Sroot if (in_pcbconnect(inp, am)) { 4846028Sroot inp->inp_laddr = laddr; 4858716Sroot (void) m_free(am); 4865244Sroot goto drop; 4876028Sroot } 4888716Sroot (void) m_free(am); 4895244Sroot tp->t_template = tcp_template(tp); 4905244Sroot if (tp->t_template == 0) { 49126386Skarels tp = tcp_drop(tp, ENOBUFS); 49217264Skarels dropsocket = 0; /* socket is already gone */ 4935244Sroot goto drop; 4945244Sroot } 49517272Skarels if (om) { 49617272Skarels tcp_dooptions(tp, om, ti); 49717272Skarels om = 0; 49817272Skarels } 49930525Skarels if (iss) 50030525Skarels tp->iss = iss; 50130525Skarels else 50230525Skarels tp->iss = tcp_iss; 50330525Skarels tcp_iss += TCP_ISSINCR/2; 5045065Swnj tp->irs = ti->ti_seq; 5055085Swnj tcp_sendseqinit(tp); 5065085Swnj tcp_rcvseqinit(tp); 50725939Skarels tp->t_flags |= TF_ACKNOW; 5085065Swnj tp->t_state = TCPS_SYN_RECEIVED; 50933745Skarels tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 51010769Ssam dropsocket = 0; /* committed to socket */ 51130525Skarels tcpstat.tcps_accepts++; 5125085Swnj goto trimthenstep6; 5138271Sroot } 5144601Swnj 5155065Swnj /* 5165065Swnj * If the state is SYN_SENT: 5175065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 5185065Swnj * if seg contains a RST, then drop the connection. 5195065Swnj * if seg does not contain SYN, then drop it. 5205065Swnj * Otherwise this is an acceptable SYN segment 5215065Swnj * initialize tp->rcv_nxt and tp->irs 5225065Swnj * if seg contains ack then advance tp->snd_una 5235065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 5245065Swnj * arrange for segment to be acked (eventually) 5255065Swnj * continue processing rest of data/controls, beginning with URG 5265065Swnj */ 5275065Swnj case TCPS_SYN_SENT: 5285065Swnj if ((tiflags & TH_ACK) && 52924816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) || 5305231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 5315085Swnj goto dropwithreset; 5325065Swnj if (tiflags & TH_RST) { 53310394Ssam if (tiflags & TH_ACK) 53410394Ssam tp = tcp_drop(tp, ECONNREFUSED); 5355065Swnj goto drop; 5364601Swnj } 5375065Swnj if ((tiflags & TH_SYN) == 0) 5385065Swnj goto drop; 53930974Skarels if (tiflags & TH_ACK) { 54030974Skarels tp->snd_una = ti->ti_ack; 54130974Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 54230974Skarels tp->snd_nxt = tp->snd_una; 54330974Skarels } 5445244Sroot tp->t_timer[TCPT_REXMT] = 0; 5455065Swnj tp->irs = ti->ti_seq; 5465085Swnj tcp_rcvseqinit(tp); 5475085Swnj tp->t_flags |= TF_ACKNOW; 54830974Skarels if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 54930525Skarels tcpstat.tcps_connects++; 5505244Sroot soisconnected(so); 5515065Swnj tp->t_state = TCPS_ESTABLISHED; 55244375Skarels (void) tcp_reass(tp, (struct tcpiphdr *)0, 55344375Skarels (struct mbuf *)0); 55432098Skarels /* 55532098Skarels * if we didn't have to retransmit the SYN, 55632098Skarels * use its rtt as our initial srtt & rtt var. 55732098Skarels */ 55844375Skarels if (tp->t_rtt) 55944375Skarels tcp_xmit_timer(tp); 5605162Swnj } else 5615085Swnj tp->t_state = TCPS_SYN_RECEIVED; 5625085Swnj 5635085Swnj trimthenstep6: 5645085Swnj /* 5655231Swnj * Advance ti->ti_seq to correspond to first data byte. 5665085Swnj * If data, trim to stay within window, 5675085Swnj * dropping FIN if necessary. 5685085Swnj */ 5695231Swnj ti->ti_seq++; 5705085Swnj if (ti->ti_len > tp->rcv_wnd) { 5715085Swnj todrop = ti->ti_len - tp->rcv_wnd; 5725085Swnj m_adj(m, -todrop); 5735085Swnj ti->ti_len = tp->rcv_wnd; 57425939Skarels tiflags &= ~TH_FIN; 57530525Skarels tcpstat.tcps_rcvpackafterwin++; 57630525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 5775065Swnj } 5785263Swnj tp->snd_wl1 = ti->ti_seq - 1; 57925939Skarels tp->rcv_up = ti->ti_seq; 5805085Swnj goto step6; 5815065Swnj } 5824601Swnj 5835065Swnj /* 58430525Skarels * States other than LISTEN or SYN_SENT. 58530525Skarels * First check that at least some bytes of segment are within 58630525Skarels * receive window. If segment begins before rcv_nxt, 58730525Skarels * drop leading data (and SYN); if nothing left, just ack. 58816222Skarels */ 58930525Skarels todrop = tp->rcv_nxt - ti->ti_seq; 59030525Skarels if (todrop > 0) { 59130525Skarels if (tiflags & TH_SYN) { 59230525Skarels tiflags &= ~TH_SYN; 59330525Skarels ti->ti_seq++; 59430525Skarels if (ti->ti_urp > 1) 59530525Skarels ti->ti_urp--; 59630525Skarels else 59730525Skarels tiflags &= ~TH_URG; 59830525Skarels todrop--; 59930525Skarels } 60030525Skarels if (todrop > ti->ti_len || 60130525Skarels todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 60233745Skarels tcpstat.tcps_rcvduppack++; 60333745Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 60431730Skarels /* 60533745Skarels * If segment is just one to the left of the window, 60633745Skarels * check two special cases: 60733745Skarels * 1. Don't toss RST in response to 4.2-style keepalive. 60833745Skarels * 2. If the only thing to drop is a FIN, we can drop 60933745Skarels * it, but check the ACK or we will get into FIN 61033745Skarels * wars if our FINs crossed (both CLOSING). 61133745Skarels * In either case, send ACK to resynchronize, 61233745Skarels * but keep on processing for RST or ACK. 61331730Skarels */ 61433745Skarels if ((tiflags & TH_FIN && todrop == ti->ti_len + 1) 61533745Skarels #ifdef TCP_COMPAT_42 61633745Skarels || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1) 61731730Skarels #endif 61833745Skarels ) { 61933745Skarels todrop = ti->ti_len; 62033745Skarels tiflags &= ~TH_FIN; 62133745Skarels tp->t_flags |= TF_ACKNOW; 62233745Skarels } else 62333745Skarels goto dropafterack; 62432034Skarels } else { 62532034Skarels tcpstat.tcps_rcvpartduppack++; 62632034Skarels tcpstat.tcps_rcvpartdupbyte += todrop; 62730525Skarels } 62830525Skarels m_adj(m, todrop); 62930525Skarels ti->ti_seq += todrop; 63030525Skarels ti->ti_len -= todrop; 63130525Skarels if (ti->ti_urp > todrop) 63230525Skarels ti->ti_urp -= todrop; 63330525Skarels else { 63430525Skarels tiflags &= ~TH_URG; 63530525Skarels ti->ti_urp = 0; 63630525Skarels } 63716222Skarels } 63816222Skarels 63932612Skarels /* 64033745Skarels * If new data are received on a connection after the 64132612Skarels * user processes are gone, then RST the other end. 64232612Skarels */ 64332612Skarels if ((so->so_state & SS_NOFDREF) && 64432612Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 64532612Skarels tp = tcp_close(tp); 64632612Skarels tcpstat.tcps_rcvafterclose++; 64732612Skarels goto dropwithreset; 64832612Skarels } 64932612Skarels 65033445Skarels /* 65133445Skarels * If segment ends after window, drop trailing data 65233445Skarels * (and PUSH and FIN); if nothing left, just ACK. 65333445Skarels */ 65433445Skarels todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 65533445Skarels if (todrop > 0) { 65633445Skarels tcpstat.tcps_rcvpackafterwin++; 65733445Skarels if (todrop >= ti->ti_len) { 65830525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 65933445Skarels /* 66033445Skarels * If a new connection request is received 66133445Skarels * while in TIME_WAIT, drop the old connection 66233445Skarels * and start over if the sequence numbers 66333445Skarels * are above the previous ones. 66433445Skarels */ 66533445Skarels if (tiflags & TH_SYN && 66633445Skarels tp->t_state == TCPS_TIME_WAIT && 66733445Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 66833445Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 66944375Skarels tp = tcp_close(tp); 67033445Skarels goto findpcb; 67133445Skarels } 67233445Skarels /* 67333445Skarels * If window is closed can only take segments at 67433445Skarels * window edge, and have to drop data and PUSH from 67533445Skarels * incoming segments. Continue processing, but 67633445Skarels * remember to ack. Otherwise, drop segment 67733445Skarels * and ack. 67833445Skarels */ 67933445Skarels if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 68033445Skarels tp->t_flags |= TF_ACKNOW; 68130525Skarels tcpstat.tcps_rcvwinprobe++; 68233445Skarels } else 6835065Swnj goto dropafterack; 68433445Skarels } else 68530525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 68633445Skarels m_adj(m, -todrop); 68733445Skarels ti->ti_len -= todrop; 68833445Skarels tiflags &= ~(TH_PUSH|TH_FIN); 6895065Swnj } 6904601Swnj 6915065Swnj /* 6925065Swnj * If the RST bit is set examine the state: 6935065Swnj * SYN_RECEIVED STATE: 6945065Swnj * If passive open, return to LISTEN state. 6955065Swnj * If active open, inform user that connection was refused. 6965065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 6975065Swnj * Inform user that connection was reset, and close tcb. 6985065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 6995065Swnj * Close the tcb. 7005065Swnj */ 7015065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 7025267Sroot 7035065Swnj case TCPS_SYN_RECEIVED: 70433745Skarels so->so_error = ECONNREFUSED; 70533745Skarels goto close; 7064601Swnj 7075065Swnj case TCPS_ESTABLISHED: 7085065Swnj case TCPS_FIN_WAIT_1: 7095065Swnj case TCPS_FIN_WAIT_2: 7105065Swnj case TCPS_CLOSE_WAIT: 71133745Skarels so->so_error = ECONNRESET; 71233745Skarels close: 71333745Skarels tp->t_state = TCPS_CLOSED; 71433745Skarels tcpstat.tcps_drops++; 71533745Skarels tp = tcp_close(tp); 7165065Swnj goto drop; 7175065Swnj 7185065Swnj case TCPS_CLOSING: 7195065Swnj case TCPS_LAST_ACK: 7205065Swnj case TCPS_TIME_WAIT: 72110394Ssam tp = tcp_close(tp); 7225065Swnj goto drop; 7234601Swnj } 7244601Swnj 7254601Swnj /* 7265065Swnj * If a SYN is in the window, then this is an 7275065Swnj * error and we send an RST and drop the connection. 7284601Swnj */ 7295065Swnj if (tiflags & TH_SYN) { 73010394Ssam tp = tcp_drop(tp, ECONNRESET); 7315085Swnj goto dropwithreset; 7324601Swnj } 7334601Swnj 7344601Swnj /* 7355065Swnj * If the ACK bit is off we drop the segment and return. 7364601Swnj */ 7375085Swnj if ((tiflags & TH_ACK) == 0) 7385065Swnj goto drop; 7395065Swnj 7405065Swnj /* 7415065Swnj * Ack processing. 7425065Swnj */ 7434601Swnj switch (tp->t_state) { 7444601Swnj 7455065Swnj /* 7465065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 74731721Skarels * ESTABLISHED state and continue processing, otherwise 7485065Swnj * send an RST. 7495065Swnj */ 7505065Swnj case TCPS_SYN_RECEIVED: 7515085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 7525231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 7535085Swnj goto dropwithreset; 75430525Skarels tcpstat.tcps_connects++; 7555085Swnj soisconnected(so); 7565085Swnj tp->t_state = TCPS_ESTABLISHED; 75744375Skarels (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0); 7585244Sroot tp->snd_wl1 = ti->ti_seq - 1; 7595085Swnj /* fall into ... */ 7604601Swnj 7615065Swnj /* 7625065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 7635065Swnj * ACKs. If the ack is in the range 7645231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 7655065Swnj * then advance tp->snd_una to ti->ti_ack and drop 7665065Swnj * data from the retransmission queue. If this ACK reflects 7675065Swnj * more up to date window information we update our window information. 7685065Swnj */ 7695065Swnj case TCPS_ESTABLISHED: 7705065Swnj case TCPS_FIN_WAIT_1: 7715065Swnj case TCPS_FIN_WAIT_2: 7725065Swnj case TCPS_CLOSE_WAIT: 7735065Swnj case TCPS_CLOSING: 7745244Sroot case TCPS_LAST_ACK: 7755244Sroot case TCPS_TIME_WAIT: 7765085Swnj 77730525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 77832098Skarels if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) { 77930525Skarels tcpstat.tcps_rcvdupack++; 78032098Skarels /* 78144375Skarels * If we have outstanding data (other than 78244375Skarels * a window probe), this is a completely 78332098Skarels * duplicate ack (ie, window info didn't 78432098Skarels * change), the ack is the biggest we've 78532098Skarels * seen and we've seen exactly our rexmt 78632098Skarels * threshhold of them, assume a packet 78732098Skarels * has been dropped and retransmit it. 78832098Skarels * Kludge snd_nxt & the congestion 78932098Skarels * window so we send only this one 79044375Skarels * packet. 79144375Skarels * 79244375Skarels * We know we're losing at the current 79344375Skarels * window size so do congestion avoidance 79444375Skarels * (set ssthresh to half the current window 79544375Skarels * and pull our congestion window back to 79644375Skarels * the new ssthresh). 79744375Skarels * 79844375Skarels * Dup acks mean that packets have left the 79944375Skarels * network (they're now cached at the receiver) 80044375Skarels * so bump cwnd by the amount in the receiver 80144375Skarels * to keep a constant cwnd packets in the 80244375Skarels * network. 80332098Skarels */ 80432098Skarels if (tp->t_timer[TCPT_REXMT] == 0 || 80532098Skarels ti->ti_ack != tp->snd_una) 80632098Skarels tp->t_dupacks = 0; 80732098Skarels else if (++tp->t_dupacks == tcprexmtthresh) { 80832098Skarels tcp_seq onxt = tp->snd_nxt; 80932376Skarels u_int win = 81037320Skarels min(tp->snd_wnd, tp->snd_cwnd) / 2 / 81132376Skarels tp->t_maxseg; 81232098Skarels 81332376Skarels if (win < 2) 81432376Skarels win = 2; 81532376Skarels tp->snd_ssthresh = win * tp->t_maxseg; 81632098Skarels tp->t_timer[TCPT_REXMT] = 0; 81732098Skarels tp->t_rtt = 0; 81832098Skarels tp->snd_nxt = ti->ti_ack; 81932098Skarels tp->snd_cwnd = tp->t_maxseg; 82032098Skarels (void) tcp_output(tp); 82144375Skarels tp->snd_cwnd = tp->snd_ssthresh + 82244375Skarels tp->t_maxseg * tp->t_dupacks; 82332098Skarels if (SEQ_GT(onxt, tp->snd_nxt)) 82432098Skarels tp->snd_nxt = onxt; 82532098Skarels goto drop; 82644375Skarels } else if (tp->t_dupacks > tcprexmtthresh) { 82744375Skarels tp->snd_cwnd += tp->t_maxseg; 82844375Skarels (void) tcp_output(tp); 82944375Skarels goto drop; 83032098Skarels } 83132098Skarels } else 83232098Skarels tp->t_dupacks = 0; 8335065Swnj break; 83430525Skarels } 83544375Skarels /* 83644375Skarels * If the congestion window was inflated to account 83744375Skarels * for the other side's cached packets, retract it. 83844375Skarels */ 83944375Skarels if (tp->t_dupacks > tcprexmtthresh && 84044375Skarels tp->snd_cwnd > tp->snd_ssthresh) 84144375Skarels tp->snd_cwnd = tp->snd_ssthresh; 84232098Skarels tp->t_dupacks = 0; 84330525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 84430525Skarels tcpstat.tcps_rcvacktoomuch++; 8455065Swnj goto dropafterack; 84630525Skarels } 8475085Swnj acked = ti->ti_ack - tp->snd_una; 84830525Skarels tcpstat.tcps_rcvackpack++; 84930525Skarels tcpstat.tcps_rcvackbyte += acked; 8505951Swnj 8515951Swnj /* 8525951Swnj * If transmit timer is running and timed sequence 8535951Swnj * number was acked, update smoothed round trip time. 85432034Skarels * Since we now have an rtt measurement, cancel the 85532034Skarels * timer backoff (cf., Phil Karn's retransmit alg.). 85632034Skarels * Recompute the initial retransmit timer. 8575951Swnj */ 85844375Skarels if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 85944375Skarels tcp_xmit_timer(tp); 86031726Skarels 86126824Skarels /* 86226824Skarels * If all outstanding data is acked, stop retransmit 86326824Skarels * timer and remember to restart (more output or persist). 86426824Skarels * If there is more data to be acked, restart retransmit 86532034Skarels * timer, using current (possibly backed-off) value. 86626824Skarels */ 86726824Skarels if (ti->ti_ack == tp->snd_max) { 8685244Sroot tp->t_timer[TCPT_REXMT] = 0; 86926824Skarels needoutput = 1; 87032034Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) 87132034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 87217360Skarels /* 87332098Skarels * When new data is acked, open the congestion window. 87432098Skarels * If the window gives us less than ssthresh packets 87532098Skarels * in flight, open exponentially (maxseg per packet). 87644375Skarels * Otherwise open linearly: maxseg per window 87744375Skarels * (maxseg^2 / cwnd per packet), plus a constant 87844375Skarels * fraction of a packet (maxseg/8) to help larger windows 87944375Skarels * open quickly enough. 88017360Skarels */ 88132098Skarels { 88244375Skarels register u_int cw = tp->snd_cwnd; 88344375Skarels register u_int incr = tp->t_maxseg; 88432098Skarels 88544375Skarels if (cw > tp->snd_ssthresh) 88644375Skarels incr = incr * incr / cw + incr / 8; 88744375Skarels tp->snd_cwnd = min(cw + incr, TCP_MAXWIN); 88832098Skarels } 8895307Sroot if (acked > so->so_snd.sb_cc) { 89015386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 89126386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 89231721Skarels ourfinisacked = 1; 8935307Sroot } else { 8946161Ssam sbdrop(&so->so_snd, acked); 8955307Sroot tp->snd_wnd -= acked; 89631721Skarels ourfinisacked = 0; 8975307Sroot } 89844375Skarels if (so->so_snd.sb_flags & SB_NOTIFY) 89944375Skarels sowwakeup(so); 9005231Swnj tp->snd_una = ti->ti_ack; 9015357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 9025357Sroot tp->snd_nxt = tp->snd_una; 9035162Swnj 9044601Swnj switch (tp->t_state) { 9054601Swnj 9065065Swnj /* 9075065Swnj * In FIN_WAIT_1 STATE in addition to the processing 9085065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 9095085Swnj * then enter FIN_WAIT_2. 9105065Swnj */ 9115065Swnj case TCPS_FIN_WAIT_1: 9125896Swnj if (ourfinisacked) { 9135896Swnj /* 9145896Swnj * If we can't receive any more 9155896Swnj * data, then closing user can proceed. 91624816Skarels * Starting the timer is contrary to the 91724816Skarels * specification, but if we don't get a FIN 91824816Skarels * we'll hang forever. 9195896Swnj */ 92024816Skarels if (so->so_state & SS_CANTRCVMORE) { 9215896Swnj soisdisconnected(so); 92233745Skarels tp->t_timer[TCPT_2MSL] = tcp_maxidle; 92324816Skarels } 9245085Swnj tp->t_state = TCPS_FIN_WAIT_2; 9255896Swnj } 9264601Swnj break; 9274601Swnj 9285065Swnj /* 9295065Swnj * In CLOSING STATE in addition to the processing for 9305065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 9315065Swnj * then enter the TIME-WAIT state, otherwise ignore 9325065Swnj * the segment. 9335065Swnj */ 9345065Swnj case TCPS_CLOSING: 9355244Sroot if (ourfinisacked) { 9365065Swnj tp->t_state = TCPS_TIME_WAIT; 9375244Sroot tcp_canceltimers(tp); 9385244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9395244Sroot soisdisconnected(so); 9405244Sroot } 9415244Sroot break; 9424601Swnj 9435065Swnj /* 94431743Skarels * In LAST_ACK, we may still be waiting for data to drain 94531743Skarels * and/or to be acked, as well as for the ack of our FIN. 94631743Skarels * If our FIN is now acknowledged, delete the TCB, 94731743Skarels * enter the closed state and return. 9485065Swnj */ 9495065Swnj case TCPS_LAST_ACK: 95031743Skarels if (ourfinisacked) { 95110394Ssam tp = tcp_close(tp); 95231743Skarels goto drop; 95331743Skarels } 95431743Skarels break; 9554601Swnj 9565065Swnj /* 9575065Swnj * In TIME_WAIT state the only thing that should arrive 9585065Swnj * is a retransmission of the remote FIN. Acknowledge 9595065Swnj * it and restart the finack timer. 9605065Swnj */ 9615065Swnj case TCPS_TIME_WAIT: 9625162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9635065Swnj goto dropafterack; 9644601Swnj } 9655085Swnj } 9664601Swnj 9675065Swnj step6: 9685065Swnj /* 9695244Sroot * Update window information. 97025939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 9715244Sroot */ 97225939Skarels if ((tiflags & TH_ACK) && 97325939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 9745391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 97525939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 97630525Skarels /* keep track of pure window updates */ 97730525Skarels if (ti->ti_len == 0 && 97832098Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) 97930525Skarels tcpstat.tcps_rcvwinupd++; 9805244Sroot tp->snd_wnd = ti->ti_win; 9815244Sroot tp->snd_wl1 = ti->ti_seq; 9825244Sroot tp->snd_wl2 = ti->ti_ack; 98325260Skarels if (tp->snd_wnd > tp->max_sndwnd) 98425260Skarels tp->max_sndwnd = tp->snd_wnd; 98526824Skarels needoutput = 1; 98626824Skarels } 9875244Sroot 9885244Sroot /* 9895547Swnj * Process segments with URG. 9905065Swnj */ 9917267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 9927267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 9935547Swnj /* 99425939Skarels * This is a kludge, but if we receive and accept 99513121Ssam * random urgent pointers, we'll crash in 99613121Ssam * soreceive. It's hard to imagine someone 99713121Ssam * actually wanting to send this much urgent data. 99812441Ssam */ 99925652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 100012441Ssam ti->ti_urp = 0; /* XXX */ 100112441Ssam tiflags &= ~TH_URG; /* XXX */ 100225939Skarels goto dodata; /* XXX */ 100312441Ssam } 100412441Ssam /* 10055547Swnj * If this segment advances the known urgent pointer, 10065547Swnj * then mark the data stream. This should not happen 10075547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 10085547Swnj * a FIN has been received from the remote side. 10095547Swnj * In these states we ignore the URG. 101027190Skarels * 101127190Skarels * According to RFC961 (Assigned Protocols), 101227190Skarels * the urgent pointer points to the last octet 101327190Skarels * of urgent data. We continue, however, 101427190Skarels * to consider it to indicate the first octet 101544375Skarels * of data past the urgent section as the original 101644375Skarels * spec states (in one of two places). 10175547Swnj */ 10185547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 10195547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 10205547Swnj so->so_oobmark = so->so_rcv.sb_cc + 10215547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 10225547Swnj if (so->so_oobmark == 0) 10235547Swnj so->so_state |= SS_RCVATMARK; 10248313Sroot sohasoutofband(so); 102524816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 10265440Swnj } 10275547Swnj /* 10285547Swnj * Remove out of band data so doesn't get presented to user. 10295547Swnj * This can happen independent of advancing the URG pointer, 10305547Swnj * but if two URG's are pending at once, some out-of-band 10315547Swnj * data may creep in... ick. 10325547Swnj */ 103344375Skarels if (ti->ti_urp <= ti->ti_len 103444375Skarels #ifdef SO_OOBINLINE 103544375Skarels && (so->so_options & SO_OOBINLINE) == 0 103644375Skarels #endif 103744375Skarels ) 103844375Skarels tcp_pulloutofband(so, ti, m); 103925939Skarels } else 104025939Skarels /* 104125939Skarels * If no out of band data is expected, 104225939Skarels * pull receive urgent pointer along 104325939Skarels * with the receive window. 104425939Skarels */ 104525939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 104625939Skarels tp->rcv_up = tp->rcv_nxt; 104725939Skarels dodata: /* XXX */ 10484601Swnj 10494601Swnj /* 10505065Swnj * Process the segment text, merging it into the TCP sequencing queue, 10515065Swnj * and arranging for acknowledgment of receipt if necessary. 10525065Swnj * This process logically involves adjusting tp->rcv_wnd as data 10535065Swnj * is presented to the user (this happens in tcp_usrreq.c, 10545065Swnj * case PRU_RCVD). If a FIN has already been received on this 10555065Swnj * connection then we just ignore the text. 10564601Swnj */ 105717946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 105817946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 105924816Skarels TCP_REASS(tp, ti, m, so, tiflags); 106025260Skarels /* 106125260Skarels * Note the amount of data that peer has sent into 106225260Skarels * our window, in order to estimate the sender's 106325260Skarels * buffer size. 106425260Skarels */ 106532098Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 10665244Sroot } else { 10674924Swnj m_freem(m); 10685263Swnj tiflags &= ~TH_FIN; 10695244Sroot } 10704601Swnj 10714601Swnj /* 10725263Swnj * If FIN is received ACK the FIN and let the user know 10735263Swnj * that the connection is closing. 10744601Swnj */ 10755263Swnj if (tiflags & TH_FIN) { 10765244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 10775244Sroot socantrcvmore(so); 10785244Sroot tp->t_flags |= TF_ACKNOW; 10795244Sroot tp->rcv_nxt++; 10805244Sroot } 10815065Swnj switch (tp->t_state) { 10824601Swnj 10835065Swnj /* 10845065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 10855065Swnj * enter the CLOSE_WAIT state. 10864884Swnj */ 10875065Swnj case TCPS_SYN_RECEIVED: 10885065Swnj case TCPS_ESTABLISHED: 10895065Swnj tp->t_state = TCPS_CLOSE_WAIT; 10905065Swnj break; 10914884Swnj 10925065Swnj /* 10935085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 10945085Swnj * enter the CLOSING state. 10954884Swnj */ 10965065Swnj case TCPS_FIN_WAIT_1: 10975085Swnj tp->t_state = TCPS_CLOSING; 10985065Swnj break; 10994601Swnj 11005065Swnj /* 11015065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 11025065Swnj * starting the time-wait timer, turning off the other 11035065Swnj * standard timers. 11045065Swnj */ 11055065Swnj case TCPS_FIN_WAIT_2: 11065244Sroot tp->t_state = TCPS_TIME_WAIT; 11075074Swnj tcp_canceltimers(tp); 11085162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 11095244Sroot soisdisconnected(so); 11105065Swnj break; 11115065Swnj 11124884Swnj /* 11135065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 11144884Swnj */ 11155065Swnj case TCPS_TIME_WAIT: 11165162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 11175065Swnj break; 11185085Swnj } 11194601Swnj } 11205267Sroot if (so->so_options & SO_DEBUG) 11215267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 11225085Swnj 11235085Swnj /* 11245085Swnj * Return any desired output. 11255085Swnj */ 112626824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 112725939Skarels (void) tcp_output(tp); 11285065Swnj return; 11295085Swnj 11305065Swnj dropafterack: 11315085Swnj /* 11326211Swnj * Generate an ACK dropping incoming segment if it occupies 11336211Swnj * sequence space, where the ACK reflects our state. 11345085Swnj */ 113526057Skarels if (tiflags & TH_RST) 11365085Swnj goto drop; 113731749Skarels m_freem(m); 113831721Skarels tp->t_flags |= TF_ACKNOW; 113931721Skarels (void) tcp_output(tp); 11405231Swnj return; 11415085Swnj 11425085Swnj dropwithreset: 114311731Ssam if (om) { 11446161Ssam (void) m_free(om); 114511731Ssam om = 0; 114611731Ssam } 11475085Swnj /* 11485244Sroot * Generate a RST, dropping incoming segment. 11495085Swnj * Make ACK acceptable to originator of segment. 115025197Skarels * Don't bother to respond if destination was broadcast. 11515085Swnj */ 115237320Skarels if ((tiflags & TH_RST) || m->m_flags & M_BCAST) 11535085Swnj goto drop; 11545085Swnj if (tiflags & TH_ACK) 115537320Skarels tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 11565085Swnj else { 11575085Swnj if (tiflags & TH_SYN) 11585085Swnj ti->ti_len++; 115937320Skarels tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 11606211Swnj TH_RST|TH_ACK); 11615085Swnj } 116210769Ssam /* destroy temporarily created socket */ 116310769Ssam if (dropsocket) 116410769Ssam (void) soabort(so); 11655231Swnj return; 11665085Swnj 11675065Swnj drop: 116811730Ssam if (om) 116911730Ssam (void) m_free(om); 11705085Swnj /* 11715085Swnj * Drop space held by incoming segment and return. 11725085Swnj */ 11736303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 11746303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 11755065Swnj m_freem(m); 117610769Ssam /* destroy temporarily created socket */ 117710769Ssam if (dropsocket) 117810769Ssam (void) soabort(so); 11795267Sroot return; 11805065Swnj } 11815065Swnj 118217272Skarels tcp_dooptions(tp, om, ti) 11835440Swnj struct tcpcb *tp; 11845440Swnj struct mbuf *om; 118517272Skarels struct tcpiphdr *ti; 11865419Swnj { 11875440Swnj register u_char *cp; 118844375Skarels u_short mss; 11895440Swnj int opt, optlen, cnt; 11905419Swnj 11915440Swnj cp = mtod(om, u_char *); 11925440Swnj cnt = om->m_len; 11935440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 11945440Swnj opt = cp[0]; 11955440Swnj if (opt == TCPOPT_EOL) 11965440Swnj break; 11975440Swnj if (opt == TCPOPT_NOP) 11985440Swnj optlen = 1; 119912169Ssam else { 12005440Swnj optlen = cp[1]; 120112169Ssam if (optlen <= 0) 120212169Ssam break; 120312169Ssam } 12045440Swnj switch (opt) { 12055440Swnj 12065440Swnj default: 120744375Skarels continue; 12085440Swnj 12095440Swnj case TCPOPT_MAXSEG: 12105440Swnj if (optlen != 4) 12115440Swnj continue; 121217272Skarels if (!(ti->ti_flags & TH_SYN)) 121317272Skarels continue; 121444375Skarels bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 121544375Skarels NTOHS(mss); 121644375Skarels (void) tcp_mss(tp, mss); /* sets t_maxseg */ 12175440Swnj break; 12185419Swnj } 12195419Swnj } 12206161Ssam (void) m_free(om); 12215419Swnj } 12225419Swnj 12235419Swnj /* 12245547Swnj * Pull out of band byte out of a segment so 12255547Swnj * it doesn't appear in the user's data queue. 12265547Swnj * It is still reflected in the segment length for 12275547Swnj * sequencing purposes. 12285547Swnj */ 122944375Skarels tcp_pulloutofband(so, ti, m) 12305547Swnj struct socket *so; 12315547Swnj struct tcpiphdr *ti; 123244375Skarels register struct mbuf *m; 12335547Swnj { 12346116Swnj int cnt = ti->ti_urp - 1; 12355547Swnj 12365547Swnj while (cnt >= 0) { 12375547Swnj if (m->m_len > cnt) { 12385547Swnj char *cp = mtod(m, caddr_t) + cnt; 12395547Swnj struct tcpcb *tp = sototcpcb(so); 12405547Swnj 12415547Swnj tp->t_iobc = *cp; 12425547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 12436161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 12445547Swnj m->m_len--; 12455547Swnj return; 12465547Swnj } 12475547Swnj cnt -= m->m_len; 12485547Swnj m = m->m_next; 12495547Swnj if (m == 0) 12505547Swnj break; 12515547Swnj } 12525547Swnj panic("tcp_pulloutofband"); 12535547Swnj } 12545547Swnj 12555547Swnj /* 125644375Skarels * Collect new round-trip time estimate 125744375Skarels * and update averages and current timeout. 125817272Skarels */ 125944375Skarels tcp_xmit_timer(tp) 126023975Skarels register struct tcpcb *tp; 126117272Skarels { 126244375Skarels register short delta; 126344375Skarels 126444375Skarels tcpstat.tcps_rttupdated++; 126544375Skarels if (tp->t_srtt != 0) { 126644375Skarels /* 126744375Skarels * srtt is stored as fixed point with 3 bits after the 126844375Skarels * binary point (i.e., scaled by 8). The following magic 126944375Skarels * is equivalent to the smoothing algorithm in rfc793 with 127044375Skarels * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 127144375Skarels * point). Adjust t_rtt to origin 0. 127244375Skarels */ 127344375Skarels delta = tp->t_rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT); 127444375Skarels if ((tp->t_srtt += delta) <= 0) 127544375Skarels tp->t_srtt = 1; 127644375Skarels /* 127744375Skarels * We accumulate a smoothed rtt variance (actually, a 127844375Skarels * smoothed mean difference), then set the retransmit 127944375Skarels * timer to smoothed rtt + 4 times the smoothed variance. 128044375Skarels * rttvar is stored as fixed point with 2 bits after the 128144375Skarels * binary point (scaled by 4). The following is 128244375Skarels * equivalent to rfc793 smoothing with an alpha of .75 128344375Skarels * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 128444375Skarels * rfc793's wired-in beta. 128544375Skarels */ 128644375Skarels if (delta < 0) 128744375Skarels delta = -delta; 128844375Skarels delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 128944375Skarels if ((tp->t_rttvar += delta) <= 0) 129044375Skarels tp->t_rttvar = 1; 129144375Skarels } else { 129244375Skarels /* 129344375Skarels * No rtt measurement yet - use the unsmoothed rtt. 129444375Skarels * Set the variance to half the rtt (so our first 1295*52199Skarels * retransmit happens at 3*rtt). 129644375Skarels */ 129744375Skarels tp->t_srtt = tp->t_rtt << TCP_RTT_SHIFT; 129844375Skarels tp->t_rttvar = tp->t_rtt << (TCP_RTTVAR_SHIFT - 1); 129944375Skarels } 130044375Skarels tp->t_rtt = 0; 130144375Skarels tp->t_rxtshift = 0; 130244375Skarels 130344375Skarels /* 130444375Skarels * the retransmit should happen at rtt + 4 * rttvar. 130544375Skarels * Because of the way we do the smoothing, srtt and rttvar 130644375Skarels * will each average +1/2 tick of bias. When we compute 130744375Skarels * the retransmit timer, we want 1/2 tick of rounding and 130844375Skarels * 1 extra tick because of +-1/2 tick uncertainty in the 130944375Skarels * firing of the timer. The bias will give us exactly the 131044375Skarels * 1.5 tick we need. But, because the bias is 131144375Skarels * statistical, we have to test that we don't drop below 131244375Skarels * the minimum feasible timer (which is 2 ticks). 131344375Skarels */ 131444375Skarels TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 131544375Skarels tp->t_rttmin, TCPTV_REXMTMAX); 131644375Skarels 131744375Skarels /* 131844375Skarels * We received an ack for a packet that wasn't retransmitted; 131944375Skarels * it is probably safe to discard any error indications we've 132044375Skarels * received recently. This isn't quite right, but close enough 132144375Skarels * for now (a route might have failed after we sent a segment, 132244375Skarels * and the return path might not be symmetrical). 132344375Skarels */ 132444375Skarels tp->t_softerror = 0; 132544375Skarels } 132644375Skarels 132744375Skarels /* 132844375Skarels * Determine a reasonable value for maxseg size. 132944375Skarels * If the route is known, check route for mtu. 133044375Skarels * If none, use an mss that can be handled on the outgoing 133144375Skarels * interface without forcing IP to fragment; if bigger than 133244375Skarels * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 133344375Skarels * to utilize large mbufs. If no route is found, route has no mtu, 133444375Skarels * or the destination isn't local, use a default, hopefully conservative 133544375Skarels * size (usually 512 or the default IP max size, but no more than the mtu 133644375Skarels * of the interface), as we can't discover anything about intervening 133744375Skarels * gateways or networks. We also initialize the congestion/slow start 133844375Skarels * window to be a single segment if the destination isn't local. 133944375Skarels * While looking at the routing entry, we also initialize other path-dependent 134044375Skarels * parameters from pre-set or cached values in the routing entry. 134144375Skarels */ 134244375Skarels 134344375Skarels tcp_mss(tp, offer) 134444375Skarels register struct tcpcb *tp; 134544375Skarels u_short offer; 134644375Skarels { 134717272Skarels struct route *ro; 134844375Skarels register struct rtentry *rt; 134917272Skarels struct ifnet *ifp; 135044375Skarels register int rtt, mss; 135144375Skarels u_long bufsize; 135217272Skarels struct inpcb *inp; 135344375Skarels struct socket *so; 135444375Skarels extern int tcp_mssdflt, tcp_rttdflt; 135517272Skarels 135617272Skarels inp = tp->t_inpcb; 135717272Skarels ro = &inp->inp_route; 135844375Skarels 135944375Skarels if ((rt = ro->ro_rt) == (struct rtentry *)0) { 136017272Skarels /* No route yet, so try to acquire one */ 136117272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 136217272Skarels ro->ro_dst.sa_family = AF_INET; 136337320Skarels ro->ro_dst.sa_len = sizeof(ro->ro_dst); 136417272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 136517272Skarels inp->inp_faddr; 136617272Skarels rtalloc(ro); 136717272Skarels } 136844375Skarels if ((rt = ro->ro_rt) == (struct rtentry *)0) 136944375Skarels return (tcp_mssdflt); 137017272Skarels } 137144375Skarels ifp = rt->rt_ifp; 137244375Skarels so = inp->inp_socket; 137317272Skarels 137444375Skarels #ifdef RTV_MTU /* if route characteristics exist ... */ 137544375Skarels /* 137644375Skarels * While we're here, check if there's an initial rtt 137744375Skarels * or rttvar. Convert from the route-table units 137844375Skarels * to scaled multiples of the slow timeout timer. 137944375Skarels */ 138044375Skarels if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 1381*52199Skarels /* 1382*52199Skarels * XXX the lock bit for MTU indicates that the value 1383*52199Skarels * is also a minimum value; this is subject to time. 1384*52199Skarels */ 1385*52199Skarels if (rt->rt_rmx.rmx_locks & RTV_RTT) 138644375Skarels tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 138744375Skarels tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 138844375Skarels if (rt->rt_rmx.rmx_rttvar) 138944375Skarels tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 139044375Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 139144375Skarels else 139244375Skarels /* default variation is +- 1 rtt */ 139344375Skarels tp->t_rttvar = 139444375Skarels tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 139544375Skarels TCPT_RANGESET(tp->t_rxtcur, 139644375Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 139744375Skarels tp->t_rttmin, TCPTV_REXMTMAX); 139844375Skarels } 139944375Skarels /* 140044375Skarels * if there's an mtu associated with the route, use it 140144375Skarels */ 140244375Skarels if (rt->rt_rmx.rmx_mtu) 140344375Skarels mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 140444375Skarels else 140544375Skarels #endif /* RTV_MTU */ 140644375Skarels { 140744375Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 140831726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 140944375Skarels if (mss > MCLBYTES) 141044375Skarels mss &= ~(MCLBYTES-1); 141117272Skarels #else 141244375Skarels if (mss > MCLBYTES) 141344375Skarels mss = mss / MCLBYTES * MCLBYTES; 141417272Skarels #endif 141544375Skarels if (!in_localaddr(inp->inp_faddr)) 141644375Skarels mss = min(mss, tcp_mssdflt); 141744375Skarels } 141844375Skarels /* 141944375Skarels * The current mss, t_maxseg, is initialized to the default value. 142044375Skarels * If we compute a smaller value, reduce the current mss. 142144375Skarels * If we compute a larger value, return it for use in sending 142244375Skarels * a max seg size option, but don't store it for use 142344375Skarels * unless we received an offer at least that large from peer. 142444375Skarels * However, do not accept offers under 32 bytes. 142544375Skarels */ 142644375Skarels if (offer) 142744375Skarels mss = min(mss, offer); 142844375Skarels mss = max(mss, 32); /* sanity */ 142944375Skarels if (mss < tp->t_maxseg || offer != 0) { 143044375Skarels /* 143144375Skarels * If there's a pipesize, change the socket buffer 143244375Skarels * to that size. Make the socket buffers an integral 143344375Skarels * number of mss units; if the mss is larger than 143444375Skarels * the socket buffer, decrease the mss. 143544375Skarels */ 143644375Skarels #ifdef RTV_SPIPE 143744375Skarels if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 143844375Skarels #endif 143944375Skarels bufsize = so->so_snd.sb_hiwat; 144044375Skarels if (bufsize < mss) 144144375Skarels mss = bufsize; 144244375Skarels else { 144344375Skarels bufsize = min(bufsize, SB_MAX) / mss * mss; 144444375Skarels (void) sbreserve(&so->so_snd, bufsize); 144544375Skarels } 144644375Skarels tp->t_maxseg = mss; 144732098Skarels 144844375Skarels #ifdef RTV_RPIPE 144944375Skarels if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 145044375Skarels #endif 145144375Skarels bufsize = so->so_rcv.sb_hiwat; 145244375Skarels if (bufsize > mss) { 145344375Skarels bufsize = min(bufsize, SB_MAX) / mss * mss; 145444375Skarels (void) sbreserve(&so->so_rcv, bufsize); 145544375Skarels } 145644375Skarels } 145732034Skarels tp->snd_cwnd = mss; 145844375Skarels 145944375Skarels #ifdef RTV_SSTHRESH 146044375Skarels if (rt->rt_rmx.rmx_ssthresh) { 146144375Skarels /* 146244375Skarels * There's some sort of gateway or interface 146344375Skarels * buffer limit on the path. Use this to set 146444375Skarels * the slow start threshhold, but set the 146544375Skarels * threshold to no less than 2*mss. 146644375Skarels */ 146744375Skarels tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 146844375Skarels } 146944375Skarels #endif /* RTV_MTU */ 147032034Skarels return (mss); 147117272Skarels } 1472