123190Smckusick /*
269644Skarels * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
363218Sbostic * The Regents of the University of California. All rights reserved.
423190Smckusick *
544485Sbostic * %sccs.include.redist.c%
632787Sbostic *
7*69655Skarels * @(#)tcp_input.c 8.12 (Berkeley) 05/24/95
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
2069644Skarels #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
2169644Skarels
2256531Sbostic #include <net/if.h>
2356531Sbostic #include <net/route.h>
2410894Ssam
2556531Sbostic #include <netinet/in.h>
2656531Sbostic #include <netinet/in_systm.h>
2756531Sbostic #include <netinet/ip.h>
2856531Sbostic #include <netinet/in_pcb.h>
2956531Sbostic #include <netinet/ip_var.h>
3056531Sbostic #include <netinet/tcp.h>
3156531Sbostic #include <netinet/tcp_fsm.h>
3256531Sbostic #include <netinet/tcp_seq.h>
3356531Sbostic #include <netinet/tcp_timer.h>
3456531Sbostic #include <netinet/tcp_var.h>
3556531Sbostic #include <netinet/tcpip.h>
3656531Sbostic #include <netinet/tcp_debug.h>
374601Swnj
3832098Skarels int tcprexmtthresh = 3;
395267Sroot struct tcpiphdr tcp_saveti;
4044375Skarels struct inpcb *tcp_last_inpcb = &tcb;
414601Swnj
4257947Ssklower extern u_long sb_max;
4357947Ssklower
4457947Ssklower #endif /* TUBA_INCLUDE */
4557433Sandrew #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
4657433Sandrew
4757433Sandrew /* for modulo comparisons of timestamps */
4857433Sandrew #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
4957433Sandrew #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
5057433Sandrew
5124816Skarels
525065Swnj /*
5324816Skarels * Insert segment ti into reassembly queue of tcp with
5424816Skarels * control block tp. Return TH_FIN if reassembly now includes
5524816Skarels * a segment with FIN. The macro form does the common case inline
5624816Skarels * (segment is the next to be received on an established connection,
5724816Skarels * and the queue is empty), avoiding linkage into and removal
5824816Skarels * from the queue and repetition of various conversions.
5934278Skarels * Set DELACK for segments received in order, but ack immediately
6034278Skarels * when segments are out of order (so fast retransmit can work).
6124816Skarels */
6224816Skarels #define TCP_REASS(tp, ti, m, so, flags) { \
6324816Skarels if ((ti)->ti_seq == (tp)->rcv_nxt && \
6424816Skarels (tp)->seg_next == (struct tcpiphdr *)(tp) && \
6524816Skarels (tp)->t_state == TCPS_ESTABLISHED) { \
6669176Smckusick tp->t_flags |= TF_DELACK; \
6724816Skarels (tp)->rcv_nxt += (ti)->ti_len; \
6824816Skarels flags = (ti)->ti_flags & TH_FIN; \
6930525Skarels tcpstat.tcps_rcvpack++;\
7030525Skarels tcpstat.tcps_rcvbyte += (ti)->ti_len;\
7124816Skarels sbappend(&(so)->so_rcv, (m)); \
7224816Skarels sorwakeup(so); \
7334278Skarels } else { \
7444375Skarels (flags) = tcp_reass((tp), (ti), (m)); \
7534278Skarels tp->t_flags |= TF_ACKNOW; \
7634278Skarels } \
7724816Skarels }
7857947Ssklower #ifndef TUBA_INCLUDE
7924816Skarels
8061335Sbostic int
tcp_reass(tp,ti,m)8144375Skarels tcp_reass(tp, ti, m)
8224816Skarels register struct tcpcb *tp;
8324816Skarels register struct tcpiphdr *ti;
8444375Skarels struct mbuf *m;
8524816Skarels {
8624816Skarels register struct tcpiphdr *q;
8724816Skarels struct socket *so = tp->t_inpcb->inp_socket;
8824816Skarels int flags;
8924816Skarels
9024816Skarels /*
9124816Skarels * Call with ti==0 after become established to
9224816Skarels * force pre-ESTABLISHED data up to user socket.
9324816Skarels */
9424816Skarels if (ti == 0)
9524816Skarels goto present;
9624816Skarels
9724816Skarels /*
9824816Skarels * Find a segment which begins after this one does.
9924816Skarels */
10024816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
10124816Skarels q = (struct tcpiphdr *)q->ti_next)
10224816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq))
10324816Skarels break;
10424816Skarels
10524816Skarels /*
10624816Skarels * If there is a preceding segment, it may provide some of
10724816Skarels * our data already. If so, drop the data from the incoming
10824816Skarels * segment. If it provides all of our data, drop us.
10924816Skarels */
11024816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
11124816Skarels register int i;
11224816Skarels q = (struct tcpiphdr *)q->ti_prev;
11324816Skarels /* conversion to int (in i) handles seq wraparound */
11424816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq;
11524816Skarels if (i > 0) {
11630525Skarels if (i >= ti->ti_len) {
11730525Skarels tcpstat.tcps_rcvduppack++;
11830525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len;
11944375Skarels m_freem(m);
12044375Skarels return (0);
12130525Skarels }
12244375Skarels m_adj(m, i);
12324816Skarels ti->ti_len -= i;
12424816Skarels ti->ti_seq += i;
12524816Skarels }
12624816Skarels q = (struct tcpiphdr *)(q->ti_next);
12724816Skarels }
12830525Skarels tcpstat.tcps_rcvoopack++;
12930525Skarels tcpstat.tcps_rcvoobyte += ti->ti_len;
13044375Skarels REASS_MBUF(ti) = m; /* XXX */
13124816Skarels
13224816Skarels /*
13324816Skarels * While we overlap succeeding segments trim them or,
13424816Skarels * if they are completely covered, dequeue them.
13524816Skarels */
13624816Skarels while (q != (struct tcpiphdr *)tp) {
13724816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
13824816Skarels if (i <= 0)
13924816Skarels break;
14024816Skarels if (i < q->ti_len) {
14124816Skarels q->ti_seq += i;
14224816Skarels q->ti_len -= i;
14344375Skarels m_adj(REASS_MBUF(q), i);
14424816Skarels break;
14524816Skarels }
14624816Skarels q = (struct tcpiphdr *)q->ti_next;
14744375Skarels m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
14824816Skarels remque(q->ti_prev);
14924816Skarels m_freem(m);
15024816Skarels }
15124816Skarels
15224816Skarels /*
15324816Skarels * Stick new segment in its place.
15424816Skarels */
15524816Skarels insque(ti, q->ti_prev);
15624816Skarels
15724816Skarels present:
15824816Skarels /*
15924816Skarels * Present data to user, advancing rcv_nxt through
16024816Skarels * completed sequence space.
16124816Skarels */
16224816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
16324816Skarels return (0);
16424816Skarels ti = tp->seg_next;
16524816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
16624816Skarels return (0);
16724816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
16824816Skarels return (0);
16924816Skarels do {
17024816Skarels tp->rcv_nxt += ti->ti_len;
17124816Skarels flags = ti->ti_flags & TH_FIN;
17224816Skarels remque(ti);
17344375Skarels m = REASS_MBUF(ti);
17424816Skarels ti = (struct tcpiphdr *)ti->ti_next;
17524816Skarels if (so->so_state & SS_CANTRCVMORE)
17624816Skarels m_freem(m);
17724816Skarels else
17824816Skarels sbappend(&so->so_rcv, m);
17924816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
18024816Skarels sorwakeup(so);
18124816Skarels return (flags);
18224816Skarels }
18324816Skarels
18424816Skarels /*
1855065Swnj * TCP input routine, follows pages 65-76 of the
1865065Swnj * protocol specification dated September, 1981 very closely.
1875065Swnj */
18861335Sbostic void
tcp_input(m,iphlen)18937320Skarels tcp_input(m, iphlen)
19037320Skarels register struct mbuf *m;
19137320Skarels int iphlen;
1924601Swnj {
1934924Swnj register struct tcpiphdr *ti;
19444375Skarels register struct inpcb *inp;
19567588Smckusick u_char *optp = NULL;
19657433Sandrew int optlen;
1974924Swnj int len, tlen, off;
1985391Swnj register struct tcpcb *tp = 0;
1994924Swnj register int tiflags;
2004803Swnj struct socket *so;
20131721Skarels int todrop, acked, ourfinisacked, needoutput = 0;
2025267Sroot short ostate;
2036028Sroot struct in_addr laddr;
20410769Ssam int dropsocket = 0;
20530525Skarels int iss = 0;
20657433Sandrew u_long tiwin, ts_val, ts_ecr;
20757433Sandrew int ts_present = 0;
2084924Swnj
20930525Skarels tcpstat.tcps_rcvtotal++;
2104924Swnj /*
2115244Sroot * Get IP and TCP header together in first mbuf.
2125244Sroot * Note: IP leaves IP header in first mbuf.
2134924Swnj */
2145020Sroot ti = mtod(m, struct tcpiphdr *);
21537320Skarels if (iphlen > sizeof (struct ip))
21637320Skarels ip_stripoptions(m, (struct mbuf *)0);
21744375Skarels if (m->m_len < sizeof (struct tcpiphdr)) {
2185307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
21930525Skarels tcpstat.tcps_rcvshort++;
2205307Sroot return;
2215085Swnj }
2225085Swnj ti = mtod(m, struct tcpiphdr *);
2235085Swnj }
2244601Swnj
2254601Swnj /*
2265244Sroot * Checksum extended TCP header and data.
2274601Swnj */
2284924Swnj tlen = ((struct ip *)ti)->ip_len;
2294924Swnj len = sizeof (struct ip) + tlen;
23037320Skarels ti->ti_next = ti->ti_prev = 0;
23137320Skarels ti->ti_x1 = 0;
23237320Skarels ti->ti_len = (u_short)tlen;
23344375Skarels HTONS(ti->ti_len);
23437320Skarels if (ti->ti_sum = in_cksum(m, len)) {
23537320Skarels tcpstat.tcps_rcvbadsum++;
23637320Skarels goto drop;
2374601Swnj }
23857947Ssklower #endif /* TUBA_INCLUDE */
2394601Swnj
2404601Swnj /*
2415244Sroot * Check that TCP offset makes sense,
24244375Skarels * pull out TCP options and adjust length. XXX
2434601Swnj */
2444924Swnj off = ti->ti_off << 2;
2455231Swnj if (off < sizeof (struct tcphdr) || off > tlen) {
24630525Skarels tcpstat.tcps_rcvbadoff++;
2475085Swnj goto drop;
2484924Swnj }
2496211Swnj tlen -= off;
2506211Swnj ti->ti_len = tlen;
2515440Swnj if (off > sizeof (struct tcphdr)) {
25224816Skarels if (m->m_len < sizeof(struct ip) + off) {
25324816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
25430525Skarels tcpstat.tcps_rcvshort++;
25524816Skarels return;
25624816Skarels }
25724816Skarels ti = mtod(m, struct tcpiphdr *);
2585440Swnj }
25957433Sandrew optlen = off - sizeof (struct tcphdr);
26067588Smckusick optp = mtod(m, u_char *) + sizeof (struct tcpiphdr);
26157433Sandrew /*
26257433Sandrew * Do quick retrieval of timestamp options ("options
26357433Sandrew * prediction?"). If timestamp is the only option and it's
26457433Sandrew * formatted as recommended in RFC 1323 appendix A, we
26557433Sandrew * quickly get the values now and not bother calling
26657433Sandrew * tcp_dooptions(), etc.
26757433Sandrew */
26857433Sandrew if ((optlen == TCPOLEN_TSTAMP_APPA ||
26957433Sandrew (optlen > TCPOLEN_TSTAMP_APPA &&
27057433Sandrew optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
27157433Sandrew *(u_long *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
27257433Sandrew (ti->ti_flags & TH_SYN) == 0) {
27357433Sandrew ts_present = 1;
27457433Sandrew ts_val = ntohl(*(u_long *)(optp + 4));
27557433Sandrew ts_ecr = ntohl(*(u_long *)(optp + 8));
27657433Sandrew optp = NULL; /* we've parsed the options */
2775440Swnj }
2785440Swnj }
2795065Swnj tiflags = ti->ti_flags;
2804924Swnj
2816093Sroot /*
2825244Sroot * Convert TCP protocol specific fields to host format.
2835085Swnj */
28444375Skarels NTOHL(ti->ti_seq);
28544375Skarels NTOHL(ti->ti_ack);
28644375Skarels NTOHS(ti->ti_win);
28744375Skarels NTOHS(ti->ti_urp);
2885085Swnj
2895085Swnj /*
2908271Sroot * Locate pcb for segment.
2914924Swnj */
29230525Skarels findpcb:
29344375Skarels inp = tcp_last_inpcb;
29444375Skarels if (inp->inp_lport != ti->ti_dport ||
29544375Skarels inp->inp_fport != ti->ti_sport ||
29644375Skarels inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
29744375Skarels inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
29844375Skarels inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
29944375Skarels ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
30044375Skarels if (inp)
30144375Skarels tcp_last_inpcb = inp;
30266740Sbostic ++tcpstat.tcps_pcbcachemiss;
30344375Skarels }
3045065Swnj
3055065Swnj /*
3065065Swnj * If the state is CLOSED (i.e., TCB does not exist) then
3075244Sroot * all data in the incoming segment is discarded.
30832098Skarels * If the TCB exists but is in CLOSED state, it is embryonic,
30932098Skarels * but should either do a listen or a connect soon.
3105065Swnj */
3115300Sroot if (inp == 0)
3125085Swnj goto dropwithreset;
3135065Swnj tp = intotcpcb(inp);
3145300Sroot if (tp == 0)
3155085Swnj goto dropwithreset;
31632098Skarels if (tp->t_state == TCPS_CLOSED)
31732098Skarels goto drop;
31857433Sandrew
31957433Sandrew /* Unscale the window into a 32-bit value. */
32057433Sandrew if ((tiflags & TH_SYN) == 0)
32157433Sandrew tiwin = ti->ti_win << tp->snd_scale;
32257433Sandrew else
32357433Sandrew tiwin = ti->ti_win;
32457433Sandrew
3255109Swnj so = inp->inp_socket;
32644375Skarels if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
32744375Skarels if (so->so_options & SO_DEBUG) {
32844375Skarels ostate = tp->t_state;
32944375Skarels tcp_saveti = *ti;
33044375Skarels }
33144375Skarels if (so->so_options & SO_ACCEPTCONN) {
33268512Sdab if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
33368512Sdab /*
33468512Sdab * Note: dropwithreset makes sure we don't
33568512Sdab * send a reset in response to a RST.
33668512Sdab */
33769644Skarels if (tiflags & TH_ACK) {
33869644Skarels tcpstat.tcps_badsyn++;
33968512Sdab goto dropwithreset;
34069644Skarels }
34168512Sdab goto drop;
34268512Sdab }
34344375Skarels so = sonewconn(so, 0);
34444375Skarels if (so == 0)
34544375Skarels goto drop;
34644375Skarels /*
34744375Skarels * This is ugly, but ....
34844375Skarels *
34944375Skarels * Mark socket as temporary until we're
35044375Skarels * committed to keeping it. The code at
35144375Skarels * ``drop'' and ``dropwithreset'' check the
35244375Skarels * flag dropsocket to see if the temporary
35344375Skarels * socket created here should be discarded.
35444375Skarels * We mark the socket as discardable until
35544375Skarels * we're committed to it below in TCPS_LISTEN.
35644375Skarels */
35744375Skarels dropsocket++;
35844375Skarels inp = (struct inpcb *)so->so_pcb;
35944375Skarels inp->inp_laddr = ti->ti_dst;
36044375Skarels inp->inp_lport = ti->ti_dport;
36144375Skarels #if BSD>=43
36244375Skarels inp->inp_options = ip_srcroute();
36344375Skarels #endif
36444375Skarels tp = intotcpcb(inp);
36544375Skarels tp->t_state = TCPS_LISTEN;
36657433Sandrew
36757433Sandrew /* Compute proper scaling value from buffer space
36857433Sandrew */
36957433Sandrew while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
37057433Sandrew TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
37157433Sandrew tp->request_r_scale++;
37244375Skarels }
3735267Sroot }
3744601Swnj
3754601Swnj /*
3765162Swnj * Segment received on connection.
3775162Swnj * Reset idle time and keep-alive timer.
3785162Swnj */
3795162Swnj tp->t_idle = 0;
38033745Skarels tp->t_timer[TCPT_KEEP] = tcp_keepidle;
3815162Swnj
3825162Swnj /*
38317272Skarels * Process options if not in LISTEN state,
38417272Skarels * else do it below (after getting remote address).
3855440Swnj */
38657433Sandrew if (optp && tp->t_state != TCPS_LISTEN)
38757433Sandrew tcp_dooptions(tp, optp, optlen, ti,
38857433Sandrew &ts_present, &ts_val, &ts_ecr);
38957433Sandrew
39044375Skarels /*
39144375Skarels * Header prediction: check for the two common cases
39244375Skarels * of a uni-directional data xfer. If the packet has
39344375Skarels * no control flags, is in-sequence, the window didn't
39444375Skarels * change and we're not retransmitting, it's a
39544375Skarels * candidate. If the length is zero and the ack moved
39644375Skarels * forward, we're the sender side of the xfer. Just
39744375Skarels * free the data acked & wake any higher level process
39844375Skarels * that was blocked waiting for space. If the length
39944375Skarels * is non-zero and the ack didn't move, we're the
40044375Skarels * receiver side. If we're getting packets in-order
40144375Skarels * (the reassembly queue is empty), add the data to
40244375Skarels * the socket buffer and note that we need a delayed ack.
40344375Skarels */
40444375Skarels if (tp->t_state == TCPS_ESTABLISHED &&
40544375Skarels (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
40657433Sandrew (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) &&
40744375Skarels ti->ti_seq == tp->rcv_nxt &&
40857433Sandrew tiwin && tiwin == tp->snd_wnd &&
40944375Skarels tp->snd_nxt == tp->snd_max) {
41057433Sandrew
41157433Sandrew /*
41257433Sandrew * If last ACK falls within this segment's sequence numbers,
41357433Sandrew * record the timestamp.
41457433Sandrew */
41557433Sandrew if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
41657433Sandrew SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
41757433Sandrew tp->ts_recent_age = tcp_now;
41857433Sandrew tp->ts_recent = ts_val;
41957433Sandrew }
42057433Sandrew
42144375Skarels if (ti->ti_len == 0) {
42244375Skarels if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
42344375Skarels SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
42444375Skarels tp->snd_cwnd >= tp->snd_wnd) {
42544375Skarels /*
42644375Skarels * this is a pure ack for outstanding data.
42744375Skarels */
42866740Sbostic ++tcpstat.tcps_predack;
42957433Sandrew if (ts_present)
43057433Sandrew tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
43157433Sandrew else if (tp->t_rtt &&
43257433Sandrew SEQ_GT(ti->ti_ack, tp->t_rtseq))
43357433Sandrew tcp_xmit_timer(tp, tp->t_rtt);
43444375Skarels acked = ti->ti_ack - tp->snd_una;
43544375Skarels tcpstat.tcps_rcvackpack++;
43644375Skarels tcpstat.tcps_rcvackbyte += acked;
43744375Skarels sbdrop(&so->so_snd, acked);
43844375Skarels tp->snd_una = ti->ti_ack;
43944375Skarels m_freem(m);
4405440Swnj
44144375Skarels /*
44244375Skarels * If all outstanding data are acked, stop
44344375Skarels * retransmit timer, otherwise restart timer
44444375Skarels * using current (possibly backed-off) value.
44544375Skarels * If process is waiting for space,
44644375Skarels * wakeup/selwakeup/signal. If data
44744375Skarels * are ready to send, let tcp_output
44844375Skarels * decide between more output or persist.
44944375Skarels */
45044375Skarels if (tp->snd_una == tp->snd_max)
45144375Skarels tp->t_timer[TCPT_REXMT] = 0;
45244375Skarels else if (tp->t_timer[TCPT_PERSIST] == 0)
45344375Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
45444375Skarels
45544375Skarels if (so->so_snd.sb_flags & SB_NOTIFY)
45644375Skarels sowwakeup(so);
45744375Skarels if (so->so_snd.sb_cc)
45844375Skarels (void) tcp_output(tp);
45944375Skarels return;
46044375Skarels }
46144375Skarels } else if (ti->ti_ack == tp->snd_una &&
46244375Skarels tp->seg_next == (struct tcpiphdr *)tp &&
46344375Skarels ti->ti_len <= sbspace(&so->so_rcv)) {
46444375Skarels /*
46544375Skarels * this is a pure, in-sequence data packet
46644375Skarels * with nothing on the reassembly queue and
46744375Skarels * we have enough buffer space to take it.
46844375Skarels */
46966740Sbostic ++tcpstat.tcps_preddat;
47044375Skarels tp->rcv_nxt += ti->ti_len;
47144375Skarels tcpstat.tcps_rcvpack++;
47244375Skarels tcpstat.tcps_rcvbyte += ti->ti_len;
47344375Skarels /*
47457433Sandrew * Drop TCP, IP headers and TCP options then add data
47557433Sandrew * to socket buffer.
47644375Skarels */
47757433Sandrew m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
47857433Sandrew m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
47944375Skarels sbappend(&so->so_rcv, m);
48044375Skarels sorwakeup(so);
48169176Smckusick tp->t_flags |= TF_DELACK;
48244375Skarels return;
48344375Skarels }
48444375Skarels }
48544375Skarels
4865440Swnj /*
48757433Sandrew * Drop TCP, IP headers and TCP options.
48844375Skarels */
48957433Sandrew m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
49057433Sandrew m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
49144375Skarels
49244375Skarels /*
4935085Swnj * Calculate amount of space in receive window,
4945085Swnj * and then do TCP input processing.
49524816Skarels * Receive window is amount of space in rcv queue,
49624816Skarels * but not less than advertised window.
4974601Swnj */
49825939Skarels { int win;
4994601Swnj
50025939Skarels win = sbspace(&so->so_rcv);
50125939Skarels if (win < 0)
50225939Skarels win = 0;
50337320Skarels tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
50425939Skarels }
50525939Skarels
5064601Swnj switch (tp->t_state) {
5074601Swnj
5085065Swnj /*
5095065Swnj * If the state is LISTEN then ignore segment if it contains an RST.
5105065Swnj * If the segment contains an ACK then it is bad and send a RST.
5115065Swnj * If it does not contain a SYN then it is not interesting; drop it.
51225197Skarels * Don't bother responding if the destination was a broadcast.
5135085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
5145065Swnj * tp->iss, and send a segment:
5155085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
5165065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
5175065Swnj * Fill in remote peer address fields if not previously specified.
5185065Swnj * Enter SYN_RECEIVED state, and process any other fields of this
5195244Sroot * segment in this state.
5205065Swnj */
5218271Sroot case TCPS_LISTEN: {
52210145Ssam struct mbuf *am;
5238271Sroot register struct sockaddr_in *sin;
5248271Sroot
52569644Skarels #ifdef already_done
5265065Swnj if (tiflags & TH_RST)
5275065Swnj goto drop;
5285300Sroot if (tiflags & TH_ACK)
5295085Swnj goto dropwithreset;
5305300Sroot if ((tiflags & TH_SYN) == 0)
5315065Swnj goto drop;
53269644Skarels #endif
53358998Ssklower /*
53458998Ssklower * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
53558998Ssklower * in_broadcast() should never return true on a received
53658998Ssklower * packet with M_BCAST not set.
53758998Ssklower */
53857433Sandrew if (m->m_flags & (M_BCAST|M_MCAST) ||
53967588Smckusick IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
54025197Skarels goto drop;
54144375Skarels am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
54210145Ssam if (am == NULL)
54310145Ssam goto drop;
54410145Ssam am->m_len = sizeof (struct sockaddr_in);
5458599Sroot sin = mtod(am, struct sockaddr_in *);
5468271Sroot sin->sin_family = AF_INET;
54737320Skarels sin->sin_len = sizeof(*sin);
5488271Sroot sin->sin_addr = ti->ti_src;
5498271Sroot sin->sin_port = ti->ti_sport;
55056399Ssklower bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
5516028Sroot laddr = inp->inp_laddr;
55210145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY)
5536028Sroot inp->inp_laddr = ti->ti_dst;
5548599Sroot if (in_pcbconnect(inp, am)) {
5556028Sroot inp->inp_laddr = laddr;
5568716Sroot (void) m_free(am);
5575244Sroot goto drop;
5586028Sroot }
5598716Sroot (void) m_free(am);
5605244Sroot tp->t_template = tcp_template(tp);
5615244Sroot if (tp->t_template == 0) {
56226386Skarels tp = tcp_drop(tp, ENOBUFS);
56317264Skarels dropsocket = 0; /* socket is already gone */
5645244Sroot goto drop;
5655244Sroot }
56657433Sandrew if (optp)
56757433Sandrew tcp_dooptions(tp, optp, optlen, ti,
56857433Sandrew &ts_present, &ts_val, &ts_ecr);
56930525Skarels if (iss)
57030525Skarels tp->iss = iss;
57130525Skarels else
57230525Skarels tp->iss = tcp_iss;
57369644Skarels tcp_iss += TCP_ISSINCR/4;
5745065Swnj tp->irs = ti->ti_seq;
5755085Swnj tcp_sendseqinit(tp);
5765085Swnj tcp_rcvseqinit(tp);
57725939Skarels tp->t_flags |= TF_ACKNOW;
5785065Swnj tp->t_state = TCPS_SYN_RECEIVED;
57933745Skarels tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
58010769Ssam dropsocket = 0; /* committed to socket */
58130525Skarels tcpstat.tcps_accepts++;
5825085Swnj goto trimthenstep6;
5838271Sroot }
5844601Swnj
5855065Swnj /*
5865065Swnj * If the state is SYN_SENT:
5875065Swnj * if seg contains an ACK, but not for our SYN, drop the input.
5885065Swnj * if seg contains a RST, then drop the connection.
5895065Swnj * if seg does not contain SYN, then drop it.
5905065Swnj * Otherwise this is an acceptable SYN segment
5915065Swnj * initialize tp->rcv_nxt and tp->irs
5925065Swnj * if seg contains ack then advance tp->snd_una
5935065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
5945065Swnj * arrange for segment to be acked (eventually)
5955065Swnj * continue processing rest of data/controls, beginning with URG
5965065Swnj */
5975065Swnj case TCPS_SYN_SENT:
5985065Swnj if ((tiflags & TH_ACK) &&
59924816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) ||
6005231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)))
6015085Swnj goto dropwithreset;
6025065Swnj if (tiflags & TH_RST) {
60310394Ssam if (tiflags & TH_ACK)
60410394Ssam tp = tcp_drop(tp, ECONNREFUSED);
6055065Swnj goto drop;
6064601Swnj }
6075065Swnj if ((tiflags & TH_SYN) == 0)
6085065Swnj goto drop;
60930974Skarels if (tiflags & TH_ACK) {
61030974Skarels tp->snd_una = ti->ti_ack;
61130974Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_una))
61230974Skarels tp->snd_nxt = tp->snd_una;
61330974Skarels }
6145244Sroot tp->t_timer[TCPT_REXMT] = 0;
6155065Swnj tp->irs = ti->ti_seq;
6165085Swnj tcp_rcvseqinit(tp);
6175085Swnj tp->t_flags |= TF_ACKNOW;
61830974Skarels if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
61930525Skarels tcpstat.tcps_connects++;
6205244Sroot soisconnected(so);
6215065Swnj tp->t_state = TCPS_ESTABLISHED;
62257433Sandrew /* Do window scaling on this connection? */
62357433Sandrew if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
62457433Sandrew (TF_RCVD_SCALE|TF_REQ_SCALE)) {
62557433Sandrew tp->snd_scale = tp->requested_s_scale;
62657433Sandrew tp->rcv_scale = tp->request_r_scale;
62757433Sandrew }
62844375Skarels (void) tcp_reass(tp, (struct tcpiphdr *)0,
62944375Skarels (struct mbuf *)0);
63032098Skarels /*
63132098Skarels * if we didn't have to retransmit the SYN,
63232098Skarels * use its rtt as our initial srtt & rtt var.
63332098Skarels */
63444375Skarels if (tp->t_rtt)
63557433Sandrew tcp_xmit_timer(tp, tp->t_rtt);
6365162Swnj } else
6375085Swnj tp->t_state = TCPS_SYN_RECEIVED;
6385085Swnj
6395085Swnj trimthenstep6:
6405085Swnj /*
6415231Swnj * Advance ti->ti_seq to correspond to first data byte.
6425085Swnj * If data, trim to stay within window,
6435085Swnj * dropping FIN if necessary.
6445085Swnj */
6455231Swnj ti->ti_seq++;
6465085Swnj if (ti->ti_len > tp->rcv_wnd) {
6475085Swnj todrop = ti->ti_len - tp->rcv_wnd;
6485085Swnj m_adj(m, -todrop);
6495085Swnj ti->ti_len = tp->rcv_wnd;
65025939Skarels tiflags &= ~TH_FIN;
65130525Skarels tcpstat.tcps_rcvpackafterwin++;
65230525Skarels tcpstat.tcps_rcvbyteafterwin += todrop;
6535065Swnj }
6545263Swnj tp->snd_wl1 = ti->ti_seq - 1;
65525939Skarels tp->rcv_up = ti->ti_seq;
6565085Swnj goto step6;
6575065Swnj }
6584601Swnj
6595065Swnj /*
66030525Skarels * States other than LISTEN or SYN_SENT.
66157433Sandrew * First check timestamp, if present.
66257433Sandrew * Then check that at least some bytes of segment are within
66330525Skarels * receive window. If segment begins before rcv_nxt,
66430525Skarels * drop leading data (and SYN); if nothing left, just ack.
66557433Sandrew *
66657433Sandrew * RFC 1323 PAWS: If we have a timestamp reply on this segment
66757433Sandrew * and it's less than ts_recent, drop it.
66816222Skarels */
66957433Sandrew if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
67057433Sandrew TSTMP_LT(ts_val, tp->ts_recent)) {
67157433Sandrew
67257433Sandrew /* Check to see if ts_recent is over 24 days old. */
67357433Sandrew if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
67457433Sandrew /*
67557433Sandrew * Invalidate ts_recent. If this segment updates
67657433Sandrew * ts_recent, the age will be reset later and ts_recent
67757433Sandrew * will get a valid value. If it does not, setting
67857433Sandrew * ts_recent to zero will at least satisfy the
67957433Sandrew * requirement that zero be placed in the timestamp
68057433Sandrew * echo reply when ts_recent isn't valid. The
68157433Sandrew * age isn't reset until we get a valid ts_recent
68257433Sandrew * because we don't want out-of-order segments to be
68357433Sandrew * dropped when ts_recent is old.
68457433Sandrew */
68557433Sandrew tp->ts_recent = 0;
68657433Sandrew } else {
68757433Sandrew tcpstat.tcps_rcvduppack++;
68857433Sandrew tcpstat.tcps_rcvdupbyte += ti->ti_len;
68957433Sandrew tcpstat.tcps_pawsdrop++;
69057433Sandrew goto dropafterack;
69157433Sandrew }
69257433Sandrew }
69357433Sandrew
69430525Skarels todrop = tp->rcv_nxt - ti->ti_seq;
69530525Skarels if (todrop > 0) {
69630525Skarels if (tiflags & TH_SYN) {
69730525Skarels tiflags &= ~TH_SYN;
69830525Skarels ti->ti_seq++;
69930525Skarels if (ti->ti_urp > 1)
70030525Skarels ti->ti_urp--;
70130525Skarels else
70230525Skarels tiflags &= ~TH_URG;
70330525Skarels todrop--;
70430525Skarels }
70564176Smckusick if (todrop >= ti->ti_len) {
70633745Skarels tcpstat.tcps_rcvduppack++;
70733745Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len;
70831730Skarels /*
70933745Skarels * If segment is just one to the left of the window,
71033745Skarels * check two special cases:
71133745Skarels * 1. Don't toss RST in response to 4.2-style keepalive.
71233745Skarels * 2. If the only thing to drop is a FIN, we can drop
71333745Skarels * it, but check the ACK or we will get into FIN
71433745Skarels * wars if our FINs crossed (both CLOSING).
71533745Skarels * In either case, send ACK to resynchronize,
71633745Skarels * but keep on processing for RST or ACK.
71731730Skarels */
71833745Skarels if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
71933745Skarels #ifdef TCP_COMPAT_42
72033745Skarels || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
72131730Skarels #endif
72233745Skarels ) {
72333745Skarels todrop = ti->ti_len;
72433745Skarels tiflags &= ~TH_FIN;
72557433Sandrew } else {
72657433Sandrew /*
72757433Sandrew * Handle the case when a bound socket connects
72857433Sandrew * to itself. Allow packets with a SYN and
72957433Sandrew * an ACK to continue with the processing.
73057433Sandrew */
73157433Sandrew if (todrop != 0 || (tiflags & TH_ACK) == 0)
73257433Sandrew goto dropafterack;
73357433Sandrew }
73468512Sdab tp->t_flags |= TF_ACKNOW;
73532034Skarels } else {
73632034Skarels tcpstat.tcps_rcvpartduppack++;
73732034Skarels tcpstat.tcps_rcvpartdupbyte += todrop;
73830525Skarels }
73930525Skarels m_adj(m, todrop);
74030525Skarels ti->ti_seq += todrop;
74130525Skarels ti->ti_len -= todrop;
74230525Skarels if (ti->ti_urp > todrop)
74330525Skarels ti->ti_urp -= todrop;
74430525Skarels else {
74530525Skarels tiflags &= ~TH_URG;
74630525Skarels ti->ti_urp = 0;
74730525Skarels }
74816222Skarels }
74916222Skarels
75032612Skarels /*
75133745Skarels * If new data are received on a connection after the
75232612Skarels * user processes are gone, then RST the other end.
75332612Skarels */
75432612Skarels if ((so->so_state & SS_NOFDREF) &&
75532612Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
75632612Skarels tp = tcp_close(tp);
75732612Skarels tcpstat.tcps_rcvafterclose++;
75832612Skarels goto dropwithreset;
75932612Skarels }
76032612Skarels
76133445Skarels /*
76233445Skarels * If segment ends after window, drop trailing data
76333445Skarels * (and PUSH and FIN); if nothing left, just ACK.
76433445Skarels */
76533445Skarels todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
76633445Skarels if (todrop > 0) {
76733445Skarels tcpstat.tcps_rcvpackafterwin++;
76833445Skarels if (todrop >= ti->ti_len) {
76930525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
77033445Skarels /*
77133445Skarels * If a new connection request is received
77233445Skarels * while in TIME_WAIT, drop the old connection
77333445Skarels * and start over if the sequence numbers
77433445Skarels * are above the previous ones.
77533445Skarels */
77633445Skarels if (tiflags & TH_SYN &&
77733445Skarels tp->t_state == TCPS_TIME_WAIT &&
77833445Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
779*69655Skarels iss = tp->snd_nxt + TCP_ISSINCR;
78044375Skarels tp = tcp_close(tp);
78133445Skarels goto findpcb;
78233445Skarels }
78333445Skarels /*
78433445Skarels * If window is closed can only take segments at
78533445Skarels * window edge, and have to drop data and PUSH from
78633445Skarels * incoming segments. Continue processing, but
78733445Skarels * remember to ack. Otherwise, drop segment
78833445Skarels * and ack.
78933445Skarels */
79033445Skarels if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
79133445Skarels tp->t_flags |= TF_ACKNOW;
79230525Skarels tcpstat.tcps_rcvwinprobe++;
79333445Skarels } else
7945065Swnj goto dropafterack;
79533445Skarels } else
79630525Skarels tcpstat.tcps_rcvbyteafterwin += todrop;
79733445Skarels m_adj(m, -todrop);
79833445Skarels ti->ti_len -= todrop;
79933445Skarels tiflags &= ~(TH_PUSH|TH_FIN);
8005065Swnj }
8014601Swnj
8025065Swnj /*
80357433Sandrew * If last ACK falls within this segment's sequence numbers,
80457433Sandrew * record its timestamp.
80557433Sandrew */
80657433Sandrew if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
80757433Sandrew SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
80857433Sandrew ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
80957433Sandrew tp->ts_recent_age = tcp_now;
81057433Sandrew tp->ts_recent = ts_val;
81157433Sandrew }
81257433Sandrew
81357433Sandrew /*
8145065Swnj * If the RST bit is set examine the state:
8155065Swnj * SYN_RECEIVED STATE:
8165065Swnj * If passive open, return to LISTEN state.
8175065Swnj * If active open, inform user that connection was refused.
8185065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
8195065Swnj * Inform user that connection was reset, and close tcb.
8205065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES
8215065Swnj * Close the tcb.
8225065Swnj */
8235065Swnj if (tiflags&TH_RST) switch (tp->t_state) {
8245267Sroot
8255065Swnj case TCPS_SYN_RECEIVED:
82633745Skarels so->so_error = ECONNREFUSED;
82733745Skarels goto close;
8284601Swnj
8295065Swnj case TCPS_ESTABLISHED:
8305065Swnj case TCPS_FIN_WAIT_1:
8315065Swnj case TCPS_FIN_WAIT_2:
8325065Swnj case TCPS_CLOSE_WAIT:
83333745Skarels so->so_error = ECONNRESET;
83433745Skarels close:
83533745Skarels tp->t_state = TCPS_CLOSED;
83633745Skarels tcpstat.tcps_drops++;
83733745Skarels tp = tcp_close(tp);
8385065Swnj goto drop;
8395065Swnj
8405065Swnj case TCPS_CLOSING:
8415065Swnj case TCPS_LAST_ACK:
8425065Swnj case TCPS_TIME_WAIT:
84310394Ssam tp = tcp_close(tp);
8445065Swnj goto drop;
8454601Swnj }
8464601Swnj
8474601Swnj /*
8485065Swnj * If a SYN is in the window, then this is an
8495065Swnj * error and we send an RST and drop the connection.
8504601Swnj */
8515065Swnj if (tiflags & TH_SYN) {
85210394Ssam tp = tcp_drop(tp, ECONNRESET);
8535085Swnj goto dropwithreset;
8544601Swnj }
8554601Swnj
8564601Swnj /*
8575065Swnj * If the ACK bit is off we drop the segment and return.
8584601Swnj */
8595085Swnj if ((tiflags & TH_ACK) == 0)
8605065Swnj goto drop;
8615065Swnj
8625065Swnj /*
8635065Swnj * Ack processing.
8645065Swnj */
8654601Swnj switch (tp->t_state) {
8664601Swnj
8675065Swnj /*
8685065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter
86931721Skarels * ESTABLISHED state and continue processing, otherwise
8705065Swnj * send an RST.
8715065Swnj */
8725065Swnj case TCPS_SYN_RECEIVED:
8735085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
8745231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))
8755085Swnj goto dropwithreset;
87630525Skarels tcpstat.tcps_connects++;
8775085Swnj soisconnected(so);
8785085Swnj tp->t_state = TCPS_ESTABLISHED;
87957433Sandrew /* Do window scaling? */
88057433Sandrew if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
88157433Sandrew (TF_RCVD_SCALE|TF_REQ_SCALE)) {
88257433Sandrew tp->snd_scale = tp->requested_s_scale;
88357433Sandrew tp->rcv_scale = tp->request_r_scale;
88457433Sandrew }
88544375Skarels (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
8865244Sroot tp->snd_wl1 = ti->ti_seq - 1;
8875085Swnj /* fall into ... */
8884601Swnj
8895065Swnj /*
8905065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
8915065Swnj * ACKs. If the ack is in the range
8925231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max
8935065Swnj * then advance tp->snd_una to ti->ti_ack and drop
8945065Swnj * data from the retransmission queue. If this ACK reflects
8955065Swnj * more up to date window information we update our window information.
8965065Swnj */
8975065Swnj case TCPS_ESTABLISHED:
8985065Swnj case TCPS_FIN_WAIT_1:
8995065Swnj case TCPS_FIN_WAIT_2:
9005065Swnj case TCPS_CLOSE_WAIT:
9015065Swnj case TCPS_CLOSING:
9025244Sroot case TCPS_LAST_ACK:
9035244Sroot case TCPS_TIME_WAIT:
9045085Swnj
90530525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
90657433Sandrew if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
90730525Skarels tcpstat.tcps_rcvdupack++;
90832098Skarels /*
90944375Skarels * If we have outstanding data (other than
91044375Skarels * a window probe), this is a completely
91132098Skarels * duplicate ack (ie, window info didn't
91232098Skarels * change), the ack is the biggest we've
91332098Skarels * seen and we've seen exactly our rexmt
91432098Skarels * threshhold of them, assume a packet
91532098Skarels * has been dropped and retransmit it.
91632098Skarels * Kludge snd_nxt & the congestion
91732098Skarels * window so we send only this one
91844375Skarels * packet.
91944375Skarels *
92044375Skarels * We know we're losing at the current
92144375Skarels * window size so do congestion avoidance
92244375Skarels * (set ssthresh to half the current window
92344375Skarels * and pull our congestion window back to
92444375Skarels * the new ssthresh).
92544375Skarels *
92644375Skarels * Dup acks mean that packets have left the
92744375Skarels * network (they're now cached at the receiver)
92844375Skarels * so bump cwnd by the amount in the receiver
92944375Skarels * to keep a constant cwnd packets in the
93044375Skarels * network.
93132098Skarels */
93232098Skarels if (tp->t_timer[TCPT_REXMT] == 0 ||
93332098Skarels ti->ti_ack != tp->snd_una)
93432098Skarels tp->t_dupacks = 0;
93532098Skarels else if (++tp->t_dupacks == tcprexmtthresh) {
93632098Skarels tcp_seq onxt = tp->snd_nxt;
93732376Skarels u_int win =
93837320Skarels min(tp->snd_wnd, tp->snd_cwnd) / 2 /
93932376Skarels tp->t_maxseg;
94032098Skarels
94132376Skarels if (win < 2)
94232376Skarels win = 2;
94332376Skarels tp->snd_ssthresh = win * tp->t_maxseg;
94432098Skarels tp->t_timer[TCPT_REXMT] = 0;
94532098Skarels tp->t_rtt = 0;
94632098Skarels tp->snd_nxt = ti->ti_ack;
94732098Skarels tp->snd_cwnd = tp->t_maxseg;
94832098Skarels (void) tcp_output(tp);
94944375Skarels tp->snd_cwnd = tp->snd_ssthresh +
95044375Skarels tp->t_maxseg * tp->t_dupacks;
95132098Skarels if (SEQ_GT(onxt, tp->snd_nxt))
95232098Skarels tp->snd_nxt = onxt;
95332098Skarels goto drop;
95444375Skarels } else if (tp->t_dupacks > tcprexmtthresh) {
95544375Skarels tp->snd_cwnd += tp->t_maxseg;
95644375Skarels (void) tcp_output(tp);
95744375Skarels goto drop;
95832098Skarels }
95932098Skarels } else
96032098Skarels tp->t_dupacks = 0;
9615065Swnj break;
96230525Skarels }
96344375Skarels /*
96444375Skarels * If the congestion window was inflated to account
96544375Skarels * for the other side's cached packets, retract it.
96644375Skarels */
96744375Skarels if (tp->t_dupacks > tcprexmtthresh &&
96844375Skarels tp->snd_cwnd > tp->snd_ssthresh)
96944375Skarels tp->snd_cwnd = tp->snd_ssthresh;
97032098Skarels tp->t_dupacks = 0;
97130525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
97230525Skarels tcpstat.tcps_rcvacktoomuch++;
9735065Swnj goto dropafterack;
97430525Skarels }
9755085Swnj acked = ti->ti_ack - tp->snd_una;
97630525Skarels tcpstat.tcps_rcvackpack++;
97730525Skarels tcpstat.tcps_rcvackbyte += acked;
9785951Swnj
9795951Swnj /*
98057433Sandrew * If we have a timestamp reply, update smoothed
98157433Sandrew * round trip time. If no timestamp is present but
98257433Sandrew * transmit timer is running and timed sequence
9835951Swnj * number was acked, update smoothed round trip time.
98432034Skarels * Since we now have an rtt measurement, cancel the
98532034Skarels * timer backoff (cf., Phil Karn's retransmit alg.).
98632034Skarels * Recompute the initial retransmit timer.
9875951Swnj */
98857433Sandrew if (ts_present)
98957433Sandrew tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
99057433Sandrew else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
99157433Sandrew tcp_xmit_timer(tp,tp->t_rtt);
99231726Skarels
99326824Skarels /*
99426824Skarels * If all outstanding data is acked, stop retransmit
99526824Skarels * timer and remember to restart (more output or persist).
99626824Skarels * If there is more data to be acked, restart retransmit
99732034Skarels * timer, using current (possibly backed-off) value.
99826824Skarels */
99926824Skarels if (ti->ti_ack == tp->snd_max) {
10005244Sroot tp->t_timer[TCPT_REXMT] = 0;
100126824Skarels needoutput = 1;
100232034Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0)
100332034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
100417360Skarels /*
100532098Skarels * When new data is acked, open the congestion window.
100632098Skarels * If the window gives us less than ssthresh packets
100732098Skarels * in flight, open exponentially (maxseg per packet).
100844375Skarels * Otherwise open linearly: maxseg per window
100967870Smckusick * (maxseg * (maxseg / cwnd) per packet).
101017360Skarels */
101132098Skarels {
101244375Skarels register u_int cw = tp->snd_cwnd;
101344375Skarels register u_int incr = tp->t_maxseg;
101432098Skarels
101544375Skarels if (cw > tp->snd_ssthresh)
101667870Smckusick incr = incr * incr / cw;
101757433Sandrew tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
101832098Skarels }
10195307Sroot if (acked > so->so_snd.sb_cc) {
102015386Ssam tp->snd_wnd -= so->so_snd.sb_cc;
102126386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
102231721Skarels ourfinisacked = 1;
10235307Sroot } else {
10246161Ssam sbdrop(&so->so_snd, acked);
10255307Sroot tp->snd_wnd -= acked;
102631721Skarels ourfinisacked = 0;
10275307Sroot }
102844375Skarels if (so->so_snd.sb_flags & SB_NOTIFY)
102944375Skarels sowwakeup(so);
10305231Swnj tp->snd_una = ti->ti_ack;
10315357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una))
10325357Sroot tp->snd_nxt = tp->snd_una;
10335162Swnj
10344601Swnj switch (tp->t_state) {
10354601Swnj
10365065Swnj /*
10375065Swnj * In FIN_WAIT_1 STATE in addition to the processing
10385065Swnj * for the ESTABLISHED state if our FIN is now acknowledged
10395085Swnj * then enter FIN_WAIT_2.
10405065Swnj */
10415065Swnj case TCPS_FIN_WAIT_1:
10425896Swnj if (ourfinisacked) {
10435896Swnj /*
10445896Swnj * If we can't receive any more
10455896Swnj * data, then closing user can proceed.
104624816Skarels * Starting the timer is contrary to the
104724816Skarels * specification, but if we don't get a FIN
104824816Skarels * we'll hang forever.
10495896Swnj */
105024816Skarels if (so->so_state & SS_CANTRCVMORE) {
10515896Swnj soisdisconnected(so);
105233745Skarels tp->t_timer[TCPT_2MSL] = tcp_maxidle;
105324816Skarels }
10545085Swnj tp->t_state = TCPS_FIN_WAIT_2;
10555896Swnj }
10564601Swnj break;
10574601Swnj
10585065Swnj /*
10595065Swnj * In CLOSING STATE in addition to the processing for
10605065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN
10615065Swnj * then enter the TIME-WAIT state, otherwise ignore
10625065Swnj * the segment.
10635065Swnj */
10645065Swnj case TCPS_CLOSING:
10655244Sroot if (ourfinisacked) {
10665065Swnj tp->t_state = TCPS_TIME_WAIT;
10675244Sroot tcp_canceltimers(tp);
10685244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10695244Sroot soisdisconnected(so);
10705244Sroot }
10715244Sroot break;
10724601Swnj
10735065Swnj /*
107431743Skarels * In LAST_ACK, we may still be waiting for data to drain
107531743Skarels * and/or to be acked, as well as for the ack of our FIN.
107631743Skarels * If our FIN is now acknowledged, delete the TCB,
107731743Skarels * enter the closed state and return.
10785065Swnj */
10795065Swnj case TCPS_LAST_ACK:
108031743Skarels if (ourfinisacked) {
108110394Ssam tp = tcp_close(tp);
108231743Skarels goto drop;
108331743Skarels }
108431743Skarels break;
10854601Swnj
10865065Swnj /*
10875065Swnj * In TIME_WAIT state the only thing that should arrive
10885065Swnj * is a retransmission of the remote FIN. Acknowledge
10895065Swnj * it and restart the finack timer.
10905065Swnj */
10915065Swnj case TCPS_TIME_WAIT:
10925162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10935065Swnj goto dropafterack;
10944601Swnj }
10955085Swnj }
10964601Swnj
10975065Swnj step6:
10985065Swnj /*
10995244Sroot * Update window information.
110025939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN.
11015244Sroot */
110225939Skarels if ((tiflags & TH_ACK) &&
110325939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
11045391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
110557433Sandrew tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))) {
110630525Skarels /* keep track of pure window updates */
110730525Skarels if (ti->ti_len == 0 &&
110857433Sandrew tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
110930525Skarels tcpstat.tcps_rcvwinupd++;
111057433Sandrew tp->snd_wnd = tiwin;
11115244Sroot tp->snd_wl1 = ti->ti_seq;
11125244Sroot tp->snd_wl2 = ti->ti_ack;
111325260Skarels if (tp->snd_wnd > tp->max_sndwnd)
111425260Skarels tp->max_sndwnd = tp->snd_wnd;
111526824Skarels needoutput = 1;
111626824Skarels }
11175244Sroot
11185244Sroot /*
11195547Swnj * Process segments with URG.
11205065Swnj */
11217267Swnj if ((tiflags & TH_URG) && ti->ti_urp &&
11227267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) {
11235547Swnj /*
112425939Skarels * This is a kludge, but if we receive and accept
112513121Ssam * random urgent pointers, we'll crash in
112613121Ssam * soreceive. It's hard to imagine someone
112713121Ssam * actually wanting to send this much urgent data.
112812441Ssam */
112957433Sandrew if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
113012441Ssam ti->ti_urp = 0; /* XXX */
113112441Ssam tiflags &= ~TH_URG; /* XXX */
113225939Skarels goto dodata; /* XXX */
113312441Ssam }
113412441Ssam /*
11355547Swnj * If this segment advances the known urgent pointer,
11365547Swnj * then mark the data stream. This should not happen
11375547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
11385547Swnj * a FIN has been received from the remote side.
11395547Swnj * In these states we ignore the URG.
114027190Skarels *
114127190Skarels * According to RFC961 (Assigned Protocols),
114227190Skarels * the urgent pointer points to the last octet
114327190Skarels * of urgent data. We continue, however,
114427190Skarels * to consider it to indicate the first octet
114544375Skarels * of data past the urgent section as the original
114644375Skarels * spec states (in one of two places).
11475547Swnj */
11485547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
11495547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp;
11505547Swnj so->so_oobmark = so->so_rcv.sb_cc +
11515547Swnj (tp->rcv_up - tp->rcv_nxt) - 1;
11525547Swnj if (so->so_oobmark == 0)
11535547Swnj so->so_state |= SS_RCVATMARK;
11548313Sroot sohasoutofband(so);
115524816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
11565440Swnj }
11575547Swnj /*
11585547Swnj * Remove out of band data so doesn't get presented to user.
11595547Swnj * This can happen independent of advancing the URG pointer,
11605547Swnj * but if two URG's are pending at once, some out-of-band
11615547Swnj * data may creep in... ick.
11625547Swnj */
116344375Skarels if (ti->ti_urp <= ti->ti_len
116444375Skarels #ifdef SO_OOBINLINE
116544375Skarels && (so->so_options & SO_OOBINLINE) == 0
116644375Skarels #endif
116744375Skarels )
116844375Skarels tcp_pulloutofband(so, ti, m);
116925939Skarels } else
117025939Skarels /*
117125939Skarels * If no out of band data is expected,
117225939Skarels * pull receive urgent pointer along
117325939Skarels * with the receive window.
117425939Skarels */
117525939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
117625939Skarels tp->rcv_up = tp->rcv_nxt;
117725939Skarels dodata: /* XXX */
11784601Swnj
11794601Swnj /*
11805065Swnj * Process the segment text, merging it into the TCP sequencing queue,
11815065Swnj * and arranging for acknowledgment of receipt if necessary.
11825065Swnj * This process logically involves adjusting tp->rcv_wnd as data
11835065Swnj * is presented to the user (this happens in tcp_usrreq.c,
11845065Swnj * case PRU_RCVD). If a FIN has already been received on this
11855065Swnj * connection then we just ignore the text.
11864601Swnj */
118717946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) &&
118817946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) {
118924816Skarels TCP_REASS(tp, ti, m, so, tiflags);
119025260Skarels /*
119125260Skarels * Note the amount of data that peer has sent into
119225260Skarels * our window, in order to estimate the sender's
119325260Skarels * buffer size.
119425260Skarels */
119532098Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
11965244Sroot } else {
11974924Swnj m_freem(m);
11985263Swnj tiflags &= ~TH_FIN;
11995244Sroot }
12004601Swnj
12014601Swnj /*
12025263Swnj * If FIN is received ACK the FIN and let the user know
12035263Swnj * that the connection is closing.
12044601Swnj */
12055263Swnj if (tiflags & TH_FIN) {
12065244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
12075244Sroot socantrcvmore(so);
12085244Sroot tp->t_flags |= TF_ACKNOW;
12095244Sroot tp->rcv_nxt++;
12105244Sroot }
12115065Swnj switch (tp->t_state) {
12124601Swnj
12135065Swnj /*
12145065Swnj * In SYN_RECEIVED and ESTABLISHED STATES
12155065Swnj * enter the CLOSE_WAIT state.
12164884Swnj */
12175065Swnj case TCPS_SYN_RECEIVED:
12185065Swnj case TCPS_ESTABLISHED:
12195065Swnj tp->t_state = TCPS_CLOSE_WAIT;
12205065Swnj break;
12214884Swnj
12225065Swnj /*
12235085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so
12245085Swnj * enter the CLOSING state.
12254884Swnj */
12265065Swnj case TCPS_FIN_WAIT_1:
12275085Swnj tp->t_state = TCPS_CLOSING;
12285065Swnj break;
12294601Swnj
12305065Swnj /*
12315065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state,
12325065Swnj * starting the time-wait timer, turning off the other
12335065Swnj * standard timers.
12345065Swnj */
12355065Swnj case TCPS_FIN_WAIT_2:
12365244Sroot tp->t_state = TCPS_TIME_WAIT;
12375074Swnj tcp_canceltimers(tp);
12385162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
12395244Sroot soisdisconnected(so);
12405065Swnj break;
12415065Swnj
12424884Swnj /*
12435065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer.
12444884Swnj */
12455065Swnj case TCPS_TIME_WAIT:
12465162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
12475065Swnj break;
12485085Swnj }
12494601Swnj }
12505267Sroot if (so->so_options & SO_DEBUG)
12515267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
12525085Swnj
12535085Swnj /*
12545085Swnj * Return any desired output.
12555085Swnj */
125626824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW))
125725939Skarels (void) tcp_output(tp);
12585065Swnj return;
12595085Swnj
12605065Swnj dropafterack:
12615085Swnj /*
12626211Swnj * Generate an ACK dropping incoming segment if it occupies
12636211Swnj * sequence space, where the ACK reflects our state.
12645085Swnj */
126526057Skarels if (tiflags & TH_RST)
12665085Swnj goto drop;
126731749Skarels m_freem(m);
126831721Skarels tp->t_flags |= TF_ACKNOW;
126931721Skarels (void) tcp_output(tp);
12705231Swnj return;
12715085Swnj
12725085Swnj dropwithreset:
12735085Swnj /*
12745244Sroot * Generate a RST, dropping incoming segment.
12755085Swnj * Make ACK acceptable to originator of segment.
127657433Sandrew * Don't bother to respond if destination was broadcast/multicast.
12775085Swnj */
127857433Sandrew if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
127967588Smckusick IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
12805085Swnj goto drop;
12815085Swnj if (tiflags & TH_ACK)
128237320Skarels tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
12835085Swnj else {
12845085Swnj if (tiflags & TH_SYN)
12855085Swnj ti->ti_len++;
128637320Skarels tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
12876211Swnj TH_RST|TH_ACK);
12885085Swnj }
128910769Ssam /* destroy temporarily created socket */
129010769Ssam if (dropsocket)
129110769Ssam (void) soabort(so);
12925231Swnj return;
12935085Swnj
12945065Swnj drop:
12955085Swnj /*
12965085Swnj * Drop space held by incoming segment and return.
12975085Swnj */
12986303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
12996303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
13005065Swnj m_freem(m);
130110769Ssam /* destroy temporarily created socket */
130210769Ssam if (dropsocket)
130310769Ssam (void) soabort(so);
13045267Sroot return;
130557947Ssklower #ifndef TUBA_INCLUDE
13065065Swnj }
13075065Swnj
130861335Sbostic void
tcp_dooptions(tp,cp,cnt,ti,ts_present,ts_val,ts_ecr)130957433Sandrew tcp_dooptions(tp, cp, cnt, ti, ts_present, ts_val, ts_ecr)
13105440Swnj struct tcpcb *tp;
131157433Sandrew u_char *cp;
131257433Sandrew int cnt;
131317272Skarels struct tcpiphdr *ti;
131457433Sandrew int *ts_present;
131557433Sandrew u_long *ts_val, *ts_ecr;
13165419Swnj {
131744375Skarels u_short mss;
131857433Sandrew int opt, optlen;
13195419Swnj
13205440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) {
13215440Swnj opt = cp[0];
13225440Swnj if (opt == TCPOPT_EOL)
13235440Swnj break;
13245440Swnj if (opt == TCPOPT_NOP)
13255440Swnj optlen = 1;
132612169Ssam else {
13275440Swnj optlen = cp[1];
132812169Ssam if (optlen <= 0)
132912169Ssam break;
133012169Ssam }
13315440Swnj switch (opt) {
13325440Swnj
13335440Swnj default:
133444375Skarels continue;
13355440Swnj
13365440Swnj case TCPOPT_MAXSEG:
133757433Sandrew if (optlen != TCPOLEN_MAXSEG)
13385440Swnj continue;
133917272Skarels if (!(ti->ti_flags & TH_SYN))
134017272Skarels continue;
134144375Skarels bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
134244375Skarels NTOHS(mss);
134344375Skarels (void) tcp_mss(tp, mss); /* sets t_maxseg */
13445440Swnj break;
134557433Sandrew
134657433Sandrew case TCPOPT_WINDOW:
134757433Sandrew if (optlen != TCPOLEN_WINDOW)
134857433Sandrew continue;
134957433Sandrew if (!(ti->ti_flags & TH_SYN))
135057433Sandrew continue;
135157433Sandrew tp->t_flags |= TF_RCVD_SCALE;
135257433Sandrew tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
135357433Sandrew break;
135457433Sandrew
135557433Sandrew case TCPOPT_TIMESTAMP:
135657433Sandrew if (optlen != TCPOLEN_TIMESTAMP)
135757433Sandrew continue;
135857433Sandrew *ts_present = 1;
135957433Sandrew bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val));
136057433Sandrew NTOHL(*ts_val);
136157433Sandrew bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr));
136257433Sandrew NTOHL(*ts_ecr);
136357433Sandrew
136457433Sandrew /*
136557433Sandrew * A timestamp received in a SYN makes
136657433Sandrew * it ok to send timestamp requests and replies.
136757433Sandrew */
136857433Sandrew if (ti->ti_flags & TH_SYN) {
136957433Sandrew tp->t_flags |= TF_RCVD_TSTMP;
137057433Sandrew tp->ts_recent = *ts_val;
137157433Sandrew tp->ts_recent_age = tcp_now;
137257433Sandrew }
137357433Sandrew break;
13745419Swnj }
13755419Swnj }
13765419Swnj }
13775419Swnj
13785419Swnj /*
13795547Swnj * Pull out of band byte out of a segment so
13805547Swnj * it doesn't appear in the user's data queue.
13815547Swnj * It is still reflected in the segment length for
13825547Swnj * sequencing purposes.
13835547Swnj */
138461335Sbostic void
tcp_pulloutofband(so,ti,m)138544375Skarels tcp_pulloutofband(so, ti, m)
13865547Swnj struct socket *so;
13875547Swnj struct tcpiphdr *ti;
138844375Skarels register struct mbuf *m;
13895547Swnj {
13906116Swnj int cnt = ti->ti_urp - 1;
13915547Swnj
13925547Swnj while (cnt >= 0) {
13935547Swnj if (m->m_len > cnt) {
13945547Swnj char *cp = mtod(m, caddr_t) + cnt;
13955547Swnj struct tcpcb *tp = sototcpcb(so);
13965547Swnj
13975547Swnj tp->t_iobc = *cp;
13985547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA;
13996161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
14005547Swnj m->m_len--;
14015547Swnj return;
14025547Swnj }
14035547Swnj cnt -= m->m_len;
14045547Swnj m = m->m_next;
14055547Swnj if (m == 0)
14065547Swnj break;
14075547Swnj }
14085547Swnj panic("tcp_pulloutofband");
14095547Swnj }
14105547Swnj
14115547Swnj /*
141244375Skarels * Collect new round-trip time estimate
141344375Skarels * and update averages and current timeout.
141417272Skarels */
141561335Sbostic void
tcp_xmit_timer(tp,rtt)141657433Sandrew tcp_xmit_timer(tp, rtt)
141723975Skarels register struct tcpcb *tp;
141861335Sbostic short rtt;
141917272Skarels {
142044375Skarels register short delta;
142144375Skarels
142244375Skarels tcpstat.tcps_rttupdated++;
142344375Skarels if (tp->t_srtt != 0) {
142444375Skarels /*
142544375Skarels * srtt is stored as fixed point with 3 bits after the
142644375Skarels * binary point (i.e., scaled by 8). The following magic
142744375Skarels * is equivalent to the smoothing algorithm in rfc793 with
142844375Skarels * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
142957433Sandrew * point). Adjust rtt to origin 0.
143044375Skarels */
143157433Sandrew delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
143244375Skarels if ((tp->t_srtt += delta) <= 0)
143344375Skarels tp->t_srtt = 1;
143444375Skarels /*
143544375Skarels * We accumulate a smoothed rtt variance (actually, a
143644375Skarels * smoothed mean difference), then set the retransmit
143744375Skarels * timer to smoothed rtt + 4 times the smoothed variance.
143844375Skarels * rttvar is stored as fixed point with 2 bits after the
143944375Skarels * binary point (scaled by 4). The following is
144044375Skarels * equivalent to rfc793 smoothing with an alpha of .75
144144375Skarels * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
144244375Skarels * rfc793's wired-in beta.
144344375Skarels */
144444375Skarels if (delta < 0)
144544375Skarels delta = -delta;
144644375Skarels delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
144744375Skarels if ((tp->t_rttvar += delta) <= 0)
144844375Skarels tp->t_rttvar = 1;
144944375Skarels } else {
145044375Skarels /*
145144375Skarels * No rtt measurement yet - use the unsmoothed rtt.
145244375Skarels * Set the variance to half the rtt (so our first
145352199Skarels * retransmit happens at 3*rtt).
145444375Skarels */
145557433Sandrew tp->t_srtt = rtt << TCP_RTT_SHIFT;
145657433Sandrew tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
145744375Skarels }
145844375Skarels tp->t_rtt = 0;
145944375Skarels tp->t_rxtshift = 0;
146044375Skarels
146144375Skarels /*
146244375Skarels * the retransmit should happen at rtt + 4 * rttvar.
146344375Skarels * Because of the way we do the smoothing, srtt and rttvar
146444375Skarels * will each average +1/2 tick of bias. When we compute
146544375Skarels * the retransmit timer, we want 1/2 tick of rounding and
146644375Skarels * 1 extra tick because of +-1/2 tick uncertainty in the
146744375Skarels * firing of the timer. The bias will give us exactly the
146844375Skarels * 1.5 tick we need. But, because the bias is
146944375Skarels * statistical, we have to test that we don't drop below
147044375Skarels * the minimum feasible timer (which is 2 ticks).
147144375Skarels */
147244375Skarels TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
147344375Skarels tp->t_rttmin, TCPTV_REXMTMAX);
147444375Skarels
147544375Skarels /*
147644375Skarels * We received an ack for a packet that wasn't retransmitted;
147744375Skarels * it is probably safe to discard any error indications we've
147844375Skarels * received recently. This isn't quite right, but close enough
147944375Skarels * for now (a route might have failed after we sent a segment,
148044375Skarels * and the return path might not be symmetrical).
148144375Skarels */
148244375Skarels tp->t_softerror = 0;
148344375Skarels }
148444375Skarels
148544375Skarels /*
148644375Skarels * Determine a reasonable value for maxseg size.
148744375Skarels * If the route is known, check route for mtu.
148844375Skarels * If none, use an mss that can be handled on the outgoing
148944375Skarels * interface without forcing IP to fragment; if bigger than
149044375Skarels * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
149144375Skarels * to utilize large mbufs. If no route is found, route has no mtu,
149244375Skarels * or the destination isn't local, use a default, hopefully conservative
149344375Skarels * size (usually 512 or the default IP max size, but no more than the mtu
149444375Skarels * of the interface), as we can't discover anything about intervening
149544375Skarels * gateways or networks. We also initialize the congestion/slow start
149644375Skarels * window to be a single segment if the destination isn't local.
149744375Skarels * While looking at the routing entry, we also initialize other path-dependent
149844375Skarels * parameters from pre-set or cached values in the routing entry.
149944375Skarels */
150061335Sbostic int
tcp_mss(tp,offer)150144375Skarels tcp_mss(tp, offer)
150244375Skarels register struct tcpcb *tp;
150365606Sbostic u_int offer;
150444375Skarels {
150517272Skarels struct route *ro;
150644375Skarels register struct rtentry *rt;
150717272Skarels struct ifnet *ifp;
150844375Skarels register int rtt, mss;
150944375Skarels u_long bufsize;
151017272Skarels struct inpcb *inp;
151144375Skarels struct socket *so;
151265460Sbostic extern int tcp_mssdflt;
151317272Skarels
151417272Skarels inp = tp->t_inpcb;
151517272Skarels ro = &inp->inp_route;
151644375Skarels
151744375Skarels if ((rt = ro->ro_rt) == (struct rtentry *)0) {
151817272Skarels /* No route yet, so try to acquire one */
151917272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) {
152017272Skarels ro->ro_dst.sa_family = AF_INET;
152137320Skarels ro->ro_dst.sa_len = sizeof(ro->ro_dst);
152217272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
152317272Skarels inp->inp_faddr;
152417272Skarels rtalloc(ro);
152517272Skarels }
152644375Skarels if ((rt = ro->ro_rt) == (struct rtentry *)0)
152757637Sandrew return (tcp_mssdflt);
152817272Skarels }
152944375Skarels ifp = rt->rt_ifp;
153044375Skarels so = inp->inp_socket;
153117272Skarels
153244375Skarels #ifdef RTV_MTU /* if route characteristics exist ... */
153344375Skarels /*
153444375Skarels * While we're here, check if there's an initial rtt
153544375Skarels * or rttvar. Convert from the route-table units
153644375Skarels * to scaled multiples of the slow timeout timer.
153744375Skarels */
153844375Skarels if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
153952199Skarels /*
154052199Skarels * XXX the lock bit for MTU indicates that the value
154152199Skarels * is also a minimum value; this is subject to time.
154252199Skarels */
154352199Skarels if (rt->rt_rmx.rmx_locks & RTV_RTT)
154444375Skarels tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
154544375Skarels tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
154644375Skarels if (rt->rt_rmx.rmx_rttvar)
154744375Skarels tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
154844375Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
154944375Skarels else
155044375Skarels /* default variation is +- 1 rtt */
155144375Skarels tp->t_rttvar =
155244375Skarels tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
155344375Skarels TCPT_RANGESET(tp->t_rxtcur,
155444375Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
155544375Skarels tp->t_rttmin, TCPTV_REXMTMAX);
155644375Skarels }
155744375Skarels /*
155844375Skarels * if there's an mtu associated with the route, use it
155944375Skarels */
156044375Skarels if (rt->rt_rmx.rmx_mtu)
156157637Sandrew mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
156244375Skarels else
156344375Skarels #endif /* RTV_MTU */
156444375Skarels {
156557637Sandrew mss = ifp->if_mtu - sizeof(struct tcpiphdr);
156631726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0
156744375Skarels if (mss > MCLBYTES)
156844375Skarels mss &= ~(MCLBYTES-1);
156917272Skarels #else
157044375Skarels if (mss > MCLBYTES)
157144375Skarels mss = mss / MCLBYTES * MCLBYTES;
157217272Skarels #endif
157344375Skarels if (!in_localaddr(inp->inp_faddr))
157457637Sandrew mss = min(mss, tcp_mssdflt);
157544375Skarels }
157644375Skarels /*
157744375Skarels * The current mss, t_maxseg, is initialized to the default value.
157844375Skarels * If we compute a smaller value, reduce the current mss.
157944375Skarels * If we compute a larger value, return it for use in sending
158044375Skarels * a max seg size option, but don't store it for use
158144375Skarels * unless we received an offer at least that large from peer.
158244375Skarels * However, do not accept offers under 32 bytes.
158344375Skarels */
158444375Skarels if (offer)
158544375Skarels mss = min(mss, offer);
158644375Skarels mss = max(mss, 32); /* sanity */
158744375Skarels if (mss < tp->t_maxseg || offer != 0) {
158844375Skarels /*
158944375Skarels * If there's a pipesize, change the socket buffer
159044375Skarels * to that size. Make the socket buffers an integral
159144375Skarels * number of mss units; if the mss is larger than
159244375Skarels * the socket buffer, decrease the mss.
159344375Skarels */
159444375Skarels #ifdef RTV_SPIPE
159544375Skarels if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
159644375Skarels #endif
159744375Skarels bufsize = so->so_snd.sb_hiwat;
159844375Skarels if (bufsize < mss)
159944375Skarels mss = bufsize;
160044375Skarels else {
160156908Storek bufsize = roundup(bufsize, mss);
160257433Sandrew if (bufsize > sb_max)
160357433Sandrew bufsize = sb_max;
160456908Storek (void)sbreserve(&so->so_snd, bufsize);
160544375Skarels }
160644375Skarels tp->t_maxseg = mss;
160732098Skarels
160844375Skarels #ifdef RTV_RPIPE
160944375Skarels if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
161044375Skarels #endif
161144375Skarels bufsize = so->so_rcv.sb_hiwat;
161244375Skarels if (bufsize > mss) {
161356908Storek bufsize = roundup(bufsize, mss);
161457433Sandrew if (bufsize > sb_max)
161557433Sandrew bufsize = sb_max;
161656908Storek (void)sbreserve(&so->so_rcv, bufsize);
161744375Skarels }
161844375Skarels }
161932034Skarels tp->snd_cwnd = mss;
162044375Skarels
162144375Skarels #ifdef RTV_SSTHRESH
162244375Skarels if (rt->rt_rmx.rmx_ssthresh) {
162344375Skarels /*
162444375Skarels * There's some sort of gateway or interface
162544375Skarels * buffer limit on the path. Use this to set
162644375Skarels * the slow start threshhold, but set the
162744375Skarels * threshold to no less than 2*mss.
162844375Skarels */
162944375Skarels tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
163044375Skarels }
163144375Skarels #endif /* RTV_MTU */
163232034Skarels return (mss);
163317272Skarels }
163457947Ssklower #endif /* TUBA_INCLUDE */
1635