1/*
2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)tcp.h 8.1 (Berkeley) 6/10/93
30 */
31
32#ifndef _NETINET_TCP_H
33#define _NETINET_TCP_H 1
34
35#include <features.h>
36
37/*
38 * User-settable options (used with setsockopt).
39 */
40#define TCP_NODELAY 1 /* Don't delay send to coalesce packets */
41#define TCP_MAXSEG 2 /* Set maximum segment size */
42#define TCP_CORK 3 /* Control sending of partial frames */
43#define TCP_KEEPIDLE 4 /* Start keeplives after this period */
44#define TCP_KEEPINTVL 5 /* Interval between keepalives */
45#define TCP_KEEPCNT 6 /* Number of keepalives before death */
46#define TCP_SYNCNT 7 /* Number of SYN retransmits */
47#define TCP_LINGER2 8 /* Life time of orphaned FIN-WAIT-2 state */
48#define TCP_DEFER_ACCEPT 9 /* Wake up listener only when data arrive */
49#define TCP_WINDOW_CLAMP 10 /* Bound advertised window */
50#define TCP_INFO 11 /* Information about this connection. */
51#define TCP_QUICKACK 12 /* Bock/reenable quick ACKs. */
52#define TCP_CONGESTION 13 /* Congestion control algorithm. */
53#define TCP_MD5SIG 14 /* TCP MD5 Signature (RFC2385) */
54#define TCP_COOKIE_TRANSACTIONS 15 /* TCP Cookie Transactions */
55#define TCP_THIN_LINEAR_TIMEOUTS 16 /* Use linear timeouts for thin streams*/
56#define TCP_THIN_DUPACK 17 /* Fast retrans. after 1 dupack */
57#define TCP_USER_TIMEOUT 18 /* How long for loss retry before timeout */
58#define TCP_REPAIR 19 /* TCP sock is under repair right now */
59#define TCP_REPAIR_QUEUE 20 /* Set TCP queue to repair */
60#define TCP_QUEUE_SEQ 21 /* Set sequence number of repaired queue. */
61#define TCP_REPAIR_OPTIONS 22 /* Repair TCP connection options */
62#define TCP_FASTOPEN 23 /* Enable FastOpen on listeners */
63#define TCP_TIMESTAMP 24 /* TCP time stamp */
64#define TCP_NOTSENT_LOWAT 25 /* Limit number of unsent bytes in
65 write queue. */
66#define TCP_CC_INFO 26 /* Get Congestion Control
67 (optional) info. */
68#define TCP_SAVE_SYN 27 /* Record SYN headers for new
69 connections. */
70#define TCP_SAVED_SYN 28 /* Get SYN headers recorded for
71 connection. */
72#define TCP_REPAIR_WINDOW 29 /* Get/set window parameters. */
73#define TCP_FASTOPEN_CONNECT 30 /* Attempt FastOpen with connect. */
74#define TCP_ULP 31 /* Attach a ULP to a TCP connection. */
75#define TCP_MD5SIG_EXT 32 /* TCP MD5 Signature with extensions. */
76#define TCP_FASTOPEN_KEY 33 /* Set the key for Fast Open (cookie). */
77#define TCP_FASTOPEN_NO_COOKIE 34 /* Enable TFO without a TFO cookie. */
78
79#ifdef __USE_MISC
80# include <sys/types.h>
81# include <sys/socket.h>
82# include <stdint.h>
83
84typedef uint32_t tcp_seq;
85/*
86 * TCP header.
87 * Per RFC 793, September, 1981.
88 */
89struct tcphdr
90 {
91 __extension__ union
92 {
93 struct
94 {
95 uint16_t th_sport; /* source port */
96 uint16_t th_dport; /* destination port */
97 tcp_seq th_seq; /* sequence number */
98 tcp_seq th_ack; /* acknowledgement number */
99# if __BYTE_ORDER == __LITTLE_ENDIAN
100 uint8_t th_x2:4; /* (unused) */
101 uint8_t th_off:4; /* data offset */
102# endif
103# if __BYTE_ORDER == __BIG_ENDIAN
104 uint8_t th_off:4; /* data offset */
105 uint8_t th_x2:4; /* (unused) */
106# endif
107 uint8_t th_flags;
108# define TH_FIN 0x01
109# define TH_SYN 0x02
110# define TH_RST 0x04
111# define TH_PUSH 0x08
112# define TH_ACK 0x10
113# define TH_URG 0x20
114 uint16_t th_win; /* window */
115 uint16_t th_sum; /* checksum */
116 uint16_t th_urp; /* urgent pointer */
117 };
118 struct
119 {
120 uint16_t source;
121 uint16_t dest;
122 uint32_t seq;
123 uint32_t ack_seq;
124# if __BYTE_ORDER == __LITTLE_ENDIAN
125 uint16_t res1:4;
126 uint16_t doff:4;
127 uint16_t fin:1;
128 uint16_t syn:1;
129 uint16_t rst:1;
130 uint16_t psh:1;
131 uint16_t ack:1;
132 uint16_t urg:1;
133 uint16_t res2:2;
134# elif __BYTE_ORDER == __BIG_ENDIAN
135 uint16_t doff:4;
136 uint16_t res1:4;
137 uint16_t res2:2;
138 uint16_t urg:1;
139 uint16_t ack:1;
140 uint16_t psh:1;
141 uint16_t rst:1;
142 uint16_t syn:1;
143 uint16_t fin:1;
144# else
145# error "Adjust your <bits/endian.h> defines"
146# endif
147 uint16_t window;
148 uint16_t check;
149 uint16_t urg_ptr;
150 };
151 };
152};
153
154enum
155{
156 TCP_ESTABLISHED = 1,
157 TCP_SYN_SENT,
158 TCP_SYN_RECV,
159 TCP_FIN_WAIT1,
160 TCP_FIN_WAIT2,
161 TCP_TIME_WAIT,
162 TCP_CLOSE,
163 TCP_CLOSE_WAIT,
164 TCP_LAST_ACK,
165 TCP_LISTEN,
166 TCP_CLOSING /* now a valid state */
167};
168
169# define TCPOPT_EOL 0
170# define TCPOPT_NOP 1
171# define TCPOPT_MAXSEG 2
172# define TCPOLEN_MAXSEG 4
173# define TCPOPT_WINDOW 3
174# define TCPOLEN_WINDOW 3
175# define TCPOPT_SACK_PERMITTED 4 /* Experimental */
176# define TCPOLEN_SACK_PERMITTED 2
177# define TCPOPT_SACK 5 /* Experimental */
178# define TCPOPT_TIMESTAMP 8
179# define TCPOLEN_TIMESTAMP 10
180# define TCPOLEN_TSTAMP_APPA (TCPOLEN_TIMESTAMP+2) /* appendix A */
181
182# define TCPOPT_TSTAMP_HDR \
183 (TCPOPT_NOP<<24|TCPOPT_NOP<<16|TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)
184
185/*
186 * Default maximum segment size for TCP.
187 * With an IP MSS of 576, this is 536,
188 * but 512 is probably more convenient.
189 * This should be defined as MIN(512, IP_MSS - sizeof (struct tcpiphdr)).
190 */
191# define TCP_MSS 512
192
193# define TCP_MAXWIN 65535 /* largest value for (unscaled) window */
194
195# define TCP_MAX_WINSHIFT 14 /* maximum window shift */
196
197# define SOL_TCP 6 /* TCP level */
198
199
200# define TCPI_OPT_TIMESTAMPS 1
201# define TCPI_OPT_SACK 2
202# define TCPI_OPT_WSCALE 4
203# define TCPI_OPT_ECN 8 /* ECN was negociated at TCP session init */
204# define TCPI_OPT_ECN_SEEN 16 /* we received at least one packet with ECT */
205# define TCPI_OPT_SYN_DATA 32 /* SYN-ACK acked data in SYN sent or rcvd */
206
207/* Values for tcpi_state. */
208enum tcp_ca_state
209{
210 TCP_CA_Open = 0,
211 TCP_CA_Disorder = 1,
212 TCP_CA_CWR = 2,
213 TCP_CA_Recovery = 3,
214 TCP_CA_Loss = 4
215};
216
217struct tcp_info
218{
219 uint8_t tcpi_state;
220 uint8_t tcpi_ca_state;
221 uint8_t tcpi_retransmits;
222 uint8_t tcpi_probes;
223 uint8_t tcpi_backoff;
224 uint8_t tcpi_options;
225 uint8_t tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4;
226
227 uint32_t tcpi_rto;
228 uint32_t tcpi_ato;
229 uint32_t tcpi_snd_mss;
230 uint32_t tcpi_rcv_mss;
231
232 uint32_t tcpi_unacked;
233 uint32_t tcpi_sacked;
234 uint32_t tcpi_lost;
235 uint32_t tcpi_retrans;
236 uint32_t tcpi_fackets;
237
238 /* Times. */
239 uint32_t tcpi_last_data_sent;
240 uint32_t tcpi_last_ack_sent; /* Not remembered, sorry. */
241 uint32_t tcpi_last_data_recv;
242 uint32_t tcpi_last_ack_recv;
243
244 /* Metrics. */
245 uint32_t tcpi_pmtu;
246 uint32_t tcpi_rcv_ssthresh;
247 uint32_t tcpi_rtt;
248 uint32_t tcpi_rttvar;
249 uint32_t tcpi_snd_ssthresh;
250 uint32_t tcpi_snd_cwnd;
251 uint32_t tcpi_advmss;
252 uint32_t tcpi_reordering;
253
254 uint32_t tcpi_rcv_rtt;
255 uint32_t tcpi_rcv_space;
256
257 uint32_t tcpi_total_retrans;
258};
259
260
261/* For TCP_MD5SIG socket option. */
262#define TCP_MD5SIG_MAXKEYLEN 80
263
264/* tcp_md5sig extension flags for TCP_MD5SIG_EXT. */
265#define TCP_MD5SIG_FLAG_PREFIX 1 /* Address prefix length. */
266
267struct tcp_md5sig
268{
269 struct sockaddr_storage tcpm_addr; /* Address associated. */
270 uint8_t tcpm_flags; /* Extension flags. */
271 uint8_t tcpm_prefixlen; /* Address prefix. */
272 uint16_t tcpm_keylen; /* Key length. */
273 uint32_t __tcpm_pad; /* Zero. */
274 uint8_t tcpm_key[TCP_MD5SIG_MAXKEYLEN]; /* Key (binary). */
275};
276
277/* For socket repair options. */
278struct tcp_repair_opt
279{
280 uint32_t opt_code;
281 uint32_t opt_val;
282};
283
284/* Queue to repair, for TCP_REPAIR_QUEUE. */
285enum
286{
287 TCP_NO_QUEUE,
288 TCP_RECV_QUEUE,
289 TCP_SEND_QUEUE,
290 TCP_QUEUES_NR,
291};
292
293/* For cookie transactions socket options. */
294#define TCP_COOKIE_MIN 8 /* 64-bits */
295#define TCP_COOKIE_MAX 16 /* 128-bits */
296#define TCP_COOKIE_PAIR_SIZE (2*TCP_COOKIE_MAX)
297
298/* Flags for both getsockopt and setsockopt */
299#define TCP_COOKIE_IN_ALWAYS (1 << 0) /* Discard SYN without cookie */
300#define TCP_COOKIE_OUT_NEVER (1 << 1) /* Prohibit outgoing cookies,
301 * supercedes everything. */
302
303/* Flags for getsockopt */
304#define TCP_S_DATA_IN (1 << 2) /* Was data received? */
305#define TCP_S_DATA_OUT (1 << 3) /* Was data sent? */
306
307#define TCP_MSS_DEFAULT 536U /* IPv4 (RFC1122, RFC2581) */
308#define TCP_MSS_DESIRED 1220U /* IPv6 (tunneled), EDNS0 (RFC3226) */
309
310struct tcp_cookie_transactions
311{
312 uint16_t tcpct_flags;
313 uint8_t __tcpct_pad1;
314 uint8_t tcpct_cookie_desired;
315 uint16_t tcpct_s_data_desired;
316 uint16_t tcpct_used;
317 uint8_t tcpct_value[TCP_MSS_DEFAULT];
318};
319
320/* For use with TCP_REPAIR_WINDOW. */
321struct tcp_repair_window
322{
323 uint32_t snd_wl1;
324 uint32_t snd_wnd;
325 uint32_t max_window;
326 uint32_t rcv_wnd;
327 uint32_t rcv_wup;
328};
329
330#endif /* Misc. */
331
332#endif /* netinet/tcp.h */
333