123190Smckusick /* 266740Sbostic * 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*67870Smckusick * @(#)tcp_input.c 8.7 (Berkeley) 10/27/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; 19367588Smckusick u_char *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); 25867588Smckusick optp = mtod(m, u_char *) + 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; 30066740Sbostic ++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 */ 41566740Sbostic ++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 */ 45666740Sbostic ++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) || 52467588Smckusick IN_MULTICAST(ntohl(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 994*67870Smckusick * (maxseg * (maxseg / cwnd) per packet). 99517360Skarels */ 99632098Skarels { 99744375Skarels register u_int cw = tp->snd_cwnd; 99844375Skarels register u_int incr = tp->t_maxseg; 99932098Skarels 100044375Skarels if (cw > tp->snd_ssthresh) 1001*67870Smckusick incr = incr * incr / cw; 100257433Sandrew tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); 100332098Skarels } 10045307Sroot if (acked > so->so_snd.sb_cc) { 100515386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 100626386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 100731721Skarels ourfinisacked = 1; 10085307Sroot } else { 10096161Ssam sbdrop(&so->so_snd, acked); 10105307Sroot tp->snd_wnd -= acked; 101131721Skarels ourfinisacked = 0; 10125307Sroot } 101344375Skarels if (so->so_snd.sb_flags & SB_NOTIFY) 101444375Skarels sowwakeup(so); 10155231Swnj tp->snd_una = ti->ti_ack; 10165357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 10175357Sroot tp->snd_nxt = tp->snd_una; 10185162Swnj 10194601Swnj switch (tp->t_state) { 10204601Swnj 10215065Swnj /* 10225065Swnj * In FIN_WAIT_1 STATE in addition to the processing 10235065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 10245085Swnj * then enter FIN_WAIT_2. 10255065Swnj */ 10265065Swnj case TCPS_FIN_WAIT_1: 10275896Swnj if (ourfinisacked) { 10285896Swnj /* 10295896Swnj * If we can't receive any more 10305896Swnj * data, then closing user can proceed. 103124816Skarels * Starting the timer is contrary to the 103224816Skarels * specification, but if we don't get a FIN 103324816Skarels * we'll hang forever. 10345896Swnj */ 103524816Skarels if (so->so_state & SS_CANTRCVMORE) { 10365896Swnj soisdisconnected(so); 103733745Skarels tp->t_timer[TCPT_2MSL] = tcp_maxidle; 103824816Skarels } 10395085Swnj tp->t_state = TCPS_FIN_WAIT_2; 10405896Swnj } 10414601Swnj break; 10424601Swnj 10435065Swnj /* 10445065Swnj * In CLOSING STATE in addition to the processing for 10455065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 10465065Swnj * then enter the TIME-WAIT state, otherwise ignore 10475065Swnj * the segment. 10485065Swnj */ 10495065Swnj case TCPS_CLOSING: 10505244Sroot if (ourfinisacked) { 10515065Swnj tp->t_state = TCPS_TIME_WAIT; 10525244Sroot tcp_canceltimers(tp); 10535244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10545244Sroot soisdisconnected(so); 10555244Sroot } 10565244Sroot break; 10574601Swnj 10585065Swnj /* 105931743Skarels * In LAST_ACK, we may still be waiting for data to drain 106031743Skarels * and/or to be acked, as well as for the ack of our FIN. 106131743Skarels * If our FIN is now acknowledged, delete the TCB, 106231743Skarels * enter the closed state and return. 10635065Swnj */ 10645065Swnj case TCPS_LAST_ACK: 106531743Skarels if (ourfinisacked) { 106610394Ssam tp = tcp_close(tp); 106731743Skarels goto drop; 106831743Skarels } 106931743Skarels break; 10704601Swnj 10715065Swnj /* 10725065Swnj * In TIME_WAIT state the only thing that should arrive 10735065Swnj * is a retransmission of the remote FIN. Acknowledge 10745065Swnj * it and restart the finack timer. 10755065Swnj */ 10765065Swnj case TCPS_TIME_WAIT: 10775162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10785065Swnj goto dropafterack; 10794601Swnj } 10805085Swnj } 10814601Swnj 10825065Swnj step6: 10835065Swnj /* 10845244Sroot * Update window information. 108525939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 10865244Sroot */ 108725939Skarels if ((tiflags & TH_ACK) && 108825939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 10895391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 109057433Sandrew tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))) { 109130525Skarels /* keep track of pure window updates */ 109230525Skarels if (ti->ti_len == 0 && 109357433Sandrew tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) 109430525Skarels tcpstat.tcps_rcvwinupd++; 109557433Sandrew tp->snd_wnd = tiwin; 10965244Sroot tp->snd_wl1 = ti->ti_seq; 10975244Sroot tp->snd_wl2 = ti->ti_ack; 109825260Skarels if (tp->snd_wnd > tp->max_sndwnd) 109925260Skarels tp->max_sndwnd = tp->snd_wnd; 110026824Skarels needoutput = 1; 110126824Skarels } 11025244Sroot 11035244Sroot /* 11045547Swnj * Process segments with URG. 11055065Swnj */ 11067267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 11077267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 11085547Swnj /* 110925939Skarels * This is a kludge, but if we receive and accept 111013121Ssam * random urgent pointers, we'll crash in 111113121Ssam * soreceive. It's hard to imagine someone 111213121Ssam * actually wanting to send this much urgent data. 111312441Ssam */ 111457433Sandrew if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) { 111512441Ssam ti->ti_urp = 0; /* XXX */ 111612441Ssam tiflags &= ~TH_URG; /* XXX */ 111725939Skarels goto dodata; /* XXX */ 111812441Ssam } 111912441Ssam /* 11205547Swnj * If this segment advances the known urgent pointer, 11215547Swnj * then mark the data stream. This should not happen 11225547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 11235547Swnj * a FIN has been received from the remote side. 11245547Swnj * In these states we ignore the URG. 112527190Skarels * 112627190Skarels * According to RFC961 (Assigned Protocols), 112727190Skarels * the urgent pointer points to the last octet 112827190Skarels * of urgent data. We continue, however, 112927190Skarels * to consider it to indicate the first octet 113044375Skarels * of data past the urgent section as the original 113144375Skarels * spec states (in one of two places). 11325547Swnj */ 11335547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 11345547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 11355547Swnj so->so_oobmark = so->so_rcv.sb_cc + 11365547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 11375547Swnj if (so->so_oobmark == 0) 11385547Swnj so->so_state |= SS_RCVATMARK; 11398313Sroot sohasoutofband(so); 114024816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 11415440Swnj } 11425547Swnj /* 11435547Swnj * Remove out of band data so doesn't get presented to user. 11445547Swnj * This can happen independent of advancing the URG pointer, 11455547Swnj * but if two URG's are pending at once, some out-of-band 11465547Swnj * data may creep in... ick. 11475547Swnj */ 114844375Skarels if (ti->ti_urp <= ti->ti_len 114944375Skarels #ifdef SO_OOBINLINE 115044375Skarels && (so->so_options & SO_OOBINLINE) == 0 115144375Skarels #endif 115244375Skarels ) 115344375Skarels tcp_pulloutofband(so, ti, m); 115425939Skarels } else 115525939Skarels /* 115625939Skarels * If no out of band data is expected, 115725939Skarels * pull receive urgent pointer along 115825939Skarels * with the receive window. 115925939Skarels */ 116025939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 116125939Skarels tp->rcv_up = tp->rcv_nxt; 116225939Skarels dodata: /* XXX */ 11634601Swnj 11644601Swnj /* 11655065Swnj * Process the segment text, merging it into the TCP sequencing queue, 11665065Swnj * and arranging for acknowledgment of receipt if necessary. 11675065Swnj * This process logically involves adjusting tp->rcv_wnd as data 11685065Swnj * is presented to the user (this happens in tcp_usrreq.c, 11695065Swnj * case PRU_RCVD). If a FIN has already been received on this 11705065Swnj * connection then we just ignore the text. 11714601Swnj */ 117217946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 117317946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 117424816Skarels TCP_REASS(tp, ti, m, so, tiflags); 117525260Skarels /* 117625260Skarels * Note the amount of data that peer has sent into 117725260Skarels * our window, in order to estimate the sender's 117825260Skarels * buffer size. 117925260Skarels */ 118032098Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 11815244Sroot } else { 11824924Swnj m_freem(m); 11835263Swnj tiflags &= ~TH_FIN; 11845244Sroot } 11854601Swnj 11864601Swnj /* 11875263Swnj * If FIN is received ACK the FIN and let the user know 11885263Swnj * that the connection is closing. 11894601Swnj */ 11905263Swnj if (tiflags & TH_FIN) { 11915244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 11925244Sroot socantrcvmore(so); 11935244Sroot tp->t_flags |= TF_ACKNOW; 11945244Sroot tp->rcv_nxt++; 11955244Sroot } 11965065Swnj switch (tp->t_state) { 11974601Swnj 11985065Swnj /* 11995065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 12005065Swnj * enter the CLOSE_WAIT state. 12014884Swnj */ 12025065Swnj case TCPS_SYN_RECEIVED: 12035065Swnj case TCPS_ESTABLISHED: 12045065Swnj tp->t_state = TCPS_CLOSE_WAIT; 12055065Swnj break; 12064884Swnj 12075065Swnj /* 12085085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 12095085Swnj * enter the CLOSING state. 12104884Swnj */ 12115065Swnj case TCPS_FIN_WAIT_1: 12125085Swnj tp->t_state = TCPS_CLOSING; 12135065Swnj break; 12144601Swnj 12155065Swnj /* 12165065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 12175065Swnj * starting the time-wait timer, turning off the other 12185065Swnj * standard timers. 12195065Swnj */ 12205065Swnj case TCPS_FIN_WAIT_2: 12215244Sroot tp->t_state = TCPS_TIME_WAIT; 12225074Swnj tcp_canceltimers(tp); 12235162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 12245244Sroot soisdisconnected(so); 12255065Swnj break; 12265065Swnj 12274884Swnj /* 12285065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 12294884Swnj */ 12305065Swnj case TCPS_TIME_WAIT: 12315162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 12325065Swnj break; 12335085Swnj } 12344601Swnj } 12355267Sroot if (so->so_options & SO_DEBUG) 12365267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 12375085Swnj 12385085Swnj /* 12395085Swnj * Return any desired output. 12405085Swnj */ 124126824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 124225939Skarels (void) tcp_output(tp); 12435065Swnj return; 12445085Swnj 12455065Swnj dropafterack: 12465085Swnj /* 12476211Swnj * Generate an ACK dropping incoming segment if it occupies 12486211Swnj * sequence space, where the ACK reflects our state. 12495085Swnj */ 125026057Skarels if (tiflags & TH_RST) 12515085Swnj goto drop; 125231749Skarels m_freem(m); 125331721Skarels tp->t_flags |= TF_ACKNOW; 125431721Skarels (void) tcp_output(tp); 12555231Swnj return; 12565085Swnj 12575085Swnj dropwithreset: 12585085Swnj /* 12595244Sroot * Generate a RST, dropping incoming segment. 12605085Swnj * Make ACK acceptable to originator of segment. 126157433Sandrew * Don't bother to respond if destination was broadcast/multicast. 12625085Swnj */ 126357433Sandrew if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) || 126467588Smckusick IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 12655085Swnj goto drop; 12665085Swnj if (tiflags & TH_ACK) 126737320Skarels tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 12685085Swnj else { 12695085Swnj if (tiflags & TH_SYN) 12705085Swnj ti->ti_len++; 127137320Skarels tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 12726211Swnj TH_RST|TH_ACK); 12735085Swnj } 127410769Ssam /* destroy temporarily created socket */ 127510769Ssam if (dropsocket) 127610769Ssam (void) soabort(so); 12775231Swnj return; 12785085Swnj 12795065Swnj drop: 12805085Swnj /* 12815085Swnj * Drop space held by incoming segment and return. 12825085Swnj */ 12836303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 12846303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 12855065Swnj m_freem(m); 128610769Ssam /* destroy temporarily created socket */ 128710769Ssam if (dropsocket) 128810769Ssam (void) soabort(so); 12895267Sroot return; 129057947Ssklower #ifndef TUBA_INCLUDE 12915065Swnj } 12925065Swnj 129361335Sbostic void 129457433Sandrew tcp_dooptions(tp, cp, cnt, ti, ts_present, ts_val, ts_ecr) 12955440Swnj struct tcpcb *tp; 129657433Sandrew u_char *cp; 129757433Sandrew int cnt; 129817272Skarels struct tcpiphdr *ti; 129957433Sandrew int *ts_present; 130057433Sandrew u_long *ts_val, *ts_ecr; 13015419Swnj { 130244375Skarels u_short mss; 130357433Sandrew int opt, optlen; 13045419Swnj 13055440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 13065440Swnj opt = cp[0]; 13075440Swnj if (opt == TCPOPT_EOL) 13085440Swnj break; 13095440Swnj if (opt == TCPOPT_NOP) 13105440Swnj optlen = 1; 131112169Ssam else { 13125440Swnj optlen = cp[1]; 131312169Ssam if (optlen <= 0) 131412169Ssam break; 131512169Ssam } 13165440Swnj switch (opt) { 13175440Swnj 13185440Swnj default: 131944375Skarels continue; 13205440Swnj 13215440Swnj case TCPOPT_MAXSEG: 132257433Sandrew if (optlen != TCPOLEN_MAXSEG) 13235440Swnj continue; 132417272Skarels if (!(ti->ti_flags & TH_SYN)) 132517272Skarels continue; 132644375Skarels bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 132744375Skarels NTOHS(mss); 132844375Skarels (void) tcp_mss(tp, mss); /* sets t_maxseg */ 13295440Swnj break; 133057433Sandrew 133157433Sandrew case TCPOPT_WINDOW: 133257433Sandrew if (optlen != TCPOLEN_WINDOW) 133357433Sandrew continue; 133457433Sandrew if (!(ti->ti_flags & TH_SYN)) 133557433Sandrew continue; 133657433Sandrew tp->t_flags |= TF_RCVD_SCALE; 133757433Sandrew tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 133857433Sandrew break; 133957433Sandrew 134057433Sandrew case TCPOPT_TIMESTAMP: 134157433Sandrew if (optlen != TCPOLEN_TIMESTAMP) 134257433Sandrew continue; 134357433Sandrew *ts_present = 1; 134457433Sandrew bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val)); 134557433Sandrew NTOHL(*ts_val); 134657433Sandrew bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr)); 134757433Sandrew NTOHL(*ts_ecr); 134857433Sandrew 134957433Sandrew /* 135057433Sandrew * A timestamp received in a SYN makes 135157433Sandrew * it ok to send timestamp requests and replies. 135257433Sandrew */ 135357433Sandrew if (ti->ti_flags & TH_SYN) { 135457433Sandrew tp->t_flags |= TF_RCVD_TSTMP; 135557433Sandrew tp->ts_recent = *ts_val; 135657433Sandrew tp->ts_recent_age = tcp_now; 135757433Sandrew } 135857433Sandrew break; 13595419Swnj } 13605419Swnj } 13615419Swnj } 13625419Swnj 13635419Swnj /* 13645547Swnj * Pull out of band byte out of a segment so 13655547Swnj * it doesn't appear in the user's data queue. 13665547Swnj * It is still reflected in the segment length for 13675547Swnj * sequencing purposes. 13685547Swnj */ 136961335Sbostic void 137044375Skarels tcp_pulloutofband(so, ti, m) 13715547Swnj struct socket *so; 13725547Swnj struct tcpiphdr *ti; 137344375Skarels register struct mbuf *m; 13745547Swnj { 13756116Swnj int cnt = ti->ti_urp - 1; 13765547Swnj 13775547Swnj while (cnt >= 0) { 13785547Swnj if (m->m_len > cnt) { 13795547Swnj char *cp = mtod(m, caddr_t) + cnt; 13805547Swnj struct tcpcb *tp = sototcpcb(so); 13815547Swnj 13825547Swnj tp->t_iobc = *cp; 13835547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 13846161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 13855547Swnj m->m_len--; 13865547Swnj return; 13875547Swnj } 13885547Swnj cnt -= m->m_len; 13895547Swnj m = m->m_next; 13905547Swnj if (m == 0) 13915547Swnj break; 13925547Swnj } 13935547Swnj panic("tcp_pulloutofband"); 13945547Swnj } 13955547Swnj 13965547Swnj /* 139744375Skarels * Collect new round-trip time estimate 139844375Skarels * and update averages and current timeout. 139917272Skarels */ 140061335Sbostic void 140157433Sandrew tcp_xmit_timer(tp, rtt) 140223975Skarels register struct tcpcb *tp; 140361335Sbostic short rtt; 140417272Skarels { 140544375Skarels register short delta; 140644375Skarels 140744375Skarels tcpstat.tcps_rttupdated++; 140844375Skarels if (tp->t_srtt != 0) { 140944375Skarels /* 141044375Skarels * srtt is stored as fixed point with 3 bits after the 141144375Skarels * binary point (i.e., scaled by 8). The following magic 141244375Skarels * is equivalent to the smoothing algorithm in rfc793 with 141344375Skarels * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 141457433Sandrew * point). Adjust rtt to origin 0. 141544375Skarels */ 141657433Sandrew delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT); 141744375Skarels if ((tp->t_srtt += delta) <= 0) 141844375Skarels tp->t_srtt = 1; 141944375Skarels /* 142044375Skarels * We accumulate a smoothed rtt variance (actually, a 142144375Skarels * smoothed mean difference), then set the retransmit 142244375Skarels * timer to smoothed rtt + 4 times the smoothed variance. 142344375Skarels * rttvar is stored as fixed point with 2 bits after the 142444375Skarels * binary point (scaled by 4). The following is 142544375Skarels * equivalent to rfc793 smoothing with an alpha of .75 142644375Skarels * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 142744375Skarels * rfc793's wired-in beta. 142844375Skarels */ 142944375Skarels if (delta < 0) 143044375Skarels delta = -delta; 143144375Skarels delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 143244375Skarels if ((tp->t_rttvar += delta) <= 0) 143344375Skarels tp->t_rttvar = 1; 143444375Skarels } else { 143544375Skarels /* 143644375Skarels * No rtt measurement yet - use the unsmoothed rtt. 143744375Skarels * Set the variance to half the rtt (so our first 143852199Skarels * retransmit happens at 3*rtt). 143944375Skarels */ 144057433Sandrew tp->t_srtt = rtt << TCP_RTT_SHIFT; 144157433Sandrew tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 144244375Skarels } 144344375Skarels tp->t_rtt = 0; 144444375Skarels tp->t_rxtshift = 0; 144544375Skarels 144644375Skarels /* 144744375Skarels * the retransmit should happen at rtt + 4 * rttvar. 144844375Skarels * Because of the way we do the smoothing, srtt and rttvar 144944375Skarels * will each average +1/2 tick of bias. When we compute 145044375Skarels * the retransmit timer, we want 1/2 tick of rounding and 145144375Skarels * 1 extra tick because of +-1/2 tick uncertainty in the 145244375Skarels * firing of the timer. The bias will give us exactly the 145344375Skarels * 1.5 tick we need. But, because the bias is 145444375Skarels * statistical, we have to test that we don't drop below 145544375Skarels * the minimum feasible timer (which is 2 ticks). 145644375Skarels */ 145744375Skarels TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 145844375Skarels tp->t_rttmin, TCPTV_REXMTMAX); 145944375Skarels 146044375Skarels /* 146144375Skarels * We received an ack for a packet that wasn't retransmitted; 146244375Skarels * it is probably safe to discard any error indications we've 146344375Skarels * received recently. This isn't quite right, but close enough 146444375Skarels * for now (a route might have failed after we sent a segment, 146544375Skarels * and the return path might not be symmetrical). 146644375Skarels */ 146744375Skarels tp->t_softerror = 0; 146844375Skarels } 146944375Skarels 147044375Skarels /* 147144375Skarels * Determine a reasonable value for maxseg size. 147244375Skarels * If the route is known, check route for mtu. 147344375Skarels * If none, use an mss that can be handled on the outgoing 147444375Skarels * interface without forcing IP to fragment; if bigger than 147544375Skarels * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 147644375Skarels * to utilize large mbufs. If no route is found, route has no mtu, 147744375Skarels * or the destination isn't local, use a default, hopefully conservative 147844375Skarels * size (usually 512 or the default IP max size, but no more than the mtu 147944375Skarels * of the interface), as we can't discover anything about intervening 148044375Skarels * gateways or networks. We also initialize the congestion/slow start 148144375Skarels * window to be a single segment if the destination isn't local. 148244375Skarels * While looking at the routing entry, we also initialize other path-dependent 148344375Skarels * parameters from pre-set or cached values in the routing entry. 148444375Skarels */ 148561335Sbostic int 148644375Skarels tcp_mss(tp, offer) 148744375Skarels register struct tcpcb *tp; 148865606Sbostic u_int offer; 148944375Skarels { 149017272Skarels struct route *ro; 149144375Skarels register struct rtentry *rt; 149217272Skarels struct ifnet *ifp; 149344375Skarels register int rtt, mss; 149444375Skarels u_long bufsize; 149517272Skarels struct inpcb *inp; 149644375Skarels struct socket *so; 149765460Sbostic extern int tcp_mssdflt; 149817272Skarels 149917272Skarels inp = tp->t_inpcb; 150017272Skarels ro = &inp->inp_route; 150144375Skarels 150244375Skarels if ((rt = ro->ro_rt) == (struct rtentry *)0) { 150317272Skarels /* No route yet, so try to acquire one */ 150417272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 150517272Skarels ro->ro_dst.sa_family = AF_INET; 150637320Skarels ro->ro_dst.sa_len = sizeof(ro->ro_dst); 150717272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 150817272Skarels inp->inp_faddr; 150917272Skarels rtalloc(ro); 151017272Skarels } 151144375Skarels if ((rt = ro->ro_rt) == (struct rtentry *)0) 151257637Sandrew return (tcp_mssdflt); 151317272Skarels } 151444375Skarels ifp = rt->rt_ifp; 151544375Skarels so = inp->inp_socket; 151617272Skarels 151744375Skarels #ifdef RTV_MTU /* if route characteristics exist ... */ 151844375Skarels /* 151944375Skarels * While we're here, check if there's an initial rtt 152044375Skarels * or rttvar. Convert from the route-table units 152144375Skarels * to scaled multiples of the slow timeout timer. 152244375Skarels */ 152344375Skarels if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 152452199Skarels /* 152552199Skarels * XXX the lock bit for MTU indicates that the value 152652199Skarels * is also a minimum value; this is subject to time. 152752199Skarels */ 152852199Skarels if (rt->rt_rmx.rmx_locks & RTV_RTT) 152944375Skarels tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 153044375Skarels tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 153144375Skarels if (rt->rt_rmx.rmx_rttvar) 153244375Skarels tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 153344375Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 153444375Skarels else 153544375Skarels /* default variation is +- 1 rtt */ 153644375Skarels tp->t_rttvar = 153744375Skarels tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 153844375Skarels TCPT_RANGESET(tp->t_rxtcur, 153944375Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 154044375Skarels tp->t_rttmin, TCPTV_REXMTMAX); 154144375Skarels } 154244375Skarels /* 154344375Skarels * if there's an mtu associated with the route, use it 154444375Skarels */ 154544375Skarels if (rt->rt_rmx.rmx_mtu) 154657637Sandrew mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 154744375Skarels else 154844375Skarels #endif /* RTV_MTU */ 154944375Skarels { 155057637Sandrew mss = ifp->if_mtu - sizeof(struct tcpiphdr); 155131726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 155244375Skarels if (mss > MCLBYTES) 155344375Skarels mss &= ~(MCLBYTES-1); 155417272Skarels #else 155544375Skarels if (mss > MCLBYTES) 155644375Skarels mss = mss / MCLBYTES * MCLBYTES; 155717272Skarels #endif 155844375Skarels if (!in_localaddr(inp->inp_faddr)) 155957637Sandrew mss = min(mss, tcp_mssdflt); 156044375Skarels } 156144375Skarels /* 156244375Skarels * The current mss, t_maxseg, is initialized to the default value. 156344375Skarels * If we compute a smaller value, reduce the current mss. 156444375Skarels * If we compute a larger value, return it for use in sending 156544375Skarels * a max seg size option, but don't store it for use 156644375Skarels * unless we received an offer at least that large from peer. 156744375Skarels * However, do not accept offers under 32 bytes. 156844375Skarels */ 156944375Skarels if (offer) 157044375Skarels mss = min(mss, offer); 157144375Skarels mss = max(mss, 32); /* sanity */ 157244375Skarels if (mss < tp->t_maxseg || offer != 0) { 157344375Skarels /* 157444375Skarels * If there's a pipesize, change the socket buffer 157544375Skarels * to that size. Make the socket buffers an integral 157644375Skarels * number of mss units; if the mss is larger than 157744375Skarels * the socket buffer, decrease the mss. 157844375Skarels */ 157944375Skarels #ifdef RTV_SPIPE 158044375Skarels if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 158144375Skarels #endif 158244375Skarels bufsize = so->so_snd.sb_hiwat; 158344375Skarels if (bufsize < mss) 158444375Skarels mss = bufsize; 158544375Skarels else { 158656908Storek bufsize = roundup(bufsize, mss); 158757433Sandrew if (bufsize > sb_max) 158857433Sandrew bufsize = sb_max; 158956908Storek (void)sbreserve(&so->so_snd, bufsize); 159044375Skarels } 159144375Skarels tp->t_maxseg = mss; 159232098Skarels 159344375Skarels #ifdef RTV_RPIPE 159444375Skarels if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 159544375Skarels #endif 159644375Skarels bufsize = so->so_rcv.sb_hiwat; 159744375Skarels if (bufsize > mss) { 159856908Storek bufsize = roundup(bufsize, mss); 159957433Sandrew if (bufsize > sb_max) 160057433Sandrew bufsize = sb_max; 160156908Storek (void)sbreserve(&so->so_rcv, bufsize); 160244375Skarels } 160344375Skarels } 160432034Skarels tp->snd_cwnd = mss; 160544375Skarels 160644375Skarels #ifdef RTV_SSTHRESH 160744375Skarels if (rt->rt_rmx.rmx_ssthresh) { 160844375Skarels /* 160944375Skarels * There's some sort of gateway or interface 161044375Skarels * buffer limit on the path. Use this to set 161144375Skarels * the slow start threshhold, but set the 161244375Skarels * threshold to no less than 2*mss. 161344375Skarels */ 161444375Skarels tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 161544375Skarels } 161644375Skarels #endif /* RTV_MTU */ 161732034Skarels return (mss); 161817272Skarels } 161957947Ssklower #endif /* TUBA_INCLUDE */ 1620