123190Smckusick /* 2*66740Sbostic * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994 363218Sbostic * The Regents of the University of California. All rights reserved. 423190Smckusick * 544485Sbostic * %sccs.include.redist.c% 632787Sbostic * 7*66740Sbostic * @(#)tcp_input.c 8.5 (Berkeley) 04/10/94 823190Smckusick */ 94601Swnj 1057947Ssklower #ifndef TUBA_INCLUDE 1156531Sbostic #include <sys/param.h> 1256531Sbostic #include <sys/systm.h> 1356531Sbostic #include <sys/malloc.h> 1456531Sbostic #include <sys/mbuf.h> 1556531Sbostic #include <sys/protosw.h> 1656531Sbostic #include <sys/socket.h> 1756531Sbostic #include <sys/socketvar.h> 1856531Sbostic #include <sys/errno.h> 1910894Ssam 2056531Sbostic #include <net/if.h> 2156531Sbostic #include <net/route.h> 2210894Ssam 2356531Sbostic #include <netinet/in.h> 2456531Sbostic #include <netinet/in_systm.h> 2556531Sbostic #include <netinet/ip.h> 2656531Sbostic #include <netinet/in_pcb.h> 2756531Sbostic #include <netinet/ip_var.h> 2856531Sbostic #include <netinet/tcp.h> 2956531Sbostic #include <netinet/tcp_fsm.h> 3056531Sbostic #include <netinet/tcp_seq.h> 3156531Sbostic #include <netinet/tcp_timer.h> 3256531Sbostic #include <netinet/tcp_var.h> 3356531Sbostic #include <netinet/tcpip.h> 3456531Sbostic #include <netinet/tcp_debug.h> 354601Swnj 3632098Skarels int tcprexmtthresh = 3; 375267Sroot struct tcpiphdr tcp_saveti; 3844375Skarels struct inpcb *tcp_last_inpcb = &tcb; 394601Swnj 4057947Ssklower extern u_long sb_max; 4157947Ssklower 4257947Ssklower #endif /* TUBA_INCLUDE */ 4357433Sandrew #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ) 4457433Sandrew 4557433Sandrew /* for modulo comparisons of timestamps */ 4657433Sandrew #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0) 4757433Sandrew #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0) 4857433Sandrew 4924816Skarels 505065Swnj /* 5124816Skarels * Insert segment ti into reassembly queue of tcp with 5224816Skarels * control block tp. Return TH_FIN if reassembly now includes 5324816Skarels * a segment with FIN. The macro form does the common case inline 5424816Skarels * (segment is the next to be received on an established connection, 5524816Skarels * and the queue is empty), avoiding linkage into and removal 5624816Skarels * from the queue and repetition of various conversions. 5734278Skarels * Set DELACK for segments received in order, but ack immediately 5834278Skarels * when segments are out of order (so fast retransmit can work). 5924816Skarels */ 6024816Skarels #define TCP_REASS(tp, ti, m, so, flags) { \ 6124816Skarels if ((ti)->ti_seq == (tp)->rcv_nxt && \ 6224816Skarels (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 6324816Skarels (tp)->t_state == TCPS_ESTABLISHED) { \ 6434278Skarels tp->t_flags |= TF_DELACK; \ 6524816Skarels (tp)->rcv_nxt += (ti)->ti_len; \ 6624816Skarels flags = (ti)->ti_flags & TH_FIN; \ 6730525Skarels tcpstat.tcps_rcvpack++;\ 6830525Skarels tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 6924816Skarels sbappend(&(so)->so_rcv, (m)); \ 7024816Skarels sorwakeup(so); \ 7134278Skarels } else { \ 7244375Skarels (flags) = tcp_reass((tp), (ti), (m)); \ 7334278Skarels tp->t_flags |= TF_ACKNOW; \ 7434278Skarels } \ 7524816Skarels } 7657947Ssklower #ifndef TUBA_INCLUDE 7724816Skarels 7861335Sbostic int 7944375Skarels tcp_reass(tp, ti, m) 8024816Skarels register struct tcpcb *tp; 8124816Skarels register struct tcpiphdr *ti; 8244375Skarels struct mbuf *m; 8324816Skarels { 8424816Skarels register struct tcpiphdr *q; 8524816Skarels struct socket *so = tp->t_inpcb->inp_socket; 8624816Skarels int flags; 8724816Skarels 8824816Skarels /* 8924816Skarels * Call with ti==0 after become established to 9024816Skarels * force pre-ESTABLISHED data up to user socket. 9124816Skarels */ 9224816Skarels if (ti == 0) 9324816Skarels goto present; 9424816Skarels 9524816Skarels /* 9624816Skarels * Find a segment which begins after this one does. 9724816Skarels */ 9824816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 9924816Skarels q = (struct tcpiphdr *)q->ti_next) 10024816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq)) 10124816Skarels break; 10224816Skarels 10324816Skarels /* 10424816Skarels * If there is a preceding segment, it may provide some of 10524816Skarels * our data already. If so, drop the data from the incoming 10624816Skarels * segment. If it provides all of our data, drop us. 10724816Skarels */ 10824816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 10924816Skarels register int i; 11024816Skarels q = (struct tcpiphdr *)q->ti_prev; 11124816Skarels /* conversion to int (in i) handles seq wraparound */ 11224816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq; 11324816Skarels if (i > 0) { 11430525Skarels if (i >= ti->ti_len) { 11530525Skarels tcpstat.tcps_rcvduppack++; 11630525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 11744375Skarels m_freem(m); 11844375Skarels return (0); 11930525Skarels } 12044375Skarels m_adj(m, i); 12124816Skarels ti->ti_len -= i; 12224816Skarels ti->ti_seq += i; 12324816Skarels } 12424816Skarels q = (struct tcpiphdr *)(q->ti_next); 12524816Skarels } 12630525Skarels tcpstat.tcps_rcvoopack++; 12730525Skarels tcpstat.tcps_rcvoobyte += ti->ti_len; 12844375Skarels REASS_MBUF(ti) = m; /* XXX */ 12924816Skarels 13024816Skarels /* 13124816Skarels * While we overlap succeeding segments trim them or, 13224816Skarels * if they are completely covered, dequeue them. 13324816Skarels */ 13424816Skarels while (q != (struct tcpiphdr *)tp) { 13524816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 13624816Skarels if (i <= 0) 13724816Skarels break; 13824816Skarels if (i < q->ti_len) { 13924816Skarels q->ti_seq += i; 14024816Skarels q->ti_len -= i; 14144375Skarels m_adj(REASS_MBUF(q), i); 14224816Skarels break; 14324816Skarels } 14424816Skarels q = (struct tcpiphdr *)q->ti_next; 14544375Skarels m = REASS_MBUF((struct tcpiphdr *)q->ti_prev); 14624816Skarels remque(q->ti_prev); 14724816Skarels m_freem(m); 14824816Skarels } 14924816Skarels 15024816Skarels /* 15124816Skarels * Stick new segment in its place. 15224816Skarels */ 15324816Skarels insque(ti, q->ti_prev); 15424816Skarels 15524816Skarels present: 15624816Skarels /* 15724816Skarels * Present data to user, advancing rcv_nxt through 15824816Skarels * completed sequence space. 15924816Skarels */ 16024816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 16124816Skarels return (0); 16224816Skarels ti = tp->seg_next; 16324816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 16424816Skarels return (0); 16524816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 16624816Skarels return (0); 16724816Skarels do { 16824816Skarels tp->rcv_nxt += ti->ti_len; 16924816Skarels flags = ti->ti_flags & TH_FIN; 17024816Skarels remque(ti); 17144375Skarels m = REASS_MBUF(ti); 17224816Skarels ti = (struct tcpiphdr *)ti->ti_next; 17324816Skarels if (so->so_state & SS_CANTRCVMORE) 17424816Skarels m_freem(m); 17524816Skarels else 17624816Skarels sbappend(&so->so_rcv, m); 17724816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 17824816Skarels sorwakeup(so); 17924816Skarels return (flags); 18024816Skarels } 18124816Skarels 18224816Skarels /* 1835065Swnj * TCP input routine, follows pages 65-76 of the 1845065Swnj * protocol specification dated September, 1981 very closely. 1855065Swnj */ 18661335Sbostic void 18737320Skarels tcp_input(m, iphlen) 18837320Skarels register struct mbuf *m; 18937320Skarels int iphlen; 1904601Swnj { 1914924Swnj register struct tcpiphdr *ti; 19244375Skarels register struct inpcb *inp; 19357433Sandrew caddr_t optp = NULL; 19457433Sandrew int optlen; 1954924Swnj int len, tlen, off; 1965391Swnj register struct tcpcb *tp = 0; 1974924Swnj register int tiflags; 1984803Swnj struct socket *so; 19931721Skarels int todrop, acked, ourfinisacked, needoutput = 0; 2005267Sroot short ostate; 2016028Sroot struct in_addr laddr; 20210769Ssam int dropsocket = 0; 20330525Skarels int iss = 0; 20457433Sandrew u_long tiwin, ts_val, ts_ecr; 20557433Sandrew int ts_present = 0; 2064924Swnj 20730525Skarels tcpstat.tcps_rcvtotal++; 2084924Swnj /* 2095244Sroot * Get IP and TCP header together in first mbuf. 2105244Sroot * Note: IP leaves IP header in first mbuf. 2114924Swnj */ 2125020Sroot ti = mtod(m, struct tcpiphdr *); 21337320Skarels if (iphlen > sizeof (struct ip)) 21437320Skarels ip_stripoptions(m, (struct mbuf *)0); 21544375Skarels if (m->m_len < sizeof (struct tcpiphdr)) { 2165307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 21730525Skarels tcpstat.tcps_rcvshort++; 2185307Sroot return; 2195085Swnj } 2205085Swnj ti = mtod(m, struct tcpiphdr *); 2215085Swnj } 2224601Swnj 2234601Swnj /* 2245244Sroot * Checksum extended TCP header and data. 2254601Swnj */ 2264924Swnj tlen = ((struct ip *)ti)->ip_len; 2274924Swnj len = sizeof (struct ip) + tlen; 22837320Skarels ti->ti_next = ti->ti_prev = 0; 22937320Skarels ti->ti_x1 = 0; 23037320Skarels ti->ti_len = (u_short)tlen; 23144375Skarels HTONS(ti->ti_len); 23237320Skarels if (ti->ti_sum = in_cksum(m, len)) { 23337320Skarels tcpstat.tcps_rcvbadsum++; 23437320Skarels goto drop; 2354601Swnj } 23657947Ssklower #endif /* TUBA_INCLUDE */ 2374601Swnj 2384601Swnj /* 2395244Sroot * Check that TCP offset makes sense, 24044375Skarels * pull out TCP options and adjust length. XXX 2414601Swnj */ 2424924Swnj off = ti->ti_off << 2; 2435231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 24430525Skarels tcpstat.tcps_rcvbadoff++; 2455085Swnj goto drop; 2464924Swnj } 2476211Swnj tlen -= off; 2486211Swnj ti->ti_len = tlen; 2495440Swnj if (off > sizeof (struct tcphdr)) { 25024816Skarels if (m->m_len < sizeof(struct ip) + off) { 25124816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 25230525Skarels tcpstat.tcps_rcvshort++; 25324816Skarels return; 25424816Skarels } 25524816Skarels ti = mtod(m, struct tcpiphdr *); 2565440Swnj } 25757433Sandrew optlen = off - sizeof (struct tcphdr); 25857433Sandrew optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 25957433Sandrew /* 26057433Sandrew * Do quick retrieval of timestamp options ("options 26157433Sandrew * prediction?"). If timestamp is the only option and it's 26257433Sandrew * formatted as recommended in RFC 1323 appendix A, we 26357433Sandrew * quickly get the values now and not bother calling 26457433Sandrew * tcp_dooptions(), etc. 26557433Sandrew */ 26657433Sandrew if ((optlen == TCPOLEN_TSTAMP_APPA || 26757433Sandrew (optlen > TCPOLEN_TSTAMP_APPA && 26857433Sandrew optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && 26957433Sandrew *(u_long *)optp == htonl(TCPOPT_TSTAMP_HDR) && 27057433Sandrew (ti->ti_flags & TH_SYN) == 0) { 27157433Sandrew ts_present = 1; 27257433Sandrew ts_val = ntohl(*(u_long *)(optp + 4)); 27357433Sandrew ts_ecr = ntohl(*(u_long *)(optp + 8)); 27457433Sandrew optp = NULL; /* we've parsed the options */ 2755440Swnj } 2765440Swnj } 2775065Swnj tiflags = ti->ti_flags; 2784924Swnj 2796093Sroot /* 2805244Sroot * Convert TCP protocol specific fields to host format. 2815085Swnj */ 28244375Skarels NTOHL(ti->ti_seq); 28344375Skarels NTOHL(ti->ti_ack); 28444375Skarels NTOHS(ti->ti_win); 28544375Skarels NTOHS(ti->ti_urp); 2865085Swnj 2875085Swnj /* 2888271Sroot * Locate pcb for segment. 2894924Swnj */ 29030525Skarels findpcb: 29144375Skarels inp = tcp_last_inpcb; 29244375Skarels if (inp->inp_lport != ti->ti_dport || 29344375Skarels inp->inp_fport != ti->ti_sport || 29444375Skarels inp->inp_faddr.s_addr != ti->ti_src.s_addr || 29544375Skarels inp->inp_laddr.s_addr != ti->ti_dst.s_addr) { 29644375Skarels inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport, 29744375Skarels ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD); 29844375Skarels if (inp) 29944375Skarels tcp_last_inpcb = inp; 300*66740Sbostic ++tcpstat.tcps_pcbcachemiss; 30144375Skarels } 3025065Swnj 3035065Swnj /* 3045065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 3055244Sroot * all data in the incoming segment is discarded. 30632098Skarels * If the TCB exists but is in CLOSED state, it is embryonic, 30732098Skarels * but should either do a listen or a connect soon. 3085065Swnj */ 3095300Sroot if (inp == 0) 3105085Swnj goto dropwithreset; 3115065Swnj tp = intotcpcb(inp); 3125300Sroot if (tp == 0) 3135085Swnj goto dropwithreset; 31432098Skarels if (tp->t_state == TCPS_CLOSED) 31532098Skarels goto drop; 31657433Sandrew 31757433Sandrew /* Unscale the window into a 32-bit value. */ 31857433Sandrew if ((tiflags & TH_SYN) == 0) 31957433Sandrew tiwin = ti->ti_win << tp->snd_scale; 32057433Sandrew else 32157433Sandrew tiwin = ti->ti_win; 32257433Sandrew 3235109Swnj so = inp->inp_socket; 32444375Skarels if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 32544375Skarels if (so->so_options & SO_DEBUG) { 32644375Skarels ostate = tp->t_state; 32744375Skarels tcp_saveti = *ti; 32844375Skarels } 32944375Skarels if (so->so_options & SO_ACCEPTCONN) { 33044375Skarels so = sonewconn(so, 0); 33144375Skarels if (so == 0) 33244375Skarels goto drop; 33344375Skarels /* 33444375Skarels * This is ugly, but .... 33544375Skarels * 33644375Skarels * Mark socket as temporary until we're 33744375Skarels * committed to keeping it. The code at 33844375Skarels * ``drop'' and ``dropwithreset'' check the 33944375Skarels * flag dropsocket to see if the temporary 34044375Skarels * socket created here should be discarded. 34144375Skarels * We mark the socket as discardable until 34244375Skarels * we're committed to it below in TCPS_LISTEN. 34344375Skarels */ 34444375Skarels dropsocket++; 34544375Skarels inp = (struct inpcb *)so->so_pcb; 34644375Skarels inp->inp_laddr = ti->ti_dst; 34744375Skarels inp->inp_lport = ti->ti_dport; 34844375Skarels #if BSD>=43 34944375Skarels inp->inp_options = ip_srcroute(); 35044375Skarels #endif 35144375Skarels tp = intotcpcb(inp); 35244375Skarels tp->t_state = TCPS_LISTEN; 35357433Sandrew 35457433Sandrew /* Compute proper scaling value from buffer space 35557433Sandrew */ 35657433Sandrew while (tp->request_r_scale < TCP_MAX_WINSHIFT && 35757433Sandrew TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat) 35857433Sandrew tp->request_r_scale++; 35944375Skarels } 3605267Sroot } 3614601Swnj 3624601Swnj /* 3635162Swnj * Segment received on connection. 3645162Swnj * Reset idle time and keep-alive timer. 3655162Swnj */ 3665162Swnj tp->t_idle = 0; 36733745Skarels tp->t_timer[TCPT_KEEP] = tcp_keepidle; 3685162Swnj 3695162Swnj /* 37017272Skarels * Process options if not in LISTEN state, 37117272Skarels * else do it below (after getting remote address). 3725440Swnj */ 37357433Sandrew if (optp && tp->t_state != TCPS_LISTEN) 37457433Sandrew tcp_dooptions(tp, optp, optlen, ti, 37557433Sandrew &ts_present, &ts_val, &ts_ecr); 37657433Sandrew 37744375Skarels /* 37844375Skarels * Header prediction: check for the two common cases 37944375Skarels * of a uni-directional data xfer. If the packet has 38044375Skarels * no control flags, is in-sequence, the window didn't 38144375Skarels * change and we're not retransmitting, it's a 38244375Skarels * candidate. If the length is zero and the ack moved 38344375Skarels * forward, we're the sender side of the xfer. Just 38444375Skarels * free the data acked & wake any higher level process 38544375Skarels * that was blocked waiting for space. If the length 38644375Skarels * is non-zero and the ack didn't move, we're the 38744375Skarels * receiver side. If we're getting packets in-order 38844375Skarels * (the reassembly queue is empty), add the data to 38944375Skarels * the socket buffer and note that we need a delayed ack. 39044375Skarels */ 39144375Skarels if (tp->t_state == TCPS_ESTABLISHED && 39244375Skarels (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 39357433Sandrew (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && 39444375Skarels ti->ti_seq == tp->rcv_nxt && 39557433Sandrew tiwin && tiwin == tp->snd_wnd && 39644375Skarels tp->snd_nxt == tp->snd_max) { 39757433Sandrew 39857433Sandrew /* 39957433Sandrew * If last ACK falls within this segment's sequence numbers, 40057433Sandrew * record the timestamp. 40157433Sandrew */ 40257433Sandrew if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) && 40357433Sandrew SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) { 40457433Sandrew tp->ts_recent_age = tcp_now; 40557433Sandrew tp->ts_recent = ts_val; 40657433Sandrew } 40757433Sandrew 40844375Skarels if (ti->ti_len == 0) { 40944375Skarels if (SEQ_GT(ti->ti_ack, tp->snd_una) && 41044375Skarels SEQ_LEQ(ti->ti_ack, tp->snd_max) && 41144375Skarels tp->snd_cwnd >= tp->snd_wnd) { 41244375Skarels /* 41344375Skarels * this is a pure ack for outstanding data. 41444375Skarels */ 415*66740Sbostic ++tcpstat.tcps_predack; 41657433Sandrew if (ts_present) 41757433Sandrew tcp_xmit_timer(tp, tcp_now-ts_ecr+1); 41857433Sandrew else if (tp->t_rtt && 41957433Sandrew SEQ_GT(ti->ti_ack, tp->t_rtseq)) 42057433Sandrew tcp_xmit_timer(tp, tp->t_rtt); 42144375Skarels acked = ti->ti_ack - tp->snd_una; 42244375Skarels tcpstat.tcps_rcvackpack++; 42344375Skarels tcpstat.tcps_rcvackbyte += acked; 42444375Skarels sbdrop(&so->so_snd, acked); 42544375Skarels tp->snd_una = ti->ti_ack; 42644375Skarels m_freem(m); 4275440Swnj 42844375Skarels /* 42944375Skarels * If all outstanding data are acked, stop 43044375Skarels * retransmit timer, otherwise restart timer 43144375Skarels * using current (possibly backed-off) value. 43244375Skarels * If process is waiting for space, 43344375Skarels * wakeup/selwakeup/signal. If data 43444375Skarels * are ready to send, let tcp_output 43544375Skarels * decide between more output or persist. 43644375Skarels */ 43744375Skarels if (tp->snd_una == tp->snd_max) 43844375Skarels tp->t_timer[TCPT_REXMT] = 0; 43944375Skarels else if (tp->t_timer[TCPT_PERSIST] == 0) 44044375Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 44144375Skarels 44244375Skarels if (so->so_snd.sb_flags & SB_NOTIFY) 44344375Skarels sowwakeup(so); 44444375Skarels if (so->so_snd.sb_cc) 44544375Skarels (void) tcp_output(tp); 44644375Skarels return; 44744375Skarels } 44844375Skarels } else if (ti->ti_ack == tp->snd_una && 44944375Skarels tp->seg_next == (struct tcpiphdr *)tp && 45044375Skarels ti->ti_len <= sbspace(&so->so_rcv)) { 45144375Skarels /* 45244375Skarels * this is a pure, in-sequence data packet 45344375Skarels * with nothing on the reassembly queue and 45444375Skarels * we have enough buffer space to take it. 45544375Skarels */ 456*66740Sbostic ++tcpstat.tcps_preddat; 45744375Skarels tp->rcv_nxt += ti->ti_len; 45844375Skarels tcpstat.tcps_rcvpack++; 45944375Skarels tcpstat.tcps_rcvbyte += ti->ti_len; 46044375Skarels /* 46157433Sandrew * Drop TCP, IP headers and TCP options then add data 46257433Sandrew * to socket buffer. 46344375Skarels */ 46457433Sandrew m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 46557433Sandrew m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 46644375Skarels sbappend(&so->so_rcv, m); 46744375Skarels sorwakeup(so); 46844375Skarels tp->t_flags |= TF_DELACK; 46944375Skarels return; 47044375Skarels } 47144375Skarels } 47244375Skarels 4735440Swnj /* 47457433Sandrew * Drop TCP, IP headers and TCP options. 47544375Skarels */ 47657433Sandrew m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 47757433Sandrew m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 47844375Skarels 47944375Skarels /* 4805085Swnj * Calculate amount of space in receive window, 4815085Swnj * and then do TCP input processing. 48224816Skarels * Receive window is amount of space in rcv queue, 48324816Skarels * but not less than advertised window. 4844601Swnj */ 48525939Skarels { int win; 4864601Swnj 48725939Skarels win = sbspace(&so->so_rcv); 48825939Skarels if (win < 0) 48925939Skarels win = 0; 49037320Skarels tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 49125939Skarels } 49225939Skarels 4934601Swnj switch (tp->t_state) { 4944601Swnj 4955065Swnj /* 4965065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 4975065Swnj * If the segment contains an ACK then it is bad and send a RST. 4985065Swnj * If it does not contain a SYN then it is not interesting; drop it. 49925197Skarels * Don't bother responding if the destination was a broadcast. 5005085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 5015065Swnj * tp->iss, and send a segment: 5025085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 5035065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 5045065Swnj * Fill in remote peer address fields if not previously specified. 5055065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 5065244Sroot * segment in this state. 5075065Swnj */ 5088271Sroot case TCPS_LISTEN: { 50910145Ssam struct mbuf *am; 5108271Sroot register struct sockaddr_in *sin; 5118271Sroot 5125065Swnj if (tiflags & TH_RST) 5135065Swnj goto drop; 5145300Sroot if (tiflags & TH_ACK) 5155085Swnj goto dropwithreset; 5165300Sroot if ((tiflags & TH_SYN) == 0) 5175065Swnj goto drop; 51858998Ssklower /* 51958998Ssklower * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 52058998Ssklower * in_broadcast() should never return true on a received 52158998Ssklower * packet with M_BCAST not set. 52258998Ssklower */ 52357433Sandrew if (m->m_flags & (M_BCAST|M_MCAST) || 52458998Ssklower IN_MULTICAST(ti->ti_dst.s_addr)) 52525197Skarels goto drop; 52644375Skarels am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 52710145Ssam if (am == NULL) 52810145Ssam goto drop; 52910145Ssam am->m_len = sizeof (struct sockaddr_in); 5308599Sroot sin = mtod(am, struct sockaddr_in *); 5318271Sroot sin->sin_family = AF_INET; 53237320Skarels sin->sin_len = sizeof(*sin); 5338271Sroot sin->sin_addr = ti->ti_src; 5348271Sroot sin->sin_port = ti->ti_sport; 53556399Ssklower bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 5366028Sroot laddr = inp->inp_laddr; 53710145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 5386028Sroot inp->inp_laddr = ti->ti_dst; 5398599Sroot if (in_pcbconnect(inp, am)) { 5406028Sroot inp->inp_laddr = laddr; 5418716Sroot (void) m_free(am); 5425244Sroot goto drop; 5436028Sroot } 5448716Sroot (void) m_free(am); 5455244Sroot tp->t_template = tcp_template(tp); 5465244Sroot if (tp->t_template == 0) { 54726386Skarels tp = tcp_drop(tp, ENOBUFS); 54817264Skarels dropsocket = 0; /* socket is already gone */ 5495244Sroot goto drop; 5505244Sroot } 55157433Sandrew if (optp) 55257433Sandrew tcp_dooptions(tp, optp, optlen, ti, 55357433Sandrew &ts_present, &ts_val, &ts_ecr); 55430525Skarels if (iss) 55530525Skarels tp->iss = iss; 55630525Skarels else 55730525Skarels tp->iss = tcp_iss; 55830525Skarels tcp_iss += TCP_ISSINCR/2; 5595065Swnj tp->irs = ti->ti_seq; 5605085Swnj tcp_sendseqinit(tp); 5615085Swnj tcp_rcvseqinit(tp); 56225939Skarels tp->t_flags |= TF_ACKNOW; 5635065Swnj tp->t_state = TCPS_SYN_RECEIVED; 56433745Skarels tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 56510769Ssam dropsocket = 0; /* committed to socket */ 56630525Skarels tcpstat.tcps_accepts++; 5675085Swnj goto trimthenstep6; 5688271Sroot } 5694601Swnj 5705065Swnj /* 5715065Swnj * If the state is SYN_SENT: 5725065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 5735065Swnj * if seg contains a RST, then drop the connection. 5745065Swnj * if seg does not contain SYN, then drop it. 5755065Swnj * Otherwise this is an acceptable SYN segment 5765065Swnj * initialize tp->rcv_nxt and tp->irs 5775065Swnj * if seg contains ack then advance tp->snd_una 5785065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 5795065Swnj * arrange for segment to be acked (eventually) 5805065Swnj * continue processing rest of data/controls, beginning with URG 5815065Swnj */ 5825065Swnj case TCPS_SYN_SENT: 5835065Swnj if ((tiflags & TH_ACK) && 58424816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) || 5855231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 5865085Swnj goto dropwithreset; 5875065Swnj if (tiflags & TH_RST) { 58810394Ssam if (tiflags & TH_ACK) 58910394Ssam tp = tcp_drop(tp, ECONNREFUSED); 5905065Swnj goto drop; 5914601Swnj } 5925065Swnj if ((tiflags & TH_SYN) == 0) 5935065Swnj goto drop; 59430974Skarels if (tiflags & TH_ACK) { 59530974Skarels tp->snd_una = ti->ti_ack; 59630974Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 59730974Skarels tp->snd_nxt = tp->snd_una; 59830974Skarels } 5995244Sroot tp->t_timer[TCPT_REXMT] = 0; 6005065Swnj tp->irs = ti->ti_seq; 6015085Swnj tcp_rcvseqinit(tp); 6025085Swnj tp->t_flags |= TF_ACKNOW; 60330974Skarels if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 60430525Skarels tcpstat.tcps_connects++; 6055244Sroot soisconnected(so); 6065065Swnj tp->t_state = TCPS_ESTABLISHED; 60757433Sandrew /* Do window scaling on this connection? */ 60857433Sandrew if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 60957433Sandrew (TF_RCVD_SCALE|TF_REQ_SCALE)) { 61057433Sandrew tp->snd_scale = tp->requested_s_scale; 61157433Sandrew tp->rcv_scale = tp->request_r_scale; 61257433Sandrew } 61344375Skarels (void) tcp_reass(tp, (struct tcpiphdr *)0, 61444375Skarels (struct mbuf *)0); 61532098Skarels /* 61632098Skarels * if we didn't have to retransmit the SYN, 61732098Skarels * use its rtt as our initial srtt & rtt var. 61832098Skarels */ 61944375Skarels if (tp->t_rtt) 62057433Sandrew tcp_xmit_timer(tp, tp->t_rtt); 6215162Swnj } else 6225085Swnj tp->t_state = TCPS_SYN_RECEIVED; 6235085Swnj 6245085Swnj trimthenstep6: 6255085Swnj /* 6265231Swnj * Advance ti->ti_seq to correspond to first data byte. 6275085Swnj * If data, trim to stay within window, 6285085Swnj * dropping FIN if necessary. 6295085Swnj */ 6305231Swnj ti->ti_seq++; 6315085Swnj if (ti->ti_len > tp->rcv_wnd) { 6325085Swnj todrop = ti->ti_len - tp->rcv_wnd; 6335085Swnj m_adj(m, -todrop); 6345085Swnj ti->ti_len = tp->rcv_wnd; 63525939Skarels tiflags &= ~TH_FIN; 63630525Skarels tcpstat.tcps_rcvpackafterwin++; 63730525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 6385065Swnj } 6395263Swnj tp->snd_wl1 = ti->ti_seq - 1; 64025939Skarels tp->rcv_up = ti->ti_seq; 6415085Swnj goto step6; 6425065Swnj } 6434601Swnj 6445065Swnj /* 64530525Skarels * States other than LISTEN or SYN_SENT. 64657433Sandrew * First check timestamp, if present. 64757433Sandrew * Then check that at least some bytes of segment are within 64830525Skarels * receive window. If segment begins before rcv_nxt, 64930525Skarels * drop leading data (and SYN); if nothing left, just ack. 65057433Sandrew * 65157433Sandrew * RFC 1323 PAWS: If we have a timestamp reply on this segment 65257433Sandrew * and it's less than ts_recent, drop it. 65316222Skarels */ 65457433Sandrew if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && 65557433Sandrew TSTMP_LT(ts_val, tp->ts_recent)) { 65657433Sandrew 65757433Sandrew /* Check to see if ts_recent is over 24 days old. */ 65857433Sandrew if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 65957433Sandrew /* 66057433Sandrew * Invalidate ts_recent. If this segment updates 66157433Sandrew * ts_recent, the age will be reset later and ts_recent 66257433Sandrew * will get a valid value. If it does not, setting 66357433Sandrew * ts_recent to zero will at least satisfy the 66457433Sandrew * requirement that zero be placed in the timestamp 66557433Sandrew * echo reply when ts_recent isn't valid. The 66657433Sandrew * age isn't reset until we get a valid ts_recent 66757433Sandrew * because we don't want out-of-order segments to be 66857433Sandrew * dropped when ts_recent is old. 66957433Sandrew */ 67057433Sandrew tp->ts_recent = 0; 67157433Sandrew } else { 67257433Sandrew tcpstat.tcps_rcvduppack++; 67357433Sandrew tcpstat.tcps_rcvdupbyte += ti->ti_len; 67457433Sandrew tcpstat.tcps_pawsdrop++; 67557433Sandrew goto dropafterack; 67657433Sandrew } 67757433Sandrew } 67857433Sandrew 67930525Skarels todrop = tp->rcv_nxt - ti->ti_seq; 68030525Skarels if (todrop > 0) { 68130525Skarels if (tiflags & TH_SYN) { 68230525Skarels tiflags &= ~TH_SYN; 68330525Skarels ti->ti_seq++; 68430525Skarels if (ti->ti_urp > 1) 68530525Skarels ti->ti_urp--; 68630525Skarels else 68730525Skarels tiflags &= ~TH_URG; 68830525Skarels todrop--; 68930525Skarels } 69064176Smckusick if (todrop >= ti->ti_len) { 69133745Skarels tcpstat.tcps_rcvduppack++; 69233745Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 69331730Skarels /* 69433745Skarels * If segment is just one to the left of the window, 69533745Skarels * check two special cases: 69633745Skarels * 1. Don't toss RST in response to 4.2-style keepalive. 69733745Skarels * 2. If the only thing to drop is a FIN, we can drop 69833745Skarels * it, but check the ACK or we will get into FIN 69933745Skarels * wars if our FINs crossed (both CLOSING). 70033745Skarels * In either case, send ACK to resynchronize, 70133745Skarels * but keep on processing for RST or ACK. 70231730Skarels */ 70333745Skarels if ((tiflags & TH_FIN && todrop == ti->ti_len + 1) 70433745Skarels #ifdef TCP_COMPAT_42 70533745Skarels || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1) 70631730Skarels #endif 70733745Skarels ) { 70833745Skarels todrop = ti->ti_len; 70933745Skarels tiflags &= ~TH_FIN; 71033745Skarels tp->t_flags |= TF_ACKNOW; 71157433Sandrew } else { 71257433Sandrew /* 71357433Sandrew * Handle the case when a bound socket connects 71457433Sandrew * to itself. Allow packets with a SYN and 71557433Sandrew * an ACK to continue with the processing. 71657433Sandrew */ 71757433Sandrew if (todrop != 0 || (tiflags & TH_ACK) == 0) 71857433Sandrew goto dropafterack; 71957433Sandrew } 72032034Skarels } else { 72132034Skarels tcpstat.tcps_rcvpartduppack++; 72232034Skarels tcpstat.tcps_rcvpartdupbyte += todrop; 72330525Skarels } 72430525Skarels m_adj(m, todrop); 72530525Skarels ti->ti_seq += todrop; 72630525Skarels ti->ti_len -= todrop; 72730525Skarels if (ti->ti_urp > todrop) 72830525Skarels ti->ti_urp -= todrop; 72930525Skarels else { 73030525Skarels tiflags &= ~TH_URG; 73130525Skarels ti->ti_urp = 0; 73230525Skarels } 73316222Skarels } 73416222Skarels 73532612Skarels /* 73633745Skarels * If new data are received on a connection after the 73732612Skarels * user processes are gone, then RST the other end. 73832612Skarels */ 73932612Skarels if ((so->so_state & SS_NOFDREF) && 74032612Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 74132612Skarels tp = tcp_close(tp); 74232612Skarels tcpstat.tcps_rcvafterclose++; 74332612Skarels goto dropwithreset; 74432612Skarels } 74532612Skarels 74633445Skarels /* 74733445Skarels * If segment ends after window, drop trailing data 74833445Skarels * (and PUSH and FIN); if nothing left, just ACK. 74933445Skarels */ 75033445Skarels todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 75133445Skarels if (todrop > 0) { 75233445Skarels tcpstat.tcps_rcvpackafterwin++; 75333445Skarels if (todrop >= ti->ti_len) { 75430525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 75533445Skarels /* 75633445Skarels * If a new connection request is received 75733445Skarels * while in TIME_WAIT, drop the old connection 75833445Skarels * and start over if the sequence numbers 75933445Skarels * are above the previous ones. 76033445Skarels */ 76133445Skarels if (tiflags & TH_SYN && 76233445Skarels tp->t_state == TCPS_TIME_WAIT && 76333445Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 76433445Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 76544375Skarels tp = tcp_close(tp); 76633445Skarels goto findpcb; 76733445Skarels } 76833445Skarels /* 76933445Skarels * If window is closed can only take segments at 77033445Skarels * window edge, and have to drop data and PUSH from 77133445Skarels * incoming segments. Continue processing, but 77233445Skarels * remember to ack. Otherwise, drop segment 77333445Skarels * and ack. 77433445Skarels */ 77533445Skarels if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 77633445Skarels tp->t_flags |= TF_ACKNOW; 77730525Skarels tcpstat.tcps_rcvwinprobe++; 77833445Skarels } else 7795065Swnj goto dropafterack; 78033445Skarels } else 78130525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 78233445Skarels m_adj(m, -todrop); 78333445Skarels ti->ti_len -= todrop; 78433445Skarels tiflags &= ~(TH_PUSH|TH_FIN); 7855065Swnj } 7864601Swnj 7875065Swnj /* 78857433Sandrew * If last ACK falls within this segment's sequence numbers, 78957433Sandrew * record its timestamp. 79057433Sandrew */ 79157433Sandrew if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) && 79257433Sandrew SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len + 79357433Sandrew ((tiflags & (TH_SYN|TH_FIN)) != 0))) { 79457433Sandrew tp->ts_recent_age = tcp_now; 79557433Sandrew tp->ts_recent = ts_val; 79657433Sandrew } 79757433Sandrew 79857433Sandrew /* 7995065Swnj * If the RST bit is set examine the state: 8005065Swnj * SYN_RECEIVED STATE: 8015065Swnj * If passive open, return to LISTEN state. 8025065Swnj * If active open, inform user that connection was refused. 8035065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 8045065Swnj * Inform user that connection was reset, and close tcb. 8055065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 8065065Swnj * Close the tcb. 8075065Swnj */ 8085065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 8095267Sroot 8105065Swnj case TCPS_SYN_RECEIVED: 81133745Skarels so->so_error = ECONNREFUSED; 81233745Skarels goto close; 8134601Swnj 8145065Swnj case TCPS_ESTABLISHED: 8155065Swnj case TCPS_FIN_WAIT_1: 8165065Swnj case TCPS_FIN_WAIT_2: 8175065Swnj case TCPS_CLOSE_WAIT: 81833745Skarels so->so_error = ECONNRESET; 81933745Skarels close: 82033745Skarels tp->t_state = TCPS_CLOSED; 82133745Skarels tcpstat.tcps_drops++; 82233745Skarels tp = tcp_close(tp); 8235065Swnj goto drop; 8245065Swnj 8255065Swnj case TCPS_CLOSING: 8265065Swnj case TCPS_LAST_ACK: 8275065Swnj case TCPS_TIME_WAIT: 82810394Ssam tp = tcp_close(tp); 8295065Swnj goto drop; 8304601Swnj } 8314601Swnj 8324601Swnj /* 8335065Swnj * If a SYN is in the window, then this is an 8345065Swnj * error and we send an RST and drop the connection. 8354601Swnj */ 8365065Swnj if (tiflags & TH_SYN) { 83710394Ssam tp = tcp_drop(tp, ECONNRESET); 8385085Swnj goto dropwithreset; 8394601Swnj } 8404601Swnj 8414601Swnj /* 8425065Swnj * If the ACK bit is off we drop the segment and return. 8434601Swnj */ 8445085Swnj if ((tiflags & TH_ACK) == 0) 8455065Swnj goto drop; 8465065Swnj 8475065Swnj /* 8485065Swnj * Ack processing. 8495065Swnj */ 8504601Swnj switch (tp->t_state) { 8514601Swnj 8525065Swnj /* 8535065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 85431721Skarels * ESTABLISHED state and continue processing, otherwise 8555065Swnj * send an RST. 8565065Swnj */ 8575065Swnj case TCPS_SYN_RECEIVED: 8585085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 8595231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 8605085Swnj goto dropwithreset; 86130525Skarels tcpstat.tcps_connects++; 8625085Swnj soisconnected(so); 8635085Swnj tp->t_state = TCPS_ESTABLISHED; 86457433Sandrew /* Do window scaling? */ 86557433Sandrew if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 86657433Sandrew (TF_RCVD_SCALE|TF_REQ_SCALE)) { 86757433Sandrew tp->snd_scale = tp->requested_s_scale; 86857433Sandrew tp->rcv_scale = tp->request_r_scale; 86957433Sandrew } 87044375Skarels (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0); 8715244Sroot tp->snd_wl1 = ti->ti_seq - 1; 8725085Swnj /* fall into ... */ 8734601Swnj 8745065Swnj /* 8755065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 8765065Swnj * ACKs. If the ack is in the range 8775231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 8785065Swnj * then advance tp->snd_una to ti->ti_ack and drop 8795065Swnj * data from the retransmission queue. If this ACK reflects 8805065Swnj * more up to date window information we update our window information. 8815065Swnj */ 8825065Swnj case TCPS_ESTABLISHED: 8835065Swnj case TCPS_FIN_WAIT_1: 8845065Swnj case TCPS_FIN_WAIT_2: 8855065Swnj case TCPS_CLOSE_WAIT: 8865065Swnj case TCPS_CLOSING: 8875244Sroot case TCPS_LAST_ACK: 8885244Sroot case TCPS_TIME_WAIT: 8895085Swnj 89030525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 89157433Sandrew if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { 89230525Skarels tcpstat.tcps_rcvdupack++; 89332098Skarels /* 89444375Skarels * If we have outstanding data (other than 89544375Skarels * a window probe), this is a completely 89632098Skarels * duplicate ack (ie, window info didn't 89732098Skarels * change), the ack is the biggest we've 89832098Skarels * seen and we've seen exactly our rexmt 89932098Skarels * threshhold of them, assume a packet 90032098Skarels * has been dropped and retransmit it. 90132098Skarels * Kludge snd_nxt & the congestion 90232098Skarels * window so we send only this one 90344375Skarels * packet. 90444375Skarels * 90544375Skarels * We know we're losing at the current 90644375Skarels * window size so do congestion avoidance 90744375Skarels * (set ssthresh to half the current window 90844375Skarels * and pull our congestion window back to 90944375Skarels * the new ssthresh). 91044375Skarels * 91144375Skarels * Dup acks mean that packets have left the 91244375Skarels * network (they're now cached at the receiver) 91344375Skarels * so bump cwnd by the amount in the receiver 91444375Skarels * to keep a constant cwnd packets in the 91544375Skarels * network. 91632098Skarels */ 91732098Skarels if (tp->t_timer[TCPT_REXMT] == 0 || 91832098Skarels ti->ti_ack != tp->snd_una) 91932098Skarels tp->t_dupacks = 0; 92032098Skarels else if (++tp->t_dupacks == tcprexmtthresh) { 92132098Skarels tcp_seq onxt = tp->snd_nxt; 92232376Skarels u_int win = 92337320Skarels min(tp->snd_wnd, tp->snd_cwnd) / 2 / 92432376Skarels tp->t_maxseg; 92532098Skarels 92632376Skarels if (win < 2) 92732376Skarels win = 2; 92832376Skarels tp->snd_ssthresh = win * tp->t_maxseg; 92932098Skarels tp->t_timer[TCPT_REXMT] = 0; 93032098Skarels tp->t_rtt = 0; 93132098Skarels tp->snd_nxt = ti->ti_ack; 93232098Skarels tp->snd_cwnd = tp->t_maxseg; 93332098Skarels (void) tcp_output(tp); 93444375Skarels tp->snd_cwnd = tp->snd_ssthresh + 93544375Skarels tp->t_maxseg * tp->t_dupacks; 93632098Skarels if (SEQ_GT(onxt, tp->snd_nxt)) 93732098Skarels tp->snd_nxt = onxt; 93832098Skarels goto drop; 93944375Skarels } else if (tp->t_dupacks > tcprexmtthresh) { 94044375Skarels tp->snd_cwnd += tp->t_maxseg; 94144375Skarels (void) tcp_output(tp); 94244375Skarels goto drop; 94332098Skarels } 94432098Skarels } else 94532098Skarels tp->t_dupacks = 0; 9465065Swnj break; 94730525Skarels } 94844375Skarels /* 94944375Skarels * If the congestion window was inflated to account 95044375Skarels * for the other side's cached packets, retract it. 95144375Skarels */ 95244375Skarels if (tp->t_dupacks > tcprexmtthresh && 95344375Skarels tp->snd_cwnd > tp->snd_ssthresh) 95444375Skarels tp->snd_cwnd = tp->snd_ssthresh; 95532098Skarels tp->t_dupacks = 0; 95630525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 95730525Skarels tcpstat.tcps_rcvacktoomuch++; 9585065Swnj goto dropafterack; 95930525Skarels } 9605085Swnj acked = ti->ti_ack - tp->snd_una; 96130525Skarels tcpstat.tcps_rcvackpack++; 96230525Skarels tcpstat.tcps_rcvackbyte += acked; 9635951Swnj 9645951Swnj /* 96557433Sandrew * If we have a timestamp reply, update smoothed 96657433Sandrew * round trip time. If no timestamp is present but 96757433Sandrew * transmit timer is running and timed sequence 9685951Swnj * number was acked, update smoothed round trip time. 96932034Skarels * Since we now have an rtt measurement, cancel the 97032034Skarels * timer backoff (cf., Phil Karn's retransmit alg.). 97132034Skarels * Recompute the initial retransmit timer. 9725951Swnj */ 97357433Sandrew if (ts_present) 97457433Sandrew tcp_xmit_timer(tp, tcp_now-ts_ecr+1); 97557433Sandrew else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 97657433Sandrew tcp_xmit_timer(tp,tp->t_rtt); 97731726Skarels 97826824Skarels /* 97926824Skarels * If all outstanding data is acked, stop retransmit 98026824Skarels * timer and remember to restart (more output or persist). 98126824Skarels * If there is more data to be acked, restart retransmit 98232034Skarels * timer, using current (possibly backed-off) value. 98326824Skarels */ 98426824Skarels if (ti->ti_ack == tp->snd_max) { 9855244Sroot tp->t_timer[TCPT_REXMT] = 0; 98626824Skarels needoutput = 1; 98732034Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) 98832034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 98917360Skarels /* 99032098Skarels * When new data is acked, open the congestion window. 99132098Skarels * If the window gives us less than ssthresh packets 99232098Skarels * in flight, open exponentially (maxseg per packet). 99344375Skarels * Otherwise open linearly: maxseg per window 99444375Skarels * (maxseg^2 / cwnd per packet), plus a constant 99544375Skarels * fraction of a packet (maxseg/8) to help larger windows 99644375Skarels * open quickly enough. 99717360Skarels */ 99832098Skarels { 99944375Skarels register u_int cw = tp->snd_cwnd; 100044375Skarels register u_int incr = tp->t_maxseg; 100132098Skarels 100244375Skarels if (cw > tp->snd_ssthresh) 100344375Skarels incr = incr * incr / cw + incr / 8; 100457433Sandrew tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); 100532098Skarels } 10065307Sroot if (acked > so->so_snd.sb_cc) { 100715386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 100826386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 100931721Skarels ourfinisacked = 1; 10105307Sroot } else { 10116161Ssam sbdrop(&so->so_snd, acked); 10125307Sroot tp->snd_wnd -= acked; 101331721Skarels ourfinisacked = 0; 10145307Sroot } 101544375Skarels if (so->so_snd.sb_flags & SB_NOTIFY) 101644375Skarels sowwakeup(so); 10175231Swnj tp->snd_una = ti->ti_ack; 10185357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 10195357Sroot tp->snd_nxt = tp->snd_una; 10205162Swnj 10214601Swnj switch (tp->t_state) { 10224601Swnj 10235065Swnj /* 10245065Swnj * In FIN_WAIT_1 STATE in addition to the processing 10255065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 10265085Swnj * then enter FIN_WAIT_2. 10275065Swnj */ 10285065Swnj case TCPS_FIN_WAIT_1: 10295896Swnj if (ourfinisacked) { 10305896Swnj /* 10315896Swnj * If we can't receive any more 10325896Swnj * data, then closing user can proceed. 103324816Skarels * Starting the timer is contrary to the 103424816Skarels * specification, but if we don't get a FIN 103524816Skarels * we'll hang forever. 10365896Swnj */ 103724816Skarels if (so->so_state & SS_CANTRCVMORE) { 10385896Swnj soisdisconnected(so); 103933745Skarels tp->t_timer[TCPT_2MSL] = tcp_maxidle; 104024816Skarels } 10415085Swnj tp->t_state = TCPS_FIN_WAIT_2; 10425896Swnj } 10434601Swnj break; 10444601Swnj 10455065Swnj /* 10465065Swnj * In CLOSING STATE in addition to the processing for 10475065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 10485065Swnj * then enter the TIME-WAIT state, otherwise ignore 10495065Swnj * the segment. 10505065Swnj */ 10515065Swnj case TCPS_CLOSING: 10525244Sroot if (ourfinisacked) { 10535065Swnj tp->t_state = TCPS_TIME_WAIT; 10545244Sroot tcp_canceltimers(tp); 10555244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10565244Sroot soisdisconnected(so); 10575244Sroot } 10585244Sroot break; 10594601Swnj 10605065Swnj /* 106131743Skarels * In LAST_ACK, we may still be waiting for data to drain 106231743Skarels * and/or to be acked, as well as for the ack of our FIN. 106331743Skarels * If our FIN is now acknowledged, delete the TCB, 106431743Skarels * enter the closed state and return. 10655065Swnj */ 10665065Swnj case TCPS_LAST_ACK: 106731743Skarels if (ourfinisacked) { 106810394Ssam tp = tcp_close(tp); 106931743Skarels goto drop; 107031743Skarels } 107131743Skarels break; 10724601Swnj 10735065Swnj /* 10745065Swnj * In TIME_WAIT state the only thing that should arrive 10755065Swnj * is a retransmission of the remote FIN. Acknowledge 10765065Swnj * it and restart the finack timer. 10775065Swnj */ 10785065Swnj case TCPS_TIME_WAIT: 10795162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10805065Swnj goto dropafterack; 10814601Swnj } 10825085Swnj } 10834601Swnj 10845065Swnj step6: 10855065Swnj /* 10865244Sroot * Update window information. 108725939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 10885244Sroot */ 108925939Skarels if ((tiflags & TH_ACK) && 109025939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 10915391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 109257433Sandrew tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))) { 109330525Skarels /* keep track of pure window updates */ 109430525Skarels if (ti->ti_len == 0 && 109557433Sandrew tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) 109630525Skarels tcpstat.tcps_rcvwinupd++; 109757433Sandrew tp->snd_wnd = tiwin; 10985244Sroot tp->snd_wl1 = ti->ti_seq; 10995244Sroot tp->snd_wl2 = ti->ti_ack; 110025260Skarels if (tp->snd_wnd > tp->max_sndwnd) 110125260Skarels tp->max_sndwnd = tp->snd_wnd; 110226824Skarels needoutput = 1; 110326824Skarels } 11045244Sroot 11055244Sroot /* 11065547Swnj * Process segments with URG. 11075065Swnj */ 11087267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 11097267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 11105547Swnj /* 111125939Skarels * This is a kludge, but if we receive and accept 111213121Ssam * random urgent pointers, we'll crash in 111313121Ssam * soreceive. It's hard to imagine someone 111413121Ssam * actually wanting to send this much urgent data. 111512441Ssam */ 111657433Sandrew if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) { 111712441Ssam ti->ti_urp = 0; /* XXX */ 111812441Ssam tiflags &= ~TH_URG; /* XXX */ 111925939Skarels goto dodata; /* XXX */ 112012441Ssam } 112112441Ssam /* 11225547Swnj * If this segment advances the known urgent pointer, 11235547Swnj * then mark the data stream. This should not happen 11245547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 11255547Swnj * a FIN has been received from the remote side. 11265547Swnj * In these states we ignore the URG. 112727190Skarels * 112827190Skarels * According to RFC961 (Assigned Protocols), 112927190Skarels * the urgent pointer points to the last octet 113027190Skarels * of urgent data. We continue, however, 113127190Skarels * to consider it to indicate the first octet 113244375Skarels * of data past the urgent section as the original 113344375Skarels * spec states (in one of two places). 11345547Swnj */ 11355547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 11365547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 11375547Swnj so->so_oobmark = so->so_rcv.sb_cc + 11385547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 11395547Swnj if (so->so_oobmark == 0) 11405547Swnj so->so_state |= SS_RCVATMARK; 11418313Sroot sohasoutofband(so); 114224816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 11435440Swnj } 11445547Swnj /* 11455547Swnj * Remove out of band data so doesn't get presented to user. 11465547Swnj * This can happen independent of advancing the URG pointer, 11475547Swnj * but if two URG's are pending at once, some out-of-band 11485547Swnj * data may creep in... ick. 11495547Swnj */ 115044375Skarels if (ti->ti_urp <= ti->ti_len 115144375Skarels #ifdef SO_OOBINLINE 115244375Skarels && (so->so_options & SO_OOBINLINE) == 0 115344375Skarels #endif 115444375Skarels ) 115544375Skarels tcp_pulloutofband(so, ti, m); 115625939Skarels } else 115725939Skarels /* 115825939Skarels * If no out of band data is expected, 115925939Skarels * pull receive urgent pointer along 116025939Skarels * with the receive window. 116125939Skarels */ 116225939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 116325939Skarels tp->rcv_up = tp->rcv_nxt; 116425939Skarels dodata: /* XXX */ 11654601Swnj 11664601Swnj /* 11675065Swnj * Process the segment text, merging it into the TCP sequencing queue, 11685065Swnj * and arranging for acknowledgment of receipt if necessary. 11695065Swnj * This process logically involves adjusting tp->rcv_wnd as data 11705065Swnj * is presented to the user (this happens in tcp_usrreq.c, 11715065Swnj * case PRU_RCVD). If a FIN has already been received on this 11725065Swnj * connection then we just ignore the text. 11734601Swnj */ 117417946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 117517946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 117624816Skarels TCP_REASS(tp, ti, m, so, tiflags); 117725260Skarels /* 117825260Skarels * Note the amount of data that peer has sent into 117925260Skarels * our window, in order to estimate the sender's 118025260Skarels * buffer size. 118125260Skarels */ 118232098Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 11835244Sroot } else { 11844924Swnj m_freem(m); 11855263Swnj tiflags &= ~TH_FIN; 11865244Sroot } 11874601Swnj 11884601Swnj /* 11895263Swnj * If FIN is received ACK the FIN and let the user know 11905263Swnj * that the connection is closing. 11914601Swnj */ 11925263Swnj if (tiflags & TH_FIN) { 11935244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 11945244Sroot socantrcvmore(so); 11955244Sroot tp->t_flags |= TF_ACKNOW; 11965244Sroot tp->rcv_nxt++; 11975244Sroot } 11985065Swnj switch (tp->t_state) { 11994601Swnj 12005065Swnj /* 12015065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 12025065Swnj * enter the CLOSE_WAIT state. 12034884Swnj */ 12045065Swnj case TCPS_SYN_RECEIVED: 12055065Swnj case TCPS_ESTABLISHED: 12065065Swnj tp->t_state = TCPS_CLOSE_WAIT; 12075065Swnj break; 12084884Swnj 12095065Swnj /* 12105085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 12115085Swnj * enter the CLOSING state. 12124884Swnj */ 12135065Swnj case TCPS_FIN_WAIT_1: 12145085Swnj tp->t_state = TCPS_CLOSING; 12155065Swnj break; 12164601Swnj 12175065Swnj /* 12185065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 12195065Swnj * starting the time-wait timer, turning off the other 12205065Swnj * standard timers. 12215065Swnj */ 12225065Swnj case TCPS_FIN_WAIT_2: 12235244Sroot tp->t_state = TCPS_TIME_WAIT; 12245074Swnj tcp_canceltimers(tp); 12255162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 12265244Sroot soisdisconnected(so); 12275065Swnj break; 12285065Swnj 12294884Swnj /* 12305065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 12314884Swnj */ 12325065Swnj case TCPS_TIME_WAIT: 12335162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 12345065Swnj break; 12355085Swnj } 12364601Swnj } 12375267Sroot if (so->so_options & SO_DEBUG) 12385267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 12395085Swnj 12405085Swnj /* 12415085Swnj * Return any desired output. 12425085Swnj */ 124326824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 124425939Skarels (void) tcp_output(tp); 12455065Swnj return; 12465085Swnj 12475065Swnj dropafterack: 12485085Swnj /* 12496211Swnj * Generate an ACK dropping incoming segment if it occupies 12506211Swnj * sequence space, where the ACK reflects our state. 12515085Swnj */ 125226057Skarels if (tiflags & TH_RST) 12535085Swnj goto drop; 125431749Skarels m_freem(m); 125531721Skarels tp->t_flags |= TF_ACKNOW; 125631721Skarels (void) tcp_output(tp); 12575231Swnj return; 12585085Swnj 12595085Swnj dropwithreset: 12605085Swnj /* 12615244Sroot * Generate a RST, dropping incoming segment. 12625085Swnj * Make ACK acceptable to originator of segment. 126357433Sandrew * Don't bother to respond if destination was broadcast/multicast. 12645085Swnj */ 126557433Sandrew if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) || 126658998Ssklower IN_MULTICAST(ti->ti_dst.s_addr)) 12675085Swnj goto drop; 12685085Swnj if (tiflags & TH_ACK) 126937320Skarels tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 12705085Swnj else { 12715085Swnj if (tiflags & TH_SYN) 12725085Swnj ti->ti_len++; 127337320Skarels tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 12746211Swnj TH_RST|TH_ACK); 12755085Swnj } 127610769Ssam /* destroy temporarily created socket */ 127710769Ssam if (dropsocket) 127810769Ssam (void) soabort(so); 12795231Swnj return; 12805085Swnj 12815065Swnj drop: 12825085Swnj /* 12835085Swnj * Drop space held by incoming segment and return. 12845085Swnj */ 12856303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 12866303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 12875065Swnj m_freem(m); 128810769Ssam /* destroy temporarily created socket */ 128910769Ssam if (dropsocket) 129010769Ssam (void) soabort(so); 12915267Sroot return; 129257947Ssklower #ifndef TUBA_INCLUDE 12935065Swnj } 12945065Swnj 129561335Sbostic void 129657433Sandrew tcp_dooptions(tp, cp, cnt, ti, ts_present, ts_val, ts_ecr) 12975440Swnj struct tcpcb *tp; 129857433Sandrew u_char *cp; 129957433Sandrew int cnt; 130017272Skarels struct tcpiphdr *ti; 130157433Sandrew int *ts_present; 130257433Sandrew u_long *ts_val, *ts_ecr; 13035419Swnj { 130444375Skarels u_short mss; 130557433Sandrew int opt, optlen; 13065419Swnj 13075440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 13085440Swnj opt = cp[0]; 13095440Swnj if (opt == TCPOPT_EOL) 13105440Swnj break; 13115440Swnj if (opt == TCPOPT_NOP) 13125440Swnj optlen = 1; 131312169Ssam else { 13145440Swnj optlen = cp[1]; 131512169Ssam if (optlen <= 0) 131612169Ssam break; 131712169Ssam } 13185440Swnj switch (opt) { 13195440Swnj 13205440Swnj default: 132144375Skarels continue; 13225440Swnj 13235440Swnj case TCPOPT_MAXSEG: 132457433Sandrew if (optlen != TCPOLEN_MAXSEG) 13255440Swnj continue; 132617272Skarels if (!(ti->ti_flags & TH_SYN)) 132717272Skarels continue; 132844375Skarels bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 132944375Skarels NTOHS(mss); 133044375Skarels (void) tcp_mss(tp, mss); /* sets t_maxseg */ 13315440Swnj break; 133257433Sandrew 133357433Sandrew case TCPOPT_WINDOW: 133457433Sandrew if (optlen != TCPOLEN_WINDOW) 133557433Sandrew continue; 133657433Sandrew if (!(ti->ti_flags & TH_SYN)) 133757433Sandrew continue; 133857433Sandrew tp->t_flags |= TF_RCVD_SCALE; 133957433Sandrew tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 134057433Sandrew break; 134157433Sandrew 134257433Sandrew case TCPOPT_TIMESTAMP: 134357433Sandrew if (optlen != TCPOLEN_TIMESTAMP) 134457433Sandrew continue; 134557433Sandrew *ts_present = 1; 134657433Sandrew bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val)); 134757433Sandrew NTOHL(*ts_val); 134857433Sandrew bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr)); 134957433Sandrew NTOHL(*ts_ecr); 135057433Sandrew 135157433Sandrew /* 135257433Sandrew * A timestamp received in a SYN makes 135357433Sandrew * it ok to send timestamp requests and replies. 135457433Sandrew */ 135557433Sandrew if (ti->ti_flags & TH_SYN) { 135657433Sandrew tp->t_flags |= TF_RCVD_TSTMP; 135757433Sandrew tp->ts_recent = *ts_val; 135857433Sandrew tp->ts_recent_age = tcp_now; 135957433Sandrew } 136057433Sandrew break; 13615419Swnj } 13625419Swnj } 13635419Swnj } 13645419Swnj 13655419Swnj /* 13665547Swnj * Pull out of band byte out of a segment so 13675547Swnj * it doesn't appear in the user's data queue. 13685547Swnj * It is still reflected in the segment length for 13695547Swnj * sequencing purposes. 13705547Swnj */ 137161335Sbostic void 137244375Skarels tcp_pulloutofband(so, ti, m) 13735547Swnj struct socket *so; 13745547Swnj struct tcpiphdr *ti; 137544375Skarels register struct mbuf *m; 13765547Swnj { 13776116Swnj int cnt = ti->ti_urp - 1; 13785547Swnj 13795547Swnj while (cnt >= 0) { 13805547Swnj if (m->m_len > cnt) { 13815547Swnj char *cp = mtod(m, caddr_t) + cnt; 13825547Swnj struct tcpcb *tp = sototcpcb(so); 13835547Swnj 13845547Swnj tp->t_iobc = *cp; 13855547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 13866161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 13875547Swnj m->m_len--; 13885547Swnj return; 13895547Swnj } 13905547Swnj cnt -= m->m_len; 13915547Swnj m = m->m_next; 13925547Swnj if (m == 0) 13935547Swnj break; 13945547Swnj } 13955547Swnj panic("tcp_pulloutofband"); 13965547Swnj } 13975547Swnj 13985547Swnj /* 139944375Skarels * Collect new round-trip time estimate 140044375Skarels * and update averages and current timeout. 140117272Skarels */ 140261335Sbostic void 140357433Sandrew tcp_xmit_timer(tp, rtt) 140423975Skarels register struct tcpcb *tp; 140561335Sbostic short rtt; 140617272Skarels { 140744375Skarels register short delta; 140844375Skarels 140944375Skarels tcpstat.tcps_rttupdated++; 141044375Skarels if (tp->t_srtt != 0) { 141144375Skarels /* 141244375Skarels * srtt is stored as fixed point with 3 bits after the 141344375Skarels * binary point (i.e., scaled by 8). The following magic 141444375Skarels * is equivalent to the smoothing algorithm in rfc793 with 141544375Skarels * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 141657433Sandrew * point). Adjust rtt to origin 0. 141744375Skarels */ 141857433Sandrew delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT); 141944375Skarels if ((tp->t_srtt += delta) <= 0) 142044375Skarels tp->t_srtt = 1; 142144375Skarels /* 142244375Skarels * We accumulate a smoothed rtt variance (actually, a 142344375Skarels * smoothed mean difference), then set the retransmit 142444375Skarels * timer to smoothed rtt + 4 times the smoothed variance. 142544375Skarels * rttvar is stored as fixed point with 2 bits after the 142644375Skarels * binary point (scaled by 4). The following is 142744375Skarels * equivalent to rfc793 smoothing with an alpha of .75 142844375Skarels * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 142944375Skarels * rfc793's wired-in beta. 143044375Skarels */ 143144375Skarels if (delta < 0) 143244375Skarels delta = -delta; 143344375Skarels delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 143444375Skarels if ((tp->t_rttvar += delta) <= 0) 143544375Skarels tp->t_rttvar = 1; 143644375Skarels } else { 143744375Skarels /* 143844375Skarels * No rtt measurement yet - use the unsmoothed rtt. 143944375Skarels * Set the variance to half the rtt (so our first 144052199Skarels * retransmit happens at 3*rtt). 144144375Skarels */ 144257433Sandrew tp->t_srtt = rtt << TCP_RTT_SHIFT; 144357433Sandrew tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 144444375Skarels } 144544375Skarels tp->t_rtt = 0; 144644375Skarels tp->t_rxtshift = 0; 144744375Skarels 144844375Skarels /* 144944375Skarels * the retransmit should happen at rtt + 4 * rttvar. 145044375Skarels * Because of the way we do the smoothing, srtt and rttvar 145144375Skarels * will each average +1/2 tick of bias. When we compute 145244375Skarels * the retransmit timer, we want 1/2 tick of rounding and 145344375Skarels * 1 extra tick because of +-1/2 tick uncertainty in the 145444375Skarels * firing of the timer. The bias will give us exactly the 145544375Skarels * 1.5 tick we need. But, because the bias is 145644375Skarels * statistical, we have to test that we don't drop below 145744375Skarels * the minimum feasible timer (which is 2 ticks). 145844375Skarels */ 145944375Skarels TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 146044375Skarels tp->t_rttmin, TCPTV_REXMTMAX); 146144375Skarels 146244375Skarels /* 146344375Skarels * We received an ack for a packet that wasn't retransmitted; 146444375Skarels * it is probably safe to discard any error indications we've 146544375Skarels * received recently. This isn't quite right, but close enough 146644375Skarels * for now (a route might have failed after we sent a segment, 146744375Skarels * and the return path might not be symmetrical). 146844375Skarels */ 146944375Skarels tp->t_softerror = 0; 147044375Skarels } 147144375Skarels 147244375Skarels /* 147344375Skarels * Determine a reasonable value for maxseg size. 147444375Skarels * If the route is known, check route for mtu. 147544375Skarels * If none, use an mss that can be handled on the outgoing 147644375Skarels * interface without forcing IP to fragment; if bigger than 147744375Skarels * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 147844375Skarels * to utilize large mbufs. If no route is found, route has no mtu, 147944375Skarels * or the destination isn't local, use a default, hopefully conservative 148044375Skarels * size (usually 512 or the default IP max size, but no more than the mtu 148144375Skarels * of the interface), as we can't discover anything about intervening 148244375Skarels * gateways or networks. We also initialize the congestion/slow start 148344375Skarels * window to be a single segment if the destination isn't local. 148444375Skarels * While looking at the routing entry, we also initialize other path-dependent 148544375Skarels * parameters from pre-set or cached values in the routing entry. 148644375Skarels */ 148761335Sbostic int 148844375Skarels tcp_mss(tp, offer) 148944375Skarels register struct tcpcb *tp; 149065606Sbostic u_int offer; 149144375Skarels { 149217272Skarels struct route *ro; 149344375Skarels register struct rtentry *rt; 149417272Skarels struct ifnet *ifp; 149544375Skarels register int rtt, mss; 149644375Skarels u_long bufsize; 149717272Skarels struct inpcb *inp; 149844375Skarels struct socket *so; 149965460Sbostic extern int tcp_mssdflt; 150017272Skarels 150117272Skarels inp = tp->t_inpcb; 150217272Skarels ro = &inp->inp_route; 150344375Skarels 150444375Skarels if ((rt = ro->ro_rt) == (struct rtentry *)0) { 150517272Skarels /* No route yet, so try to acquire one */ 150617272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 150717272Skarels ro->ro_dst.sa_family = AF_INET; 150837320Skarels ro->ro_dst.sa_len = sizeof(ro->ro_dst); 150917272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 151017272Skarels inp->inp_faddr; 151117272Skarels rtalloc(ro); 151217272Skarels } 151344375Skarels if ((rt = ro->ro_rt) == (struct rtentry *)0) 151457637Sandrew return (tcp_mssdflt); 151517272Skarels } 151644375Skarels ifp = rt->rt_ifp; 151744375Skarels so = inp->inp_socket; 151817272Skarels 151944375Skarels #ifdef RTV_MTU /* if route characteristics exist ... */ 152044375Skarels /* 152144375Skarels * While we're here, check if there's an initial rtt 152244375Skarels * or rttvar. Convert from the route-table units 152344375Skarels * to scaled multiples of the slow timeout timer. 152444375Skarels */ 152544375Skarels if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 152652199Skarels /* 152752199Skarels * XXX the lock bit for MTU indicates that the value 152852199Skarels * is also a minimum value; this is subject to time. 152952199Skarels */ 153052199Skarels if (rt->rt_rmx.rmx_locks & RTV_RTT) 153144375Skarels tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 153244375Skarels tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 153344375Skarels if (rt->rt_rmx.rmx_rttvar) 153444375Skarels tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 153544375Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 153644375Skarels else 153744375Skarels /* default variation is +- 1 rtt */ 153844375Skarels tp->t_rttvar = 153944375Skarels tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 154044375Skarels TCPT_RANGESET(tp->t_rxtcur, 154144375Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 154244375Skarels tp->t_rttmin, TCPTV_REXMTMAX); 154344375Skarels } 154444375Skarels /* 154544375Skarels * if there's an mtu associated with the route, use it 154644375Skarels */ 154744375Skarels if (rt->rt_rmx.rmx_mtu) 154857637Sandrew mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 154944375Skarels else 155044375Skarels #endif /* RTV_MTU */ 155144375Skarels { 155257637Sandrew mss = ifp->if_mtu - sizeof(struct tcpiphdr); 155331726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 155444375Skarels if (mss > MCLBYTES) 155544375Skarels mss &= ~(MCLBYTES-1); 155617272Skarels #else 155744375Skarels if (mss > MCLBYTES) 155844375Skarels mss = mss / MCLBYTES * MCLBYTES; 155917272Skarels #endif 156044375Skarels if (!in_localaddr(inp->inp_faddr)) 156157637Sandrew mss = min(mss, tcp_mssdflt); 156244375Skarels } 156344375Skarels /* 156444375Skarels * The current mss, t_maxseg, is initialized to the default value. 156544375Skarels * If we compute a smaller value, reduce the current mss. 156644375Skarels * If we compute a larger value, return it for use in sending 156744375Skarels * a max seg size option, but don't store it for use 156844375Skarels * unless we received an offer at least that large from peer. 156944375Skarels * However, do not accept offers under 32 bytes. 157044375Skarels */ 157144375Skarels if (offer) 157244375Skarels mss = min(mss, offer); 157344375Skarels mss = max(mss, 32); /* sanity */ 157444375Skarels if (mss < tp->t_maxseg || offer != 0) { 157544375Skarels /* 157644375Skarels * If there's a pipesize, change the socket buffer 157744375Skarels * to that size. Make the socket buffers an integral 157844375Skarels * number of mss units; if the mss is larger than 157944375Skarels * the socket buffer, decrease the mss. 158044375Skarels */ 158144375Skarels #ifdef RTV_SPIPE 158244375Skarels if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 158344375Skarels #endif 158444375Skarels bufsize = so->so_snd.sb_hiwat; 158544375Skarels if (bufsize < mss) 158644375Skarels mss = bufsize; 158744375Skarels else { 158856908Storek bufsize = roundup(bufsize, mss); 158957433Sandrew if (bufsize > sb_max) 159057433Sandrew bufsize = sb_max; 159156908Storek (void)sbreserve(&so->so_snd, bufsize); 159244375Skarels } 159344375Skarels tp->t_maxseg = mss; 159432098Skarels 159544375Skarels #ifdef RTV_RPIPE 159644375Skarels if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 159744375Skarels #endif 159844375Skarels bufsize = so->so_rcv.sb_hiwat; 159944375Skarels if (bufsize > mss) { 160056908Storek bufsize = roundup(bufsize, mss); 160157433Sandrew if (bufsize > sb_max) 160257433Sandrew bufsize = sb_max; 160356908Storek (void)sbreserve(&so->so_rcv, bufsize); 160444375Skarels } 160544375Skarels } 160632034Skarels tp->snd_cwnd = mss; 160744375Skarels 160844375Skarels #ifdef RTV_SSTHRESH 160944375Skarels if (rt->rt_rmx.rmx_ssthresh) { 161044375Skarels /* 161144375Skarels * There's some sort of gateway or interface 161244375Skarels * buffer limit on the path. Use this to set 161344375Skarels * the slow start threshhold, but set the 161444375Skarels * threshold to no less than 2*mss. 161544375Skarels */ 161644375Skarels tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 161744375Skarels } 161844375Skarels #endif /* RTV_MTU */ 161932034Skarels return (mss); 162017272Skarels } 162157947Ssklower #endif /* TUBA_INCLUDE */ 1622