123190Smckusick /* 263218Sbostic * Copyright (c) 1982, 1986, 1988, 1990, 1993 363218Sbostic * The Regents of the University of California. All rights reserved. 423190Smckusick * 544485Sbostic * %sccs.include.redist.c% 632787Sbostic * 7*65606Sbostic * @(#)tcp_input.c 8.4 (Berkeley) 01/11/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; 3744375Skarels int tcppredack; /* XXX debugging: times hdr predict ok for acks */ 3844375Skarels int tcppreddat; /* XXX # times header prediction ok for data packets */ 3944375Skarels int tcppcbcachemiss; 405267Sroot struct tcpiphdr tcp_saveti; 4144375Skarels struct inpcb *tcp_last_inpcb = &tcb; 424601Swnj 4357947Ssklower extern u_long sb_max; 4457947Ssklower 4557947Ssklower #endif /* TUBA_INCLUDE */ 4657433Sandrew #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ) 4757433Sandrew 4857433Sandrew /* for modulo comparisons of timestamps */ 4957433Sandrew #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0) 5057433Sandrew #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0) 5157433Sandrew 5224816Skarels 535065Swnj /* 5424816Skarels * Insert segment ti into reassembly queue of tcp with 5524816Skarels * control block tp. Return TH_FIN if reassembly now includes 5624816Skarels * a segment with FIN. The macro form does the common case inline 5724816Skarels * (segment is the next to be received on an established connection, 5824816Skarels * and the queue is empty), avoiding linkage into and removal 5924816Skarels * from the queue and repetition of various conversions. 6034278Skarels * Set DELACK for segments received in order, but ack immediately 6134278Skarels * when segments are out of order (so fast retransmit can work). 6224816Skarels */ 6324816Skarels #define TCP_REASS(tp, ti, m, so, flags) { \ 6424816Skarels if ((ti)->ti_seq == (tp)->rcv_nxt && \ 6524816Skarels (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 6624816Skarels (tp)->t_state == TCPS_ESTABLISHED) { \ 6734278Skarels tp->t_flags |= TF_DELACK; \ 6824816Skarels (tp)->rcv_nxt += (ti)->ti_len; \ 6924816Skarels flags = (ti)->ti_flags & TH_FIN; \ 7030525Skarels tcpstat.tcps_rcvpack++;\ 7130525Skarels tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 7224816Skarels sbappend(&(so)->so_rcv, (m)); \ 7324816Skarels sorwakeup(so); \ 7434278Skarels } else { \ 7544375Skarels (flags) = tcp_reass((tp), (ti), (m)); \ 7634278Skarels tp->t_flags |= TF_ACKNOW; \ 7734278Skarels } \ 7824816Skarels } 7957947Ssklower #ifndef TUBA_INCLUDE 8024816Skarels 8161335Sbostic int 8244375Skarels tcp_reass(tp, ti, m) 8324816Skarels register struct tcpcb *tp; 8424816Skarels register struct tcpiphdr *ti; 8544375Skarels struct mbuf *m; 8624816Skarels { 8724816Skarels register struct tcpiphdr *q; 8824816Skarels struct socket *so = tp->t_inpcb->inp_socket; 8924816Skarels int flags; 9024816Skarels 9124816Skarels /* 9224816Skarels * Call with ti==0 after become established to 9324816Skarels * force pre-ESTABLISHED data up to user socket. 9424816Skarels */ 9524816Skarels if (ti == 0) 9624816Skarels goto present; 9724816Skarels 9824816Skarels /* 9924816Skarels * Find a segment which begins after this one does. 10024816Skarels */ 10124816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 10224816Skarels q = (struct tcpiphdr *)q->ti_next) 10324816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq)) 10424816Skarels break; 10524816Skarels 10624816Skarels /* 10724816Skarels * If there is a preceding segment, it may provide some of 10824816Skarels * our data already. If so, drop the data from the incoming 10924816Skarels * segment. If it provides all of our data, drop us. 11024816Skarels */ 11124816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 11224816Skarels register int i; 11324816Skarels q = (struct tcpiphdr *)q->ti_prev; 11424816Skarels /* conversion to int (in i) handles seq wraparound */ 11524816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq; 11624816Skarels if (i > 0) { 11730525Skarels if (i >= ti->ti_len) { 11830525Skarels tcpstat.tcps_rcvduppack++; 11930525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 12044375Skarels m_freem(m); 12144375Skarels return (0); 12230525Skarels } 12344375Skarels m_adj(m, i); 12424816Skarels ti->ti_len -= i; 12524816Skarels ti->ti_seq += i; 12624816Skarels } 12724816Skarels q = (struct tcpiphdr *)(q->ti_next); 12824816Skarels } 12930525Skarels tcpstat.tcps_rcvoopack++; 13030525Skarels tcpstat.tcps_rcvoobyte += ti->ti_len; 13144375Skarels REASS_MBUF(ti) = m; /* XXX */ 13224816Skarels 13324816Skarels /* 13424816Skarels * While we overlap succeeding segments trim them or, 13524816Skarels * if they are completely covered, dequeue them. 13624816Skarels */ 13724816Skarels while (q != (struct tcpiphdr *)tp) { 13824816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 13924816Skarels if (i <= 0) 14024816Skarels break; 14124816Skarels if (i < q->ti_len) { 14224816Skarels q->ti_seq += i; 14324816Skarels q->ti_len -= i; 14444375Skarels m_adj(REASS_MBUF(q), i); 14524816Skarels break; 14624816Skarels } 14724816Skarels q = (struct tcpiphdr *)q->ti_next; 14844375Skarels m = REASS_MBUF((struct tcpiphdr *)q->ti_prev); 14924816Skarels remque(q->ti_prev); 15024816Skarels m_freem(m); 15124816Skarels } 15224816Skarels 15324816Skarels /* 15424816Skarels * Stick new segment in its place. 15524816Skarels */ 15624816Skarels insque(ti, q->ti_prev); 15724816Skarels 15824816Skarels present: 15924816Skarels /* 16024816Skarels * Present data to user, advancing rcv_nxt through 16124816Skarels * completed sequence space. 16224816Skarels */ 16324816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 16424816Skarels return (0); 16524816Skarels ti = tp->seg_next; 16624816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 16724816Skarels return (0); 16824816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 16924816Skarels return (0); 17024816Skarels do { 17124816Skarels tp->rcv_nxt += ti->ti_len; 17224816Skarels flags = ti->ti_flags & TH_FIN; 17324816Skarels remque(ti); 17444375Skarels m = REASS_MBUF(ti); 17524816Skarels ti = (struct tcpiphdr *)ti->ti_next; 17624816Skarels if (so->so_state & SS_CANTRCVMORE) 17724816Skarels m_freem(m); 17824816Skarels else 17924816Skarels sbappend(&so->so_rcv, m); 18024816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 18124816Skarels sorwakeup(so); 18224816Skarels return (flags); 18324816Skarels } 18424816Skarels 18524816Skarels /* 1865065Swnj * TCP input routine, follows pages 65-76 of the 1875065Swnj * protocol specification dated September, 1981 very closely. 1885065Swnj */ 18961335Sbostic void 19037320Skarels tcp_input(m, iphlen) 19137320Skarels register struct mbuf *m; 19237320Skarels int iphlen; 1934601Swnj { 1944924Swnj register struct tcpiphdr *ti; 19544375Skarels register struct inpcb *inp; 19657433Sandrew caddr_t optp = NULL; 19757433Sandrew int optlen; 1984924Swnj int len, tlen, off; 1995391Swnj register struct tcpcb *tp = 0; 2004924Swnj register int tiflags; 2014803Swnj struct socket *so; 20231721Skarels int todrop, acked, ourfinisacked, needoutput = 0; 2035267Sroot short ostate; 2046028Sroot struct in_addr laddr; 20510769Ssam int dropsocket = 0; 20630525Skarels int iss = 0; 20757433Sandrew u_long tiwin, ts_val, ts_ecr; 20857433Sandrew int ts_present = 0; 2094924Swnj 21030525Skarels tcpstat.tcps_rcvtotal++; 2114924Swnj /* 2125244Sroot * Get IP and TCP header together in first mbuf. 2135244Sroot * Note: IP leaves IP header in first mbuf. 2144924Swnj */ 2155020Sroot ti = mtod(m, struct tcpiphdr *); 21637320Skarels if (iphlen > sizeof (struct ip)) 21737320Skarels ip_stripoptions(m, (struct mbuf *)0); 21844375Skarels if (m->m_len < sizeof (struct tcpiphdr)) { 2195307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 22030525Skarels tcpstat.tcps_rcvshort++; 2215307Sroot return; 2225085Swnj } 2235085Swnj ti = mtod(m, struct tcpiphdr *); 2245085Swnj } 2254601Swnj 2264601Swnj /* 2275244Sroot * Checksum extended TCP header and data. 2284601Swnj */ 2294924Swnj tlen = ((struct ip *)ti)->ip_len; 2304924Swnj len = sizeof (struct ip) + tlen; 23137320Skarels ti->ti_next = ti->ti_prev = 0; 23237320Skarels ti->ti_x1 = 0; 23337320Skarels ti->ti_len = (u_short)tlen; 23444375Skarels HTONS(ti->ti_len); 23537320Skarels if (ti->ti_sum = in_cksum(m, len)) { 23637320Skarels tcpstat.tcps_rcvbadsum++; 23737320Skarels goto drop; 2384601Swnj } 23957947Ssklower #endif /* TUBA_INCLUDE */ 2404601Swnj 2414601Swnj /* 2425244Sroot * Check that TCP offset makes sense, 24344375Skarels * pull out TCP options and adjust length. XXX 2444601Swnj */ 2454924Swnj off = ti->ti_off << 2; 2465231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 24730525Skarels tcpstat.tcps_rcvbadoff++; 2485085Swnj goto drop; 2494924Swnj } 2506211Swnj tlen -= off; 2516211Swnj ti->ti_len = tlen; 2525440Swnj if (off > sizeof (struct tcphdr)) { 25324816Skarels if (m->m_len < sizeof(struct ip) + off) { 25424816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 25530525Skarels tcpstat.tcps_rcvshort++; 25624816Skarels return; 25724816Skarels } 25824816Skarels ti = mtod(m, struct tcpiphdr *); 2595440Swnj } 26057433Sandrew optlen = off - sizeof (struct tcphdr); 26157433Sandrew optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 26257433Sandrew /* 26357433Sandrew * Do quick retrieval of timestamp options ("options 26457433Sandrew * prediction?"). If timestamp is the only option and it's 26557433Sandrew * formatted as recommended in RFC 1323 appendix A, we 26657433Sandrew * quickly get the values now and not bother calling 26757433Sandrew * tcp_dooptions(), etc. 26857433Sandrew */ 26957433Sandrew if ((optlen == TCPOLEN_TSTAMP_APPA || 27057433Sandrew (optlen > TCPOLEN_TSTAMP_APPA && 27157433Sandrew optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && 27257433Sandrew *(u_long *)optp == htonl(TCPOPT_TSTAMP_HDR) && 27357433Sandrew (ti->ti_flags & TH_SYN) == 0) { 27457433Sandrew ts_present = 1; 27557433Sandrew ts_val = ntohl(*(u_long *)(optp + 4)); 27657433Sandrew ts_ecr = ntohl(*(u_long *)(optp + 8)); 27757433Sandrew optp = NULL; /* we've parsed the options */ 2785440Swnj } 2795440Swnj } 2805065Swnj tiflags = ti->ti_flags; 2814924Swnj 2826093Sroot /* 2835244Sroot * Convert TCP protocol specific fields to host format. 2845085Swnj */ 28544375Skarels NTOHL(ti->ti_seq); 28644375Skarels NTOHL(ti->ti_ack); 28744375Skarels NTOHS(ti->ti_win); 28844375Skarels NTOHS(ti->ti_urp); 2895085Swnj 2905085Swnj /* 2918271Sroot * Locate pcb for segment. 2924924Swnj */ 29330525Skarels findpcb: 29444375Skarels inp = tcp_last_inpcb; 29544375Skarels if (inp->inp_lport != ti->ti_dport || 29644375Skarels inp->inp_fport != ti->ti_sport || 29744375Skarels inp->inp_faddr.s_addr != ti->ti_src.s_addr || 29844375Skarels inp->inp_laddr.s_addr != ti->ti_dst.s_addr) { 29944375Skarels inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport, 30044375Skarels ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD); 30144375Skarels if (inp) 30244375Skarels tcp_last_inpcb = inp; 30344375Skarels ++tcppcbcachemiss; 30444375Skarels } 3055065Swnj 3065065Swnj /* 3075065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 3085244Sroot * all data in the incoming segment is discarded. 30932098Skarels * If the TCB exists but is in CLOSED state, it is embryonic, 31032098Skarels * but should either do a listen or a connect soon. 3115065Swnj */ 3125300Sroot if (inp == 0) 3135085Swnj goto dropwithreset; 3145065Swnj tp = intotcpcb(inp); 3155300Sroot if (tp == 0) 3165085Swnj goto dropwithreset; 31732098Skarels if (tp->t_state == TCPS_CLOSED) 31832098Skarels goto drop; 31957433Sandrew 32057433Sandrew /* Unscale the window into a 32-bit value. */ 32157433Sandrew if ((tiflags & TH_SYN) == 0) 32257433Sandrew tiwin = ti->ti_win << tp->snd_scale; 32357433Sandrew else 32457433Sandrew tiwin = ti->ti_win; 32557433Sandrew 3265109Swnj so = inp->inp_socket; 32744375Skarels if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 32844375Skarels if (so->so_options & SO_DEBUG) { 32944375Skarels ostate = tp->t_state; 33044375Skarels tcp_saveti = *ti; 33144375Skarels } 33244375Skarels if (so->so_options & SO_ACCEPTCONN) { 33344375Skarels so = sonewconn(so, 0); 33444375Skarels if (so == 0) 33544375Skarels goto drop; 33644375Skarels /* 33744375Skarels * This is ugly, but .... 33844375Skarels * 33944375Skarels * Mark socket as temporary until we're 34044375Skarels * committed to keeping it. The code at 34144375Skarels * ``drop'' and ``dropwithreset'' check the 34244375Skarels * flag dropsocket to see if the temporary 34344375Skarels * socket created here should be discarded. 34444375Skarels * We mark the socket as discardable until 34544375Skarels * we're committed to it below in TCPS_LISTEN. 34644375Skarels */ 34744375Skarels dropsocket++; 34844375Skarels inp = (struct inpcb *)so->so_pcb; 34944375Skarels inp->inp_laddr = ti->ti_dst; 35044375Skarels inp->inp_lport = ti->ti_dport; 35144375Skarels #if BSD>=43 35244375Skarels inp->inp_options = ip_srcroute(); 35344375Skarels #endif 35444375Skarels tp = intotcpcb(inp); 35544375Skarels tp->t_state = TCPS_LISTEN; 35657433Sandrew 35757433Sandrew /* Compute proper scaling value from buffer space 35857433Sandrew */ 35957433Sandrew while (tp->request_r_scale < TCP_MAX_WINSHIFT && 36057433Sandrew TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat) 36157433Sandrew tp->request_r_scale++; 36244375Skarels } 3635267Sroot } 3644601Swnj 3654601Swnj /* 3665162Swnj * Segment received on connection. 3675162Swnj * Reset idle time and keep-alive timer. 3685162Swnj */ 3695162Swnj tp->t_idle = 0; 37033745Skarels tp->t_timer[TCPT_KEEP] = tcp_keepidle; 3715162Swnj 3725162Swnj /* 37317272Skarels * Process options if not in LISTEN state, 37417272Skarels * else do it below (after getting remote address). 3755440Swnj */ 37657433Sandrew if (optp && tp->t_state != TCPS_LISTEN) 37757433Sandrew tcp_dooptions(tp, optp, optlen, ti, 37857433Sandrew &ts_present, &ts_val, &ts_ecr); 37957433Sandrew 38044375Skarels /* 38144375Skarels * Header prediction: check for the two common cases 38244375Skarels * of a uni-directional data xfer. If the packet has 38344375Skarels * no control flags, is in-sequence, the window didn't 38444375Skarels * change and we're not retransmitting, it's a 38544375Skarels * candidate. If the length is zero and the ack moved 38644375Skarels * forward, we're the sender side of the xfer. Just 38744375Skarels * free the data acked & wake any higher level process 38844375Skarels * that was blocked waiting for space. If the length 38944375Skarels * is non-zero and the ack didn't move, we're the 39044375Skarels * receiver side. If we're getting packets in-order 39144375Skarels * (the reassembly queue is empty), add the data to 39244375Skarels * the socket buffer and note that we need a delayed ack. 39344375Skarels */ 39444375Skarels if (tp->t_state == TCPS_ESTABLISHED && 39544375Skarels (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 39657433Sandrew (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && 39744375Skarels ti->ti_seq == tp->rcv_nxt && 39857433Sandrew tiwin && tiwin == tp->snd_wnd && 39944375Skarels tp->snd_nxt == tp->snd_max) { 40057433Sandrew 40157433Sandrew /* 40257433Sandrew * If last ACK falls within this segment's sequence numbers, 40357433Sandrew * record the timestamp. 40457433Sandrew */ 40557433Sandrew if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) && 40657433Sandrew SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) { 40757433Sandrew tp->ts_recent_age = tcp_now; 40857433Sandrew tp->ts_recent = ts_val; 40957433Sandrew } 41057433Sandrew 41144375Skarels if (ti->ti_len == 0) { 41244375Skarels if (SEQ_GT(ti->ti_ack, tp->snd_una) && 41344375Skarels SEQ_LEQ(ti->ti_ack, tp->snd_max) && 41444375Skarels tp->snd_cwnd >= tp->snd_wnd) { 41544375Skarels /* 41644375Skarels * this is a pure ack for outstanding data. 41744375Skarels */ 41844375Skarels ++tcppredack; 41957433Sandrew if (ts_present) 42057433Sandrew tcp_xmit_timer(tp, tcp_now-ts_ecr+1); 42157433Sandrew else if (tp->t_rtt && 42257433Sandrew SEQ_GT(ti->ti_ack, tp->t_rtseq)) 42357433Sandrew tcp_xmit_timer(tp, tp->t_rtt); 42444375Skarels acked = ti->ti_ack - tp->snd_una; 42544375Skarels tcpstat.tcps_rcvackpack++; 42644375Skarels tcpstat.tcps_rcvackbyte += acked; 42744375Skarels sbdrop(&so->so_snd, acked); 42844375Skarels tp->snd_una = ti->ti_ack; 42944375Skarels m_freem(m); 4305440Swnj 43144375Skarels /* 43244375Skarels * If all outstanding data are acked, stop 43344375Skarels * retransmit timer, otherwise restart timer 43444375Skarels * using current (possibly backed-off) value. 43544375Skarels * If process is waiting for space, 43644375Skarels * wakeup/selwakeup/signal. If data 43744375Skarels * are ready to send, let tcp_output 43844375Skarels * decide between more output or persist. 43944375Skarels */ 44044375Skarels if (tp->snd_una == tp->snd_max) 44144375Skarels tp->t_timer[TCPT_REXMT] = 0; 44244375Skarels else if (tp->t_timer[TCPT_PERSIST] == 0) 44344375Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 44444375Skarels 44544375Skarels if (so->so_snd.sb_flags & SB_NOTIFY) 44644375Skarels sowwakeup(so); 44744375Skarels if (so->so_snd.sb_cc) 44844375Skarels (void) tcp_output(tp); 44944375Skarels return; 45044375Skarels } 45144375Skarels } else if (ti->ti_ack == tp->snd_una && 45244375Skarels tp->seg_next == (struct tcpiphdr *)tp && 45344375Skarels ti->ti_len <= sbspace(&so->so_rcv)) { 45444375Skarels /* 45544375Skarels * this is a pure, in-sequence data packet 45644375Skarels * with nothing on the reassembly queue and 45744375Skarels * we have enough buffer space to take it. 45844375Skarels */ 45944375Skarels ++tcppreddat; 46044375Skarels tp->rcv_nxt += ti->ti_len; 46144375Skarels tcpstat.tcps_rcvpack++; 46244375Skarels tcpstat.tcps_rcvbyte += ti->ti_len; 46344375Skarels /* 46457433Sandrew * Drop TCP, IP headers and TCP options then add data 46557433Sandrew * to socket buffer. 46644375Skarels */ 46757433Sandrew m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 46857433Sandrew m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 46944375Skarels sbappend(&so->so_rcv, m); 47044375Skarels sorwakeup(so); 47144375Skarels tp->t_flags |= TF_DELACK; 47244375Skarels return; 47344375Skarels } 47444375Skarels } 47544375Skarels 4765440Swnj /* 47757433Sandrew * Drop TCP, IP headers and TCP options. 47844375Skarels */ 47957433Sandrew m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 48057433Sandrew m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 48144375Skarels 48244375Skarels /* 4835085Swnj * Calculate amount of space in receive window, 4845085Swnj * and then do TCP input processing. 48524816Skarels * Receive window is amount of space in rcv queue, 48624816Skarels * but not less than advertised window. 4874601Swnj */ 48825939Skarels { int win; 4894601Swnj 49025939Skarels win = sbspace(&so->so_rcv); 49125939Skarels if (win < 0) 49225939Skarels win = 0; 49337320Skarels tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 49425939Skarels } 49525939Skarels 4964601Swnj switch (tp->t_state) { 4974601Swnj 4985065Swnj /* 4995065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 5005065Swnj * If the segment contains an ACK then it is bad and send a RST. 5015065Swnj * If it does not contain a SYN then it is not interesting; drop it. 50225197Skarels * Don't bother responding if the destination was a broadcast. 5035085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 5045065Swnj * tp->iss, and send a segment: 5055085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 5065065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 5075065Swnj * Fill in remote peer address fields if not previously specified. 5085065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 5095244Sroot * segment in this state. 5105065Swnj */ 5118271Sroot case TCPS_LISTEN: { 51210145Ssam struct mbuf *am; 5138271Sroot register struct sockaddr_in *sin; 5148271Sroot 5155065Swnj if (tiflags & TH_RST) 5165065Swnj goto drop; 5175300Sroot if (tiflags & TH_ACK) 5185085Swnj goto dropwithreset; 5195300Sroot if ((tiflags & TH_SYN) == 0) 5205065Swnj goto drop; 52158998Ssklower /* 52258998Ssklower * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 52358998Ssklower * in_broadcast() should never return true on a received 52458998Ssklower * packet with M_BCAST not set. 52558998Ssklower */ 52657433Sandrew if (m->m_flags & (M_BCAST|M_MCAST) || 52758998Ssklower IN_MULTICAST(ti->ti_dst.s_addr)) 52825197Skarels goto drop; 52944375Skarels am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 53010145Ssam if (am == NULL) 53110145Ssam goto drop; 53210145Ssam am->m_len = sizeof (struct sockaddr_in); 5338599Sroot sin = mtod(am, struct sockaddr_in *); 5348271Sroot sin->sin_family = AF_INET; 53537320Skarels sin->sin_len = sizeof(*sin); 5368271Sroot sin->sin_addr = ti->ti_src; 5378271Sroot sin->sin_port = ti->ti_sport; 53856399Ssklower bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 5396028Sroot laddr = inp->inp_laddr; 54010145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 5416028Sroot inp->inp_laddr = ti->ti_dst; 5428599Sroot if (in_pcbconnect(inp, am)) { 5436028Sroot inp->inp_laddr = laddr; 5448716Sroot (void) m_free(am); 5455244Sroot goto drop; 5466028Sroot } 5478716Sroot (void) m_free(am); 5485244Sroot tp->t_template = tcp_template(tp); 5495244Sroot if (tp->t_template == 0) { 55026386Skarels tp = tcp_drop(tp, ENOBUFS); 55117264Skarels dropsocket = 0; /* socket is already gone */ 5525244Sroot goto drop; 5535244Sroot } 55457433Sandrew if (optp) 55557433Sandrew tcp_dooptions(tp, optp, optlen, ti, 55657433Sandrew &ts_present, &ts_val, &ts_ecr); 55730525Skarels if (iss) 55830525Skarels tp->iss = iss; 55930525Skarels else 56030525Skarels tp->iss = tcp_iss; 56130525Skarels tcp_iss += TCP_ISSINCR/2; 5625065Swnj tp->irs = ti->ti_seq; 5635085Swnj tcp_sendseqinit(tp); 5645085Swnj tcp_rcvseqinit(tp); 56525939Skarels tp->t_flags |= TF_ACKNOW; 5665065Swnj tp->t_state = TCPS_SYN_RECEIVED; 56733745Skarels tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 56810769Ssam dropsocket = 0; /* committed to socket */ 56930525Skarels tcpstat.tcps_accepts++; 5705085Swnj goto trimthenstep6; 5718271Sroot } 5724601Swnj 5735065Swnj /* 5745065Swnj * If the state is SYN_SENT: 5755065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 5765065Swnj * if seg contains a RST, then drop the connection. 5775065Swnj * if seg does not contain SYN, then drop it. 5785065Swnj * Otherwise this is an acceptable SYN segment 5795065Swnj * initialize tp->rcv_nxt and tp->irs 5805065Swnj * if seg contains ack then advance tp->snd_una 5815065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 5825065Swnj * arrange for segment to be acked (eventually) 5835065Swnj * continue processing rest of data/controls, beginning with URG 5845065Swnj */ 5855065Swnj case TCPS_SYN_SENT: 5865065Swnj if ((tiflags & TH_ACK) && 58724816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) || 5885231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 5895085Swnj goto dropwithreset; 5905065Swnj if (tiflags & TH_RST) { 59110394Ssam if (tiflags & TH_ACK) 59210394Ssam tp = tcp_drop(tp, ECONNREFUSED); 5935065Swnj goto drop; 5944601Swnj } 5955065Swnj if ((tiflags & TH_SYN) == 0) 5965065Swnj goto drop; 59730974Skarels if (tiflags & TH_ACK) { 59830974Skarels tp->snd_una = ti->ti_ack; 59930974Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 60030974Skarels tp->snd_nxt = tp->snd_una; 60130974Skarels } 6025244Sroot tp->t_timer[TCPT_REXMT] = 0; 6035065Swnj tp->irs = ti->ti_seq; 6045085Swnj tcp_rcvseqinit(tp); 6055085Swnj tp->t_flags |= TF_ACKNOW; 60630974Skarels if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 60730525Skarels tcpstat.tcps_connects++; 6085244Sroot soisconnected(so); 6095065Swnj tp->t_state = TCPS_ESTABLISHED; 61057433Sandrew /* Do window scaling on this connection? */ 61157433Sandrew if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 61257433Sandrew (TF_RCVD_SCALE|TF_REQ_SCALE)) { 61357433Sandrew tp->snd_scale = tp->requested_s_scale; 61457433Sandrew tp->rcv_scale = tp->request_r_scale; 61557433Sandrew } 61644375Skarels (void) tcp_reass(tp, (struct tcpiphdr *)0, 61744375Skarels (struct mbuf *)0); 61832098Skarels /* 61932098Skarels * if we didn't have to retransmit the SYN, 62032098Skarels * use its rtt as our initial srtt & rtt var. 62132098Skarels */ 62244375Skarels if (tp->t_rtt) 62357433Sandrew tcp_xmit_timer(tp, tp->t_rtt); 6245162Swnj } else 6255085Swnj tp->t_state = TCPS_SYN_RECEIVED; 6265085Swnj 6275085Swnj trimthenstep6: 6285085Swnj /* 6295231Swnj * Advance ti->ti_seq to correspond to first data byte. 6305085Swnj * If data, trim to stay within window, 6315085Swnj * dropping FIN if necessary. 6325085Swnj */ 6335231Swnj ti->ti_seq++; 6345085Swnj if (ti->ti_len > tp->rcv_wnd) { 6355085Swnj todrop = ti->ti_len - tp->rcv_wnd; 6365085Swnj m_adj(m, -todrop); 6375085Swnj ti->ti_len = tp->rcv_wnd; 63825939Skarels tiflags &= ~TH_FIN; 63930525Skarels tcpstat.tcps_rcvpackafterwin++; 64030525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 6415065Swnj } 6425263Swnj tp->snd_wl1 = ti->ti_seq - 1; 64325939Skarels tp->rcv_up = ti->ti_seq; 6445085Swnj goto step6; 6455065Swnj } 6464601Swnj 6475065Swnj /* 64830525Skarels * States other than LISTEN or SYN_SENT. 64957433Sandrew * First check timestamp, if present. 65057433Sandrew * Then check that at least some bytes of segment are within 65130525Skarels * receive window. If segment begins before rcv_nxt, 65230525Skarels * drop leading data (and SYN); if nothing left, just ack. 65357433Sandrew * 65457433Sandrew * RFC 1323 PAWS: If we have a timestamp reply on this segment 65557433Sandrew * and it's less than ts_recent, drop it. 65616222Skarels */ 65757433Sandrew if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && 65857433Sandrew TSTMP_LT(ts_val, tp->ts_recent)) { 65957433Sandrew 66057433Sandrew /* Check to see if ts_recent is over 24 days old. */ 66157433Sandrew if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 66257433Sandrew /* 66357433Sandrew * Invalidate ts_recent. If this segment updates 66457433Sandrew * ts_recent, the age will be reset later and ts_recent 66557433Sandrew * will get a valid value. If it does not, setting 66657433Sandrew * ts_recent to zero will at least satisfy the 66757433Sandrew * requirement that zero be placed in the timestamp 66857433Sandrew * echo reply when ts_recent isn't valid. The 66957433Sandrew * age isn't reset until we get a valid ts_recent 67057433Sandrew * because we don't want out-of-order segments to be 67157433Sandrew * dropped when ts_recent is old. 67257433Sandrew */ 67357433Sandrew tp->ts_recent = 0; 67457433Sandrew } else { 67557433Sandrew tcpstat.tcps_rcvduppack++; 67657433Sandrew tcpstat.tcps_rcvdupbyte += ti->ti_len; 67757433Sandrew tcpstat.tcps_pawsdrop++; 67857433Sandrew goto dropafterack; 67957433Sandrew } 68057433Sandrew } 68157433Sandrew 68230525Skarels todrop = tp->rcv_nxt - ti->ti_seq; 68330525Skarels if (todrop > 0) { 68430525Skarels if (tiflags & TH_SYN) { 68530525Skarels tiflags &= ~TH_SYN; 68630525Skarels ti->ti_seq++; 68730525Skarels if (ti->ti_urp > 1) 68830525Skarels ti->ti_urp--; 68930525Skarels else 69030525Skarels tiflags &= ~TH_URG; 69130525Skarels todrop--; 69230525Skarels } 69364176Smckusick if (todrop >= ti->ti_len) { 69433745Skarels tcpstat.tcps_rcvduppack++; 69533745Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 69631730Skarels /* 69733745Skarels * If segment is just one to the left of the window, 69833745Skarels * check two special cases: 69933745Skarels * 1. Don't toss RST in response to 4.2-style keepalive. 70033745Skarels * 2. If the only thing to drop is a FIN, we can drop 70133745Skarels * it, but check the ACK or we will get into FIN 70233745Skarels * wars if our FINs crossed (both CLOSING). 70333745Skarels * In either case, send ACK to resynchronize, 70433745Skarels * but keep on processing for RST or ACK. 70531730Skarels */ 70633745Skarels if ((tiflags & TH_FIN && todrop == ti->ti_len + 1) 70733745Skarels #ifdef TCP_COMPAT_42 70833745Skarels || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1) 70931730Skarels #endif 71033745Skarels ) { 71133745Skarels todrop = ti->ti_len; 71233745Skarels tiflags &= ~TH_FIN; 71333745Skarels tp->t_flags |= TF_ACKNOW; 71457433Sandrew } else { 71557433Sandrew /* 71657433Sandrew * Handle the case when a bound socket connects 71757433Sandrew * to itself. Allow packets with a SYN and 71857433Sandrew * an ACK to continue with the processing. 71957433Sandrew */ 72057433Sandrew if (todrop != 0 || (tiflags & TH_ACK) == 0) 72157433Sandrew goto dropafterack; 72257433Sandrew } 72332034Skarels } else { 72432034Skarels tcpstat.tcps_rcvpartduppack++; 72532034Skarels tcpstat.tcps_rcvpartdupbyte += todrop; 72630525Skarels } 72730525Skarels m_adj(m, todrop); 72830525Skarels ti->ti_seq += todrop; 72930525Skarels ti->ti_len -= todrop; 73030525Skarels if (ti->ti_urp > todrop) 73130525Skarels ti->ti_urp -= todrop; 73230525Skarels else { 73330525Skarels tiflags &= ~TH_URG; 73430525Skarels ti->ti_urp = 0; 73530525Skarels } 73616222Skarels } 73716222Skarels 73832612Skarels /* 73933745Skarels * If new data are received on a connection after the 74032612Skarels * user processes are gone, then RST the other end. 74132612Skarels */ 74232612Skarels if ((so->so_state & SS_NOFDREF) && 74332612Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 74432612Skarels tp = tcp_close(tp); 74532612Skarels tcpstat.tcps_rcvafterclose++; 74632612Skarels goto dropwithreset; 74732612Skarels } 74832612Skarels 74933445Skarels /* 75033445Skarels * If segment ends after window, drop trailing data 75133445Skarels * (and PUSH and FIN); if nothing left, just ACK. 75233445Skarels */ 75333445Skarels todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 75433445Skarels if (todrop > 0) { 75533445Skarels tcpstat.tcps_rcvpackafterwin++; 75633445Skarels if (todrop >= ti->ti_len) { 75730525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 75833445Skarels /* 75933445Skarels * If a new connection request is received 76033445Skarels * while in TIME_WAIT, drop the old connection 76133445Skarels * and start over if the sequence numbers 76233445Skarels * are above the previous ones. 76333445Skarels */ 76433445Skarels if (tiflags & TH_SYN && 76533445Skarels tp->t_state == TCPS_TIME_WAIT && 76633445Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 76733445Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 76844375Skarels tp = tcp_close(tp); 76933445Skarels goto findpcb; 77033445Skarels } 77133445Skarels /* 77233445Skarels * If window is closed can only take segments at 77333445Skarels * window edge, and have to drop data and PUSH from 77433445Skarels * incoming segments. Continue processing, but 77533445Skarels * remember to ack. Otherwise, drop segment 77633445Skarels * and ack. 77733445Skarels */ 77833445Skarels if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 77933445Skarels tp->t_flags |= TF_ACKNOW; 78030525Skarels tcpstat.tcps_rcvwinprobe++; 78133445Skarels } else 7825065Swnj goto dropafterack; 78333445Skarels } else 78430525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 78533445Skarels m_adj(m, -todrop); 78633445Skarels ti->ti_len -= todrop; 78733445Skarels tiflags &= ~(TH_PUSH|TH_FIN); 7885065Swnj } 7894601Swnj 7905065Swnj /* 79157433Sandrew * If last ACK falls within this segment's sequence numbers, 79257433Sandrew * record its timestamp. 79357433Sandrew */ 79457433Sandrew if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) && 79557433Sandrew SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len + 79657433Sandrew ((tiflags & (TH_SYN|TH_FIN)) != 0))) { 79757433Sandrew tp->ts_recent_age = tcp_now; 79857433Sandrew tp->ts_recent = ts_val; 79957433Sandrew } 80057433Sandrew 80157433Sandrew /* 8025065Swnj * If the RST bit is set examine the state: 8035065Swnj * SYN_RECEIVED STATE: 8045065Swnj * If passive open, return to LISTEN state. 8055065Swnj * If active open, inform user that connection was refused. 8065065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 8075065Swnj * Inform user that connection was reset, and close tcb. 8085065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 8095065Swnj * Close the tcb. 8105065Swnj */ 8115065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 8125267Sroot 8135065Swnj case TCPS_SYN_RECEIVED: 81433745Skarels so->so_error = ECONNREFUSED; 81533745Skarels goto close; 8164601Swnj 8175065Swnj case TCPS_ESTABLISHED: 8185065Swnj case TCPS_FIN_WAIT_1: 8195065Swnj case TCPS_FIN_WAIT_2: 8205065Swnj case TCPS_CLOSE_WAIT: 82133745Skarels so->so_error = ECONNRESET; 82233745Skarels close: 82333745Skarels tp->t_state = TCPS_CLOSED; 82433745Skarels tcpstat.tcps_drops++; 82533745Skarels tp = tcp_close(tp); 8265065Swnj goto drop; 8275065Swnj 8285065Swnj case TCPS_CLOSING: 8295065Swnj case TCPS_LAST_ACK: 8305065Swnj case TCPS_TIME_WAIT: 83110394Ssam tp = tcp_close(tp); 8325065Swnj goto drop; 8334601Swnj } 8344601Swnj 8354601Swnj /* 8365065Swnj * If a SYN is in the window, then this is an 8375065Swnj * error and we send an RST and drop the connection. 8384601Swnj */ 8395065Swnj if (tiflags & TH_SYN) { 84010394Ssam tp = tcp_drop(tp, ECONNRESET); 8415085Swnj goto dropwithreset; 8424601Swnj } 8434601Swnj 8444601Swnj /* 8455065Swnj * If the ACK bit is off we drop the segment and return. 8464601Swnj */ 8475085Swnj if ((tiflags & TH_ACK) == 0) 8485065Swnj goto drop; 8495065Swnj 8505065Swnj /* 8515065Swnj * Ack processing. 8525065Swnj */ 8534601Swnj switch (tp->t_state) { 8544601Swnj 8555065Swnj /* 8565065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 85731721Skarels * ESTABLISHED state and continue processing, otherwise 8585065Swnj * send an RST. 8595065Swnj */ 8605065Swnj case TCPS_SYN_RECEIVED: 8615085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 8625231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 8635085Swnj goto dropwithreset; 86430525Skarels tcpstat.tcps_connects++; 8655085Swnj soisconnected(so); 8665085Swnj tp->t_state = TCPS_ESTABLISHED; 86757433Sandrew /* Do window scaling? */ 86857433Sandrew if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 86957433Sandrew (TF_RCVD_SCALE|TF_REQ_SCALE)) { 87057433Sandrew tp->snd_scale = tp->requested_s_scale; 87157433Sandrew tp->rcv_scale = tp->request_r_scale; 87257433Sandrew } 87344375Skarels (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0); 8745244Sroot tp->snd_wl1 = ti->ti_seq - 1; 8755085Swnj /* fall into ... */ 8764601Swnj 8775065Swnj /* 8785065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 8795065Swnj * ACKs. If the ack is in the range 8805231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 8815065Swnj * then advance tp->snd_una to ti->ti_ack and drop 8825065Swnj * data from the retransmission queue. If this ACK reflects 8835065Swnj * more up to date window information we update our window information. 8845065Swnj */ 8855065Swnj case TCPS_ESTABLISHED: 8865065Swnj case TCPS_FIN_WAIT_1: 8875065Swnj case TCPS_FIN_WAIT_2: 8885065Swnj case TCPS_CLOSE_WAIT: 8895065Swnj case TCPS_CLOSING: 8905244Sroot case TCPS_LAST_ACK: 8915244Sroot case TCPS_TIME_WAIT: 8925085Swnj 89330525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 89457433Sandrew if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { 89530525Skarels tcpstat.tcps_rcvdupack++; 89632098Skarels /* 89744375Skarels * If we have outstanding data (other than 89844375Skarels * a window probe), this is a completely 89932098Skarels * duplicate ack (ie, window info didn't 90032098Skarels * change), the ack is the biggest we've 90132098Skarels * seen and we've seen exactly our rexmt 90232098Skarels * threshhold of them, assume a packet 90332098Skarels * has been dropped and retransmit it. 90432098Skarels * Kludge snd_nxt & the congestion 90532098Skarels * window so we send only this one 90644375Skarels * packet. 90744375Skarels * 90844375Skarels * We know we're losing at the current 90944375Skarels * window size so do congestion avoidance 91044375Skarels * (set ssthresh to half the current window 91144375Skarels * and pull our congestion window back to 91244375Skarels * the new ssthresh). 91344375Skarels * 91444375Skarels * Dup acks mean that packets have left the 91544375Skarels * network (they're now cached at the receiver) 91644375Skarels * so bump cwnd by the amount in the receiver 91744375Skarels * to keep a constant cwnd packets in the 91844375Skarels * network. 91932098Skarels */ 92032098Skarels if (tp->t_timer[TCPT_REXMT] == 0 || 92132098Skarels ti->ti_ack != tp->snd_una) 92232098Skarels tp->t_dupacks = 0; 92332098Skarels else if (++tp->t_dupacks == tcprexmtthresh) { 92432098Skarels tcp_seq onxt = tp->snd_nxt; 92532376Skarels u_int win = 92637320Skarels min(tp->snd_wnd, tp->snd_cwnd) / 2 / 92732376Skarels tp->t_maxseg; 92832098Skarels 92932376Skarels if (win < 2) 93032376Skarels win = 2; 93132376Skarels tp->snd_ssthresh = win * tp->t_maxseg; 93232098Skarels tp->t_timer[TCPT_REXMT] = 0; 93332098Skarels tp->t_rtt = 0; 93432098Skarels tp->snd_nxt = ti->ti_ack; 93532098Skarels tp->snd_cwnd = tp->t_maxseg; 93632098Skarels (void) tcp_output(tp); 93744375Skarels tp->snd_cwnd = tp->snd_ssthresh + 93844375Skarels tp->t_maxseg * tp->t_dupacks; 93932098Skarels if (SEQ_GT(onxt, tp->snd_nxt)) 94032098Skarels tp->snd_nxt = onxt; 94132098Skarels goto drop; 94244375Skarels } else if (tp->t_dupacks > tcprexmtthresh) { 94344375Skarels tp->snd_cwnd += tp->t_maxseg; 94444375Skarels (void) tcp_output(tp); 94544375Skarels goto drop; 94632098Skarels } 94732098Skarels } else 94832098Skarels tp->t_dupacks = 0; 9495065Swnj break; 95030525Skarels } 95144375Skarels /* 95244375Skarels * If the congestion window was inflated to account 95344375Skarels * for the other side's cached packets, retract it. 95444375Skarels */ 95544375Skarels if (tp->t_dupacks > tcprexmtthresh && 95644375Skarels tp->snd_cwnd > tp->snd_ssthresh) 95744375Skarels tp->snd_cwnd = tp->snd_ssthresh; 95832098Skarels tp->t_dupacks = 0; 95930525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 96030525Skarels tcpstat.tcps_rcvacktoomuch++; 9615065Swnj goto dropafterack; 96230525Skarels } 9635085Swnj acked = ti->ti_ack - tp->snd_una; 96430525Skarels tcpstat.tcps_rcvackpack++; 96530525Skarels tcpstat.tcps_rcvackbyte += acked; 9665951Swnj 9675951Swnj /* 96857433Sandrew * If we have a timestamp reply, update smoothed 96957433Sandrew * round trip time. If no timestamp is present but 97057433Sandrew * transmit timer is running and timed sequence 9715951Swnj * number was acked, update smoothed round trip time. 97232034Skarels * Since we now have an rtt measurement, cancel the 97332034Skarels * timer backoff (cf., Phil Karn's retransmit alg.). 97432034Skarels * Recompute the initial retransmit timer. 9755951Swnj */ 97657433Sandrew if (ts_present) 97757433Sandrew tcp_xmit_timer(tp, tcp_now-ts_ecr+1); 97857433Sandrew else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 97957433Sandrew tcp_xmit_timer(tp,tp->t_rtt); 98031726Skarels 98126824Skarels /* 98226824Skarels * If all outstanding data is acked, stop retransmit 98326824Skarels * timer and remember to restart (more output or persist). 98426824Skarels * If there is more data to be acked, restart retransmit 98532034Skarels * timer, using current (possibly backed-off) value. 98626824Skarels */ 98726824Skarels if (ti->ti_ack == tp->snd_max) { 9885244Sroot tp->t_timer[TCPT_REXMT] = 0; 98926824Skarels needoutput = 1; 99032034Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) 99132034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 99217360Skarels /* 99332098Skarels * When new data is acked, open the congestion window. 99432098Skarels * If the window gives us less than ssthresh packets 99532098Skarels * in flight, open exponentially (maxseg per packet). 99644375Skarels * Otherwise open linearly: maxseg per window 99744375Skarels * (maxseg^2 / cwnd per packet), plus a constant 99844375Skarels * fraction of a packet (maxseg/8) to help larger windows 99944375Skarels * open quickly enough. 100017360Skarels */ 100132098Skarels { 100244375Skarels register u_int cw = tp->snd_cwnd; 100344375Skarels register u_int incr = tp->t_maxseg; 100432098Skarels 100544375Skarels if (cw > tp->snd_ssthresh) 100644375Skarels incr = incr * incr / cw + incr / 8; 100757433Sandrew tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); 100832098Skarels } 10095307Sroot if (acked > so->so_snd.sb_cc) { 101015386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 101126386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 101231721Skarels ourfinisacked = 1; 10135307Sroot } else { 10146161Ssam sbdrop(&so->so_snd, acked); 10155307Sroot tp->snd_wnd -= acked; 101631721Skarels ourfinisacked = 0; 10175307Sroot } 101844375Skarels if (so->so_snd.sb_flags & SB_NOTIFY) 101944375Skarels sowwakeup(so); 10205231Swnj tp->snd_una = ti->ti_ack; 10215357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 10225357Sroot tp->snd_nxt = tp->snd_una; 10235162Swnj 10244601Swnj switch (tp->t_state) { 10254601Swnj 10265065Swnj /* 10275065Swnj * In FIN_WAIT_1 STATE in addition to the processing 10285065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 10295085Swnj * then enter FIN_WAIT_2. 10305065Swnj */ 10315065Swnj case TCPS_FIN_WAIT_1: 10325896Swnj if (ourfinisacked) { 10335896Swnj /* 10345896Swnj * If we can't receive any more 10355896Swnj * data, then closing user can proceed. 103624816Skarels * Starting the timer is contrary to the 103724816Skarels * specification, but if we don't get a FIN 103824816Skarels * we'll hang forever. 10395896Swnj */ 104024816Skarels if (so->so_state & SS_CANTRCVMORE) { 10415896Swnj soisdisconnected(so); 104233745Skarels tp->t_timer[TCPT_2MSL] = tcp_maxidle; 104324816Skarels } 10445085Swnj tp->t_state = TCPS_FIN_WAIT_2; 10455896Swnj } 10464601Swnj break; 10474601Swnj 10485065Swnj /* 10495065Swnj * In CLOSING STATE in addition to the processing for 10505065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 10515065Swnj * then enter the TIME-WAIT state, otherwise ignore 10525065Swnj * the segment. 10535065Swnj */ 10545065Swnj case TCPS_CLOSING: 10555244Sroot if (ourfinisacked) { 10565065Swnj tp->t_state = TCPS_TIME_WAIT; 10575244Sroot tcp_canceltimers(tp); 10585244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10595244Sroot soisdisconnected(so); 10605244Sroot } 10615244Sroot break; 10624601Swnj 10635065Swnj /* 106431743Skarels * In LAST_ACK, we may still be waiting for data to drain 106531743Skarels * and/or to be acked, as well as for the ack of our FIN. 106631743Skarels * If our FIN is now acknowledged, delete the TCB, 106731743Skarels * enter the closed state and return. 10685065Swnj */ 10695065Swnj case TCPS_LAST_ACK: 107031743Skarels if (ourfinisacked) { 107110394Ssam tp = tcp_close(tp); 107231743Skarels goto drop; 107331743Skarels } 107431743Skarels break; 10754601Swnj 10765065Swnj /* 10775065Swnj * In TIME_WAIT state the only thing that should arrive 10785065Swnj * is a retransmission of the remote FIN. Acknowledge 10795065Swnj * it and restart the finack timer. 10805065Swnj */ 10815065Swnj case TCPS_TIME_WAIT: 10825162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10835065Swnj goto dropafterack; 10844601Swnj } 10855085Swnj } 10864601Swnj 10875065Swnj step6: 10885065Swnj /* 10895244Sroot * Update window information. 109025939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 10915244Sroot */ 109225939Skarels if ((tiflags & TH_ACK) && 109325939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 10945391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 109557433Sandrew tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))) { 109630525Skarels /* keep track of pure window updates */ 109730525Skarels if (ti->ti_len == 0 && 109857433Sandrew tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) 109930525Skarels tcpstat.tcps_rcvwinupd++; 110057433Sandrew tp->snd_wnd = tiwin; 11015244Sroot tp->snd_wl1 = ti->ti_seq; 11025244Sroot tp->snd_wl2 = ti->ti_ack; 110325260Skarels if (tp->snd_wnd > tp->max_sndwnd) 110425260Skarels tp->max_sndwnd = tp->snd_wnd; 110526824Skarels needoutput = 1; 110626824Skarels } 11075244Sroot 11085244Sroot /* 11095547Swnj * Process segments with URG. 11105065Swnj */ 11117267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 11127267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 11135547Swnj /* 111425939Skarels * This is a kludge, but if we receive and accept 111513121Ssam * random urgent pointers, we'll crash in 111613121Ssam * soreceive. It's hard to imagine someone 111713121Ssam * actually wanting to send this much urgent data. 111812441Ssam */ 111957433Sandrew if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) { 112012441Ssam ti->ti_urp = 0; /* XXX */ 112112441Ssam tiflags &= ~TH_URG; /* XXX */ 112225939Skarels goto dodata; /* XXX */ 112312441Ssam } 112412441Ssam /* 11255547Swnj * If this segment advances the known urgent pointer, 11265547Swnj * then mark the data stream. This should not happen 11275547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 11285547Swnj * a FIN has been received from the remote side. 11295547Swnj * In these states we ignore the URG. 113027190Skarels * 113127190Skarels * According to RFC961 (Assigned Protocols), 113227190Skarels * the urgent pointer points to the last octet 113327190Skarels * of urgent data. We continue, however, 113427190Skarels * to consider it to indicate the first octet 113544375Skarels * of data past the urgent section as the original 113644375Skarels * spec states (in one of two places). 11375547Swnj */ 11385547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 11395547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 11405547Swnj so->so_oobmark = so->so_rcv.sb_cc + 11415547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 11425547Swnj if (so->so_oobmark == 0) 11435547Swnj so->so_state |= SS_RCVATMARK; 11448313Sroot sohasoutofband(so); 114524816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 11465440Swnj } 11475547Swnj /* 11485547Swnj * Remove out of band data so doesn't get presented to user. 11495547Swnj * This can happen independent of advancing the URG pointer, 11505547Swnj * but if two URG's are pending at once, some out-of-band 11515547Swnj * data may creep in... ick. 11525547Swnj */ 115344375Skarels if (ti->ti_urp <= ti->ti_len 115444375Skarels #ifdef SO_OOBINLINE 115544375Skarels && (so->so_options & SO_OOBINLINE) == 0 115644375Skarels #endif 115744375Skarels ) 115844375Skarels tcp_pulloutofband(so, ti, m); 115925939Skarels } else 116025939Skarels /* 116125939Skarels * If no out of band data is expected, 116225939Skarels * pull receive urgent pointer along 116325939Skarels * with the receive window. 116425939Skarels */ 116525939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 116625939Skarels tp->rcv_up = tp->rcv_nxt; 116725939Skarels dodata: /* XXX */ 11684601Swnj 11694601Swnj /* 11705065Swnj * Process the segment text, merging it into the TCP sequencing queue, 11715065Swnj * and arranging for acknowledgment of receipt if necessary. 11725065Swnj * This process logically involves adjusting tp->rcv_wnd as data 11735065Swnj * is presented to the user (this happens in tcp_usrreq.c, 11745065Swnj * case PRU_RCVD). If a FIN has already been received on this 11755065Swnj * connection then we just ignore the text. 11764601Swnj */ 117717946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 117817946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 117924816Skarels TCP_REASS(tp, ti, m, so, tiflags); 118025260Skarels /* 118125260Skarels * Note the amount of data that peer has sent into 118225260Skarels * our window, in order to estimate the sender's 118325260Skarels * buffer size. 118425260Skarels */ 118532098Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 11865244Sroot } else { 11874924Swnj m_freem(m); 11885263Swnj tiflags &= ~TH_FIN; 11895244Sroot } 11904601Swnj 11914601Swnj /* 11925263Swnj * If FIN is received ACK the FIN and let the user know 11935263Swnj * that the connection is closing. 11944601Swnj */ 11955263Swnj if (tiflags & TH_FIN) { 11965244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 11975244Sroot socantrcvmore(so); 11985244Sroot tp->t_flags |= TF_ACKNOW; 11995244Sroot tp->rcv_nxt++; 12005244Sroot } 12015065Swnj switch (tp->t_state) { 12024601Swnj 12035065Swnj /* 12045065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 12055065Swnj * enter the CLOSE_WAIT state. 12064884Swnj */ 12075065Swnj case TCPS_SYN_RECEIVED: 12085065Swnj case TCPS_ESTABLISHED: 12095065Swnj tp->t_state = TCPS_CLOSE_WAIT; 12105065Swnj break; 12114884Swnj 12125065Swnj /* 12135085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 12145085Swnj * enter the CLOSING state. 12154884Swnj */ 12165065Swnj case TCPS_FIN_WAIT_1: 12175085Swnj tp->t_state = TCPS_CLOSING; 12185065Swnj break; 12194601Swnj 12205065Swnj /* 12215065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 12225065Swnj * starting the time-wait timer, turning off the other 12235065Swnj * standard timers. 12245065Swnj */ 12255065Swnj case TCPS_FIN_WAIT_2: 12265244Sroot tp->t_state = TCPS_TIME_WAIT; 12275074Swnj tcp_canceltimers(tp); 12285162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 12295244Sroot soisdisconnected(so); 12305065Swnj break; 12315065Swnj 12324884Swnj /* 12335065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 12344884Swnj */ 12355065Swnj case TCPS_TIME_WAIT: 12365162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 12375065Swnj break; 12385085Swnj } 12394601Swnj } 12405267Sroot if (so->so_options & SO_DEBUG) 12415267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 12425085Swnj 12435085Swnj /* 12445085Swnj * Return any desired output. 12455085Swnj */ 124626824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 124725939Skarels (void) tcp_output(tp); 12485065Swnj return; 12495085Swnj 12505065Swnj dropafterack: 12515085Swnj /* 12526211Swnj * Generate an ACK dropping incoming segment if it occupies 12536211Swnj * sequence space, where the ACK reflects our state. 12545085Swnj */ 125526057Skarels if (tiflags & TH_RST) 12565085Swnj goto drop; 125731749Skarels m_freem(m); 125831721Skarels tp->t_flags |= TF_ACKNOW; 125931721Skarels (void) tcp_output(tp); 12605231Swnj return; 12615085Swnj 12625085Swnj dropwithreset: 12635085Swnj /* 12645244Sroot * Generate a RST, dropping incoming segment. 12655085Swnj * Make ACK acceptable to originator of segment. 126657433Sandrew * Don't bother to respond if destination was broadcast/multicast. 12675085Swnj */ 126857433Sandrew if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) || 126958998Ssklower IN_MULTICAST(ti->ti_dst.s_addr)) 12705085Swnj goto drop; 12715085Swnj if (tiflags & TH_ACK) 127237320Skarels tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 12735085Swnj else { 12745085Swnj if (tiflags & TH_SYN) 12755085Swnj ti->ti_len++; 127637320Skarels tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 12776211Swnj TH_RST|TH_ACK); 12785085Swnj } 127910769Ssam /* destroy temporarily created socket */ 128010769Ssam if (dropsocket) 128110769Ssam (void) soabort(so); 12825231Swnj return; 12835085Swnj 12845065Swnj drop: 12855085Swnj /* 12865085Swnj * Drop space held by incoming segment and return. 12875085Swnj */ 12886303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 12896303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 12905065Swnj m_freem(m); 129110769Ssam /* destroy temporarily created socket */ 129210769Ssam if (dropsocket) 129310769Ssam (void) soabort(so); 12945267Sroot return; 129557947Ssklower #ifndef TUBA_INCLUDE 12965065Swnj } 12975065Swnj 129861335Sbostic void 129957433Sandrew tcp_dooptions(tp, cp, cnt, ti, ts_present, ts_val, ts_ecr) 13005440Swnj struct tcpcb *tp; 130157433Sandrew u_char *cp; 130257433Sandrew int cnt; 130317272Skarels struct tcpiphdr *ti; 130457433Sandrew int *ts_present; 130557433Sandrew u_long *ts_val, *ts_ecr; 13065419Swnj { 130744375Skarels u_short mss; 130857433Sandrew int opt, optlen; 13095419Swnj 13105440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 13115440Swnj opt = cp[0]; 13125440Swnj if (opt == TCPOPT_EOL) 13135440Swnj break; 13145440Swnj if (opt == TCPOPT_NOP) 13155440Swnj optlen = 1; 131612169Ssam else { 13175440Swnj optlen = cp[1]; 131812169Ssam if (optlen <= 0) 131912169Ssam break; 132012169Ssam } 13215440Swnj switch (opt) { 13225440Swnj 13235440Swnj default: 132444375Skarels continue; 13255440Swnj 13265440Swnj case TCPOPT_MAXSEG: 132757433Sandrew if (optlen != TCPOLEN_MAXSEG) 13285440Swnj continue; 132917272Skarels if (!(ti->ti_flags & TH_SYN)) 133017272Skarels continue; 133144375Skarels bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 133244375Skarels NTOHS(mss); 133344375Skarels (void) tcp_mss(tp, mss); /* sets t_maxseg */ 13345440Swnj break; 133557433Sandrew 133657433Sandrew case TCPOPT_WINDOW: 133757433Sandrew if (optlen != TCPOLEN_WINDOW) 133857433Sandrew continue; 133957433Sandrew if (!(ti->ti_flags & TH_SYN)) 134057433Sandrew continue; 134157433Sandrew tp->t_flags |= TF_RCVD_SCALE; 134257433Sandrew tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 134357433Sandrew break; 134457433Sandrew 134557433Sandrew case TCPOPT_TIMESTAMP: 134657433Sandrew if (optlen != TCPOLEN_TIMESTAMP) 134757433Sandrew continue; 134857433Sandrew *ts_present = 1; 134957433Sandrew bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val)); 135057433Sandrew NTOHL(*ts_val); 135157433Sandrew bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr)); 135257433Sandrew NTOHL(*ts_ecr); 135357433Sandrew 135457433Sandrew /* 135557433Sandrew * A timestamp received in a SYN makes 135657433Sandrew * it ok to send timestamp requests and replies. 135757433Sandrew */ 135857433Sandrew if (ti->ti_flags & TH_SYN) { 135957433Sandrew tp->t_flags |= TF_RCVD_TSTMP; 136057433Sandrew tp->ts_recent = *ts_val; 136157433Sandrew tp->ts_recent_age = tcp_now; 136257433Sandrew } 136357433Sandrew break; 13645419Swnj } 13655419Swnj } 13665419Swnj } 13675419Swnj 13685419Swnj /* 13695547Swnj * Pull out of band byte out of a segment so 13705547Swnj * it doesn't appear in the user's data queue. 13715547Swnj * It is still reflected in the segment length for 13725547Swnj * sequencing purposes. 13735547Swnj */ 137461335Sbostic void 137544375Skarels tcp_pulloutofband(so, ti, m) 13765547Swnj struct socket *so; 13775547Swnj struct tcpiphdr *ti; 137844375Skarels register struct mbuf *m; 13795547Swnj { 13806116Swnj int cnt = ti->ti_urp - 1; 13815547Swnj 13825547Swnj while (cnt >= 0) { 13835547Swnj if (m->m_len > cnt) { 13845547Swnj char *cp = mtod(m, caddr_t) + cnt; 13855547Swnj struct tcpcb *tp = sototcpcb(so); 13865547Swnj 13875547Swnj tp->t_iobc = *cp; 13885547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 13896161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 13905547Swnj m->m_len--; 13915547Swnj return; 13925547Swnj } 13935547Swnj cnt -= m->m_len; 13945547Swnj m = m->m_next; 13955547Swnj if (m == 0) 13965547Swnj break; 13975547Swnj } 13985547Swnj panic("tcp_pulloutofband"); 13995547Swnj } 14005547Swnj 14015547Swnj /* 140244375Skarels * Collect new round-trip time estimate 140344375Skarels * and update averages and current timeout. 140417272Skarels */ 140561335Sbostic void 140657433Sandrew tcp_xmit_timer(tp, rtt) 140723975Skarels register struct tcpcb *tp; 140861335Sbostic short rtt; 140917272Skarels { 141044375Skarels register short delta; 141144375Skarels 141244375Skarels tcpstat.tcps_rttupdated++; 141344375Skarels if (tp->t_srtt != 0) { 141444375Skarels /* 141544375Skarels * srtt is stored as fixed point with 3 bits after the 141644375Skarels * binary point (i.e., scaled by 8). The following magic 141744375Skarels * is equivalent to the smoothing algorithm in rfc793 with 141844375Skarels * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 141957433Sandrew * point). Adjust rtt to origin 0. 142044375Skarels */ 142157433Sandrew delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT); 142244375Skarels if ((tp->t_srtt += delta) <= 0) 142344375Skarels tp->t_srtt = 1; 142444375Skarels /* 142544375Skarels * We accumulate a smoothed rtt variance (actually, a 142644375Skarels * smoothed mean difference), then set the retransmit 142744375Skarels * timer to smoothed rtt + 4 times the smoothed variance. 142844375Skarels * rttvar is stored as fixed point with 2 bits after the 142944375Skarels * binary point (scaled by 4). The following is 143044375Skarels * equivalent to rfc793 smoothing with an alpha of .75 143144375Skarels * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 143244375Skarels * rfc793's wired-in beta. 143344375Skarels */ 143444375Skarels if (delta < 0) 143544375Skarels delta = -delta; 143644375Skarels delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 143744375Skarels if ((tp->t_rttvar += delta) <= 0) 143844375Skarels tp->t_rttvar = 1; 143944375Skarels } else { 144044375Skarels /* 144144375Skarels * No rtt measurement yet - use the unsmoothed rtt. 144244375Skarels * Set the variance to half the rtt (so our first 144352199Skarels * retransmit happens at 3*rtt). 144444375Skarels */ 144557433Sandrew tp->t_srtt = rtt << TCP_RTT_SHIFT; 144657433Sandrew tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 144744375Skarels } 144844375Skarels tp->t_rtt = 0; 144944375Skarels tp->t_rxtshift = 0; 145044375Skarels 145144375Skarels /* 145244375Skarels * the retransmit should happen at rtt + 4 * rttvar. 145344375Skarels * Because of the way we do the smoothing, srtt and rttvar 145444375Skarels * will each average +1/2 tick of bias. When we compute 145544375Skarels * the retransmit timer, we want 1/2 tick of rounding and 145644375Skarels * 1 extra tick because of +-1/2 tick uncertainty in the 145744375Skarels * firing of the timer. The bias will give us exactly the 145844375Skarels * 1.5 tick we need. But, because the bias is 145944375Skarels * statistical, we have to test that we don't drop below 146044375Skarels * the minimum feasible timer (which is 2 ticks). 146144375Skarels */ 146244375Skarels TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 146344375Skarels tp->t_rttmin, TCPTV_REXMTMAX); 146444375Skarels 146544375Skarels /* 146644375Skarels * We received an ack for a packet that wasn't retransmitted; 146744375Skarels * it is probably safe to discard any error indications we've 146844375Skarels * received recently. This isn't quite right, but close enough 146944375Skarels * for now (a route might have failed after we sent a segment, 147044375Skarels * and the return path might not be symmetrical). 147144375Skarels */ 147244375Skarels tp->t_softerror = 0; 147344375Skarels } 147444375Skarels 147544375Skarels /* 147644375Skarels * Determine a reasonable value for maxseg size. 147744375Skarels * If the route is known, check route for mtu. 147844375Skarels * If none, use an mss that can be handled on the outgoing 147944375Skarels * interface without forcing IP to fragment; if bigger than 148044375Skarels * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 148144375Skarels * to utilize large mbufs. If no route is found, route has no mtu, 148244375Skarels * or the destination isn't local, use a default, hopefully conservative 148344375Skarels * size (usually 512 or the default IP max size, but no more than the mtu 148444375Skarels * of the interface), as we can't discover anything about intervening 148544375Skarels * gateways or networks. We also initialize the congestion/slow start 148644375Skarels * window to be a single segment if the destination isn't local. 148744375Skarels * While looking at the routing entry, we also initialize other path-dependent 148844375Skarels * parameters from pre-set or cached values in the routing entry. 148944375Skarels */ 149061335Sbostic int 149144375Skarels tcp_mss(tp, offer) 149244375Skarels register struct tcpcb *tp; 1493*65606Sbostic u_int offer; 149444375Skarels { 149517272Skarels struct route *ro; 149644375Skarels register struct rtentry *rt; 149717272Skarels struct ifnet *ifp; 149844375Skarels register int rtt, mss; 149944375Skarels u_long bufsize; 150017272Skarels struct inpcb *inp; 150144375Skarels struct socket *so; 150265460Sbostic extern int tcp_mssdflt; 150317272Skarels 150417272Skarels inp = tp->t_inpcb; 150517272Skarels ro = &inp->inp_route; 150644375Skarels 150744375Skarels if ((rt = ro->ro_rt) == (struct rtentry *)0) { 150817272Skarels /* No route yet, so try to acquire one */ 150917272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 151017272Skarels ro->ro_dst.sa_family = AF_INET; 151137320Skarels ro->ro_dst.sa_len = sizeof(ro->ro_dst); 151217272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 151317272Skarels inp->inp_faddr; 151417272Skarels rtalloc(ro); 151517272Skarels } 151644375Skarels if ((rt = ro->ro_rt) == (struct rtentry *)0) 151757637Sandrew return (tcp_mssdflt); 151817272Skarels } 151944375Skarels ifp = rt->rt_ifp; 152044375Skarels so = inp->inp_socket; 152117272Skarels 152244375Skarels #ifdef RTV_MTU /* if route characteristics exist ... */ 152344375Skarels /* 152444375Skarels * While we're here, check if there's an initial rtt 152544375Skarels * or rttvar. Convert from the route-table units 152644375Skarels * to scaled multiples of the slow timeout timer. 152744375Skarels */ 152844375Skarels if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 152952199Skarels /* 153052199Skarels * XXX the lock bit for MTU indicates that the value 153152199Skarels * is also a minimum value; this is subject to time. 153252199Skarels */ 153352199Skarels if (rt->rt_rmx.rmx_locks & RTV_RTT) 153444375Skarels tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 153544375Skarels tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 153644375Skarels if (rt->rt_rmx.rmx_rttvar) 153744375Skarels tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 153844375Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 153944375Skarels else 154044375Skarels /* default variation is +- 1 rtt */ 154144375Skarels tp->t_rttvar = 154244375Skarels tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 154344375Skarels TCPT_RANGESET(tp->t_rxtcur, 154444375Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 154544375Skarels tp->t_rttmin, TCPTV_REXMTMAX); 154644375Skarels } 154744375Skarels /* 154844375Skarels * if there's an mtu associated with the route, use it 154944375Skarels */ 155044375Skarels if (rt->rt_rmx.rmx_mtu) 155157637Sandrew mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 155244375Skarels else 155344375Skarels #endif /* RTV_MTU */ 155444375Skarels { 155557637Sandrew mss = ifp->if_mtu - sizeof(struct tcpiphdr); 155631726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 155744375Skarels if (mss > MCLBYTES) 155844375Skarels mss &= ~(MCLBYTES-1); 155917272Skarels #else 156044375Skarels if (mss > MCLBYTES) 156144375Skarels mss = mss / MCLBYTES * MCLBYTES; 156217272Skarels #endif 156344375Skarels if (!in_localaddr(inp->inp_faddr)) 156457637Sandrew mss = min(mss, tcp_mssdflt); 156544375Skarels } 156644375Skarels /* 156744375Skarels * The current mss, t_maxseg, is initialized to the default value. 156844375Skarels * If we compute a smaller value, reduce the current mss. 156944375Skarels * If we compute a larger value, return it for use in sending 157044375Skarels * a max seg size option, but don't store it for use 157144375Skarels * unless we received an offer at least that large from peer. 157244375Skarels * However, do not accept offers under 32 bytes. 157344375Skarels */ 157444375Skarels if (offer) 157544375Skarels mss = min(mss, offer); 157644375Skarels mss = max(mss, 32); /* sanity */ 157744375Skarels if (mss < tp->t_maxseg || offer != 0) { 157844375Skarels /* 157944375Skarels * If there's a pipesize, change the socket buffer 158044375Skarels * to that size. Make the socket buffers an integral 158144375Skarels * number of mss units; if the mss is larger than 158244375Skarels * the socket buffer, decrease the mss. 158344375Skarels */ 158444375Skarels #ifdef RTV_SPIPE 158544375Skarels if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 158644375Skarels #endif 158744375Skarels bufsize = so->so_snd.sb_hiwat; 158844375Skarels if (bufsize < mss) 158944375Skarels mss = bufsize; 159044375Skarels else { 159156908Storek bufsize = roundup(bufsize, mss); 159257433Sandrew if (bufsize > sb_max) 159357433Sandrew bufsize = sb_max; 159456908Storek (void)sbreserve(&so->so_snd, bufsize); 159544375Skarels } 159644375Skarels tp->t_maxseg = mss; 159732098Skarels 159844375Skarels #ifdef RTV_RPIPE 159944375Skarels if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 160044375Skarels #endif 160144375Skarels bufsize = so->so_rcv.sb_hiwat; 160244375Skarels if (bufsize > mss) { 160356908Storek bufsize = roundup(bufsize, mss); 160457433Sandrew if (bufsize > sb_max) 160557433Sandrew bufsize = sb_max; 160656908Storek (void)sbreserve(&so->so_rcv, bufsize); 160744375Skarels } 160844375Skarels } 160932034Skarels tp->snd_cwnd = mss; 161044375Skarels 161144375Skarels #ifdef RTV_SSTHRESH 161244375Skarels if (rt->rt_rmx.rmx_ssthresh) { 161344375Skarels /* 161444375Skarels * There's some sort of gateway or interface 161544375Skarels * buffer limit on the path. Use this to set 161644375Skarels * the slow start threshhold, but set the 161744375Skarels * threshold to no less than 2*mss. 161844375Skarels */ 161944375Skarels tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 162044375Skarels } 162144375Skarels #endif /* RTV_MTU */ 162232034Skarels return (mss); 162317272Skarels } 162457947Ssklower #endif /* TUBA_INCLUDE */ 1625