1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (c) 1982, 1986, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	@(#)tcp_var.h	8.3 (Berkeley) 4/10/94
31  * tcp_var.h,v 1.3 1994/08/21 05:27:39 paul Exp
32  */
33 
34 #ifndef TCP_VAR_H
35 #define TCP_VAR_H
36 
37 #include "tcpip.h"
38 #include "tcp_timer.h"
39 
40 /*
41  * Tcp control block, one per tcp; fields:
42  */
43 struct tcpcb {
44     struct tcpiphdr *seg_next; /* sequencing queue */
45     struct tcpiphdr *seg_prev;
46     short t_state; /* state of this connection */
47     short t_timer[TCPT_NTIMERS]; /* tcp timers */
48     short t_rxtshift; /* log(2) of rexmt exp. backoff */
49     short t_rxtcur; /* current retransmit value */
50     short t_dupacks; /* consecutive dup acks recd */
51     uint16_t t_maxseg; /* maximum segment size */
52     uint8_t t_force; /* 1 if forcing out a byte */
53     uint16_t t_flags;
54 #define TF_ACKNOW 0x0001 /* ack peer immediately */
55 #define TF_DELACK 0x0002 /* ack, but try to delay it */
56 #define TF_NODELAY 0x0004 /* don't delay packets to coalesce */
57 #define TF_NOOPT 0x0008 /* don't use tcp options */
58 #define TF_SENTFIN 0x0010 /* have sent FIN */
59 #define TF_REQ_SCALE 0x0020 /* have/will request window scaling */
60 #define TF_RCVD_SCALE 0x0040 /* other side has requested scaling */
61 #define TF_REQ_TSTMP 0x0080 /* have/will request timestamps */
62 #define TF_RCVD_TSTMP 0x0100 /* a timestamp was received in SYN */
63 #define TF_SACK_PERMIT 0x0200 /* other side said I could SACK */
64 
65     struct tcpiphdr t_template; /* static skeletal packet for xmit */
66 
67     struct socket *t_socket; /* back pointer to socket */
68     /*
69      * The following fields are used as in the protocol specification.
70      * See RFC783, Dec. 1981, page 21.
71      */
72     /* send sequence variables */
73     tcp_seq snd_una; /* send unacknowledged */
74     tcp_seq snd_nxt; /* send next */
75     tcp_seq snd_up; /* send urgent pointer */
76     tcp_seq snd_wl1; /* window update seg seq number */
77     tcp_seq snd_wl2; /* window update seg ack number */
78     tcp_seq iss; /* initial send sequence number */
79     uint32_t snd_wnd; /* send window */
80     /* receive sequence variables */
81     uint32_t rcv_wnd; /* receive window */
82     tcp_seq rcv_nxt; /* receive next */
83     tcp_seq rcv_up; /* receive urgent pointer */
84     tcp_seq irs; /* initial receive sequence number */
85     /*
86      * Additional variables for this implementation.
87      */
88     /* receive variables */
89     tcp_seq rcv_adv; /* advertised window */
90     /* retransmit variables */
91     tcp_seq snd_max; /* highest sequence number sent;
92                       * used to recognize retransmits
93                       */
94     /* congestion control (for slow start, source quench, retransmit after loss)
95      */
96     uint32_t snd_cwnd; /* congestion-controlled window */
97     uint32_t snd_ssthresh; /* snd_cwnd size threshold for
98                             * for slow start exponential to
99                             * linear switch
100                             */
101     /*
102      * transmit timing stuff.  See below for scale of srtt and rttvar.
103      * "Variance" is actually smoothed difference.
104      */
105     short t_idle; /* inactivity time */
106     short t_rtt; /* round trip time */
107     tcp_seq t_rtseq; /* sequence number being timed */
108     short t_srtt; /* smoothed round-trip time */
109     short t_rttvar; /* variance in round-trip time */
110     uint16_t t_rttmin; /* minimum rtt allowed */
111     uint32_t max_sndwnd; /* largest window peer has offered */
112 
113     /* out-of-band data */
114     uint8_t t_oobflags; /* have some */
115     uint8_t t_iobc; /* input character */
116 #define TCPOOB_HAVEDATA 0x01
117 #define TCPOOB_HADDATA 0x02
118     short t_softerror; /* possible error not yet reported */
119 
120     /* RFC 1323 variables */
121     uint8_t snd_scale; /* window scaling for send window */
122     uint8_t rcv_scale; /* window scaling for recv window */
123     uint8_t request_r_scale; /* pending window scaling */
124     uint8_t requested_s_scale;
125     uint32_t ts_recent; /* timestamp echo data */
126     uint32_t ts_recent_age; /* when last updated */
127     tcp_seq last_ack_sent;
128 };
129 
130 #define sototcpcb(so) ((so)->so_tcpcb)
131 
132 /*
133  * The smoothed round-trip time and estimated variance
134  * are stored as fixed point numbers scaled by the values below.
135  * For convenience, these scales are also used in smoothing the average
136  * (smoothed = (1/scale)sample + ((scale-1)/scale)smoothed).
137  * With these scales, srtt has 3 bits to the right of the binary point,
138  * and thus an "ALPHA" of 0.875.  rttvar has 2 bits to the right of the
139  * binary point, and is smoothed with an ALPHA of 0.75.
140  */
141 #define TCP_RTT_SCALE 8 /* multiplier for srtt; 3 bits frac. */
142 #define TCP_RTT_SHIFT 3 /* shift for srtt; 3 bits frac. */
143 #define TCP_RTTVAR_SCALE 4 /* multiplier for rttvar; 2 bits */
144 #define TCP_RTTVAR_SHIFT 2 /* multiplier for rttvar; 2 bits */
145 
146 /*
147  * The initial retransmission should happen at rtt + 4 * rttvar.
148  * Because of the way we do the smoothing, srtt and rttvar
149  * will each average +1/2 tick of bias.  When we compute
150  * the retransmit timer, we want 1/2 tick of rounding and
151  * 1 extra tick because of +-1/2 tick uncertainty in the
152  * firing of the timer.  The bias will give us exactly the
153  * 1.5 tick we need.  But, because the bias is
154  * statistical, we have to test that we don't drop below
155  * the minimum feasible timer (which is 2 ticks).
156  * This macro assumes that the value of TCP_RTTVAR_SCALE
157  * is the same as the multiplier for rttvar.
158  */
159 #define TCP_REXMTVAL(tp) (((tp)->t_srtt >> TCP_RTT_SHIFT) + (tp)->t_rttvar)
160 
161 #endif
162