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 
65 #ifdef __USE_MISC
66 # include <sys/types.h>
67 # include <sys/socket.h>
68 # include <stdint.h>
69 
70 typedef	uint32_t tcp_seq;
71 /*
72  * TCP header.
73  * Per RFC 793, September, 1981.
74  */
75 struct tcphdr
76   {
77     __extension__ union
78     {
79       struct
80       {
81 	uint16_t th_sport;	/* source port */
82 	uint16_t th_dport;	/* destination port */
83 	tcp_seq th_seq;		/* sequence number */
84 	tcp_seq th_ack;		/* acknowledgement number */
85 # if __BYTE_ORDER == __LITTLE_ENDIAN
86 	uint8_t th_x2:4;	/* (unused) */
87 	uint8_t th_off:4;	/* data offset */
88 # endif
89 # if __BYTE_ORDER == __BIG_ENDIAN
90 	uint8_t th_off:4;	/* data offset */
91 	uint8_t th_x2:4;	/* (unused) */
92 # endif
93 	uint8_t th_flags;
94 # define TH_FIN	0x01
95 # define TH_SYN	0x02
96 # define TH_RST	0x04
97 # define TH_PUSH	0x08
98 # define TH_ACK	0x10
99 # define TH_URG	0x20
100 	uint16_t th_win;	/* window */
101 	uint16_t th_sum;	/* checksum */
102 	uint16_t th_urp;	/* urgent pointer */
103       };
104       struct
105       {
106 	uint16_t source;
107 	uint16_t dest;
108 	uint32_t seq;
109 	uint32_t ack_seq;
110 # if __BYTE_ORDER == __LITTLE_ENDIAN
111 	uint16_t res1:4;
112 	uint16_t doff:4;
113 	uint16_t fin:1;
114 	uint16_t syn:1;
115 	uint16_t rst:1;
116 	uint16_t psh:1;
117 	uint16_t ack:1;
118 	uint16_t urg:1;
119 	uint16_t res2:2;
120 # elif __BYTE_ORDER == __BIG_ENDIAN
121 	uint16_t doff:4;
122 	uint16_t res1:4;
123 	uint16_t res2:2;
124 	uint16_t urg:1;
125 	uint16_t ack:1;
126 	uint16_t psh:1;
127 	uint16_t rst:1;
128 	uint16_t syn:1;
129 	uint16_t fin:1;
130 # else
131 #  error "Adjust your <bits/endian.h> defines"
132 # endif
133 	uint16_t window;
134 	uint16_t check;
135 	uint16_t urg_ptr;
136       };
137     };
138 };
139 
140 enum
141 {
142   TCP_ESTABLISHED = 1,
143   TCP_SYN_SENT,
144   TCP_SYN_RECV,
145   TCP_FIN_WAIT1,
146   TCP_FIN_WAIT2,
147   TCP_TIME_WAIT,
148   TCP_CLOSE,
149   TCP_CLOSE_WAIT,
150   TCP_LAST_ACK,
151   TCP_LISTEN,
152   TCP_CLOSING   /* now a valid state */
153 };
154 
155 # define TCPOPT_EOL		0
156 # define TCPOPT_NOP		1
157 # define TCPOPT_MAXSEG		2
158 # define TCPOLEN_MAXSEG		4
159 # define TCPOPT_WINDOW		3
160 # define TCPOLEN_WINDOW		3
161 # define TCPOPT_SACK_PERMITTED	4		/* Experimental */
162 # define TCPOLEN_SACK_PERMITTED	2
163 # define TCPOPT_SACK		5		/* Experimental */
164 # define TCPOPT_TIMESTAMP	8
165 # define TCPOLEN_TIMESTAMP	10
166 # define TCPOLEN_TSTAMP_APPA	(TCPOLEN_TIMESTAMP+2) /* appendix A */
167 
168 # define TCPOPT_TSTAMP_HDR	\
169     (TCPOPT_NOP<<24|TCPOPT_NOP<<16|TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)
170 
171 /*
172  * Default maximum segment size for TCP.
173  * With an IP MSS of 576, this is 536,
174  * but 512 is probably more convenient.
175  * This should be defined as MIN(512, IP_MSS - sizeof (struct tcpiphdr)).
176  */
177 # define TCP_MSS	512
178 
179 # define TCP_MAXWIN	65535	/* largest value for (unscaled) window */
180 
181 # define TCP_MAX_WINSHIFT	14	/* maximum window shift */
182 
183 # define SOL_TCP		6	/* TCP level */
184 
185 
186 # define TCPI_OPT_TIMESTAMPS	1
187 # define TCPI_OPT_SACK		2
188 # define TCPI_OPT_WSCALE	4
189 # define TCPI_OPT_ECN		8
190 
191 /* Values for tcpi_state.  */
192 enum tcp_ca_state
193 {
194   TCP_CA_Open = 0,
195   TCP_CA_Disorder = 1,
196   TCP_CA_CWR = 2,
197   TCP_CA_Recovery = 3,
198   TCP_CA_Loss = 4
199 };
200 
201 struct tcp_info
202 {
203   u_int8_t	tcpi_state;
204   u_int8_t	tcpi_ca_state;
205   u_int8_t	tcpi_retransmits;
206   u_int8_t	tcpi_probes;
207   u_int8_t	tcpi_backoff;
208   u_int8_t	tcpi_options;
209   u_int8_t	tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4;
210 
211   u_int32_t	tcpi_rto;
212   u_int32_t	tcpi_ato;
213   u_int32_t	tcpi_snd_mss;
214   u_int32_t	tcpi_rcv_mss;
215 
216   u_int32_t	tcpi_unacked;
217   u_int32_t	tcpi_sacked;
218   u_int32_t	tcpi_lost;
219   u_int32_t	tcpi_retrans;
220   u_int32_t	tcpi_fackets;
221 
222   /* Times. */
223   u_int32_t	tcpi_last_data_sent;
224   u_int32_t	tcpi_last_ack_sent;	/* Not remembered, sorry.  */
225   u_int32_t	tcpi_last_data_recv;
226   u_int32_t	tcpi_last_ack_recv;
227 
228   /* Metrics. */
229   u_int32_t	tcpi_pmtu;
230   u_int32_t	tcpi_rcv_ssthresh;
231   u_int32_t	tcpi_rtt;
232   u_int32_t	tcpi_rttvar;
233   u_int32_t	tcpi_snd_ssthresh;
234   u_int32_t	tcpi_snd_cwnd;
235   u_int32_t	tcpi_advmss;
236   u_int32_t	tcpi_reordering;
237 
238   u_int32_t	tcpi_rcv_rtt;
239   u_int32_t	tcpi_rcv_space;
240 
241   u_int32_t	tcpi_total_retrans;
242 };
243 
244 
245 /* For TCP_MD5SIG socket option.  */
246 #define TCP_MD5SIG_MAXKEYLEN	80
247 
248 struct tcp_md5sig
249 {
250   struct sockaddr_storage tcpm_addr;		/* Address associated.  */
251   u_int16_t	__tcpm_pad1;			/* Zero.  */
252   u_int16_t	tcpm_keylen;			/* Key length.  */
253   u_int32_t	__tcpm_pad2;			/* Zero.  */
254   u_int8_t	tcpm_key[TCP_MD5SIG_MAXKEYLEN];	/* Key (binary).  */
255 };
256 
257 #endif /* Misc.  */
258 
259 #endif /* netinet/tcp.h */
260