tcp.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #ifndef _NETINET_TCP_H
  32. #define _NETINET_TCP_H 1
  33. #include <features.h>
  34. /*
  35. * User-settable options (used with setsockopt).
  36. */
  37. #define TCP_NODELAY 1 /* Don't delay send to coalesce packets */
  38. #define TCP_MAXSEG 2 /* Set maximum segment size */
  39. #define TCP_CORK 3 /* Control sending of partial frames */
  40. #define TCP_KEEPIDLE 4 /* Start keeplives after this period */
  41. #define TCP_KEEPINTVL 5 /* Interval between keepalives */
  42. #define TCP_KEEPCNT 6 /* Number of keepalives before death */
  43. #define TCP_SYNCNT 7 /* Number of SYN retransmits */
  44. #define TCP_LINGER2 8 /* Life time of orphaned FIN-WAIT-2 state */
  45. #define TCP_DEFER_ACCEPT 9 /* Wake up listener only when data arrive */
  46. #define TCP_WINDOW_CLAMP 10 /* Bound advertised window */
  47. #define TCP_INFO 11 /* Information about this connection. */
  48. #define TCP_QUICKACK 12 /* Bock/reenable quick ACKs. */
  49. #define TCP_CONGESTION 13 /* Congestion control algorithm. */
  50. #define TCP_MD5SIG 14 /* TCP MD5 Signature (RFC2385) */
  51. #define TCP_COOKIE_TRANSACTIONS 15 /* TCP Cookie Transactions */
  52. #define TCP_THIN_LINEAR_TIMEOUTS 16 /* Use linear timeouts for thin streams*/
  53. #define TCP_THIN_DUPACK 17 /* Fast retrans. after 1 dupack */
  54. #define TCP_USER_TIMEOUT 18 /* How long for loss retry before timeout */
  55. #define TCP_REPAIR 19 /* TCP sock is under repair right now */
  56. #define TCP_REPAIR_QUEUE 20 /* Set TCP queue to repair */
  57. #define TCP_QUEUE_SEQ 21 /* Set sequence number of repaired queue. */
  58. #define TCP_REPAIR_OPTIONS 22 /* Repair TCP connection options */
  59. #define TCP_FASTOPEN 23 /* Enable FastOpen on listeners */
  60. #define TCP_TIMESTAMP 24 /* TCP time stamp */
  61. #define TCP_NOTSENT_LOWAT 25 /* Limit number of unsent bytes in
  62. write queue. */
  63. #define TCP_CC_INFO 26 /* Get Congestion Control
  64. (optional) info. */
  65. #define TCP_SAVE_SYN 27 /* Record SYN headers for new
  66. connections. */
  67. #define TCP_SAVED_SYN 28 /* Get SYN headers recorded for
  68. connection. */
  69. #define TCP_REPAIR_WINDOW 29 /* Get/set window parameters. */
  70. #ifdef __USE_MISC
  71. # include <sys/types.h>
  72. # include <sys/socket.h>
  73. # include <stdint.h>
  74. typedef uint32_t tcp_seq;
  75. /*
  76. * TCP header.
  77. * Per RFC 793, September, 1981.
  78. */
  79. struct tcphdr
  80. {
  81. __extension__ union
  82. {
  83. struct
  84. {
  85. uint16_t th_sport; /* source port */
  86. uint16_t th_dport; /* destination port */
  87. tcp_seq th_seq; /* sequence number */
  88. tcp_seq th_ack; /* acknowledgement number */
  89. # if __BYTE_ORDER == __LITTLE_ENDIAN
  90. uint8_t th_x2:4; /* (unused) */
  91. uint8_t th_off:4; /* data offset */
  92. # endif
  93. # if __BYTE_ORDER == __BIG_ENDIAN
  94. uint8_t th_off:4; /* data offset */
  95. uint8_t th_x2:4; /* (unused) */
  96. # endif
  97. uint8_t th_flags;
  98. # define TH_FIN 0x01
  99. # define TH_SYN 0x02
  100. # define TH_RST 0x04
  101. # define TH_PUSH 0x08
  102. # define TH_ACK 0x10
  103. # define TH_URG 0x20
  104. uint16_t th_win; /* window */
  105. uint16_t th_sum; /* checksum */
  106. uint16_t th_urp; /* urgent pointer */
  107. };
  108. struct
  109. {
  110. uint16_t source;
  111. uint16_t dest;
  112. uint32_t seq;
  113. uint32_t ack_seq;
  114. # if __BYTE_ORDER == __LITTLE_ENDIAN
  115. uint16_t res1:4;
  116. uint16_t doff:4;
  117. uint16_t fin:1;
  118. uint16_t syn:1;
  119. uint16_t rst:1;
  120. uint16_t psh:1;
  121. uint16_t ack:1;
  122. uint16_t urg:1;
  123. uint16_t res2:2;
  124. # elif __BYTE_ORDER == __BIG_ENDIAN
  125. uint16_t doff:4;
  126. uint16_t res1:4;
  127. uint16_t res2:2;
  128. uint16_t urg:1;
  129. uint16_t ack:1;
  130. uint16_t psh:1;
  131. uint16_t rst:1;
  132. uint16_t syn:1;
  133. uint16_t fin:1;
  134. # else
  135. # error "Adjust your <bits/endian.h> defines"
  136. # endif
  137. uint16_t window;
  138. uint16_t check;
  139. uint16_t urg_ptr;
  140. };
  141. };
  142. };
  143. enum
  144. {
  145. TCP_ESTABLISHED = 1,
  146. TCP_SYN_SENT,
  147. TCP_SYN_RECV,
  148. TCP_FIN_WAIT1,
  149. TCP_FIN_WAIT2,
  150. TCP_TIME_WAIT,
  151. TCP_CLOSE,
  152. TCP_CLOSE_WAIT,
  153. TCP_LAST_ACK,
  154. TCP_LISTEN,
  155. TCP_CLOSING /* now a valid state */
  156. };
  157. # define TCPOPT_EOL 0
  158. # define TCPOPT_NOP 1
  159. # define TCPOPT_MAXSEG 2
  160. # define TCPOLEN_MAXSEG 4
  161. # define TCPOPT_WINDOW 3
  162. # define TCPOLEN_WINDOW 3
  163. # define TCPOPT_SACK_PERMITTED 4 /* Experimental */
  164. # define TCPOLEN_SACK_PERMITTED 2
  165. # define TCPOPT_SACK 5 /* Experimental */
  166. # define TCPOPT_TIMESTAMP 8
  167. # define TCPOLEN_TIMESTAMP 10
  168. # define TCPOLEN_TSTAMP_APPA (TCPOLEN_TIMESTAMP+2) /* appendix A */
  169. # define TCPOPT_TSTAMP_HDR \
  170. (TCPOPT_NOP<<24|TCPOPT_NOP<<16|TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)
  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. # define TCP_MAXWIN 65535 /* largest value for (unscaled) window */
  179. # define TCP_MAX_WINSHIFT 14 /* maximum window shift */
  180. # define SOL_TCP 6 /* TCP level */
  181. # define TCPI_OPT_TIMESTAMPS 1
  182. # define TCPI_OPT_SACK 2
  183. # define TCPI_OPT_WSCALE 4
  184. # define TCPI_OPT_ECN 8 /* ECN was negociated at TCP session init */
  185. # define TCPI_OPT_ECN_SEEN 16 /* we received at least one packet with ECT */
  186. # define TCPI_OPT_SYN_DATA 32 /* SYN-ACK acked data in SYN sent or rcvd */
  187. /* Values for tcpi_state. */
  188. enum tcp_ca_state
  189. {
  190. TCP_CA_Open = 0,
  191. TCP_CA_Disorder = 1,
  192. TCP_CA_CWR = 2,
  193. TCP_CA_Recovery = 3,
  194. TCP_CA_Loss = 4
  195. };
  196. struct tcp_info
  197. {
  198. uint8_t tcpi_state;
  199. uint8_t tcpi_ca_state;
  200. uint8_t tcpi_retransmits;
  201. uint8_t tcpi_probes;
  202. uint8_t tcpi_backoff;
  203. uint8_t tcpi_options;
  204. uint8_t tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4;
  205. uint32_t tcpi_rto;
  206. uint32_t tcpi_ato;
  207. uint32_t tcpi_snd_mss;
  208. uint32_t tcpi_rcv_mss;
  209. uint32_t tcpi_unacked;
  210. uint32_t tcpi_sacked;
  211. uint32_t tcpi_lost;
  212. uint32_t tcpi_retrans;
  213. uint32_t tcpi_fackets;
  214. /* Times. */
  215. uint32_t tcpi_last_data_sent;
  216. uint32_t tcpi_last_ack_sent; /* Not remembered, sorry. */
  217. uint32_t tcpi_last_data_recv;
  218. uint32_t tcpi_last_ack_recv;
  219. /* Metrics. */
  220. uint32_t tcpi_pmtu;
  221. uint32_t tcpi_rcv_ssthresh;
  222. uint32_t tcpi_rtt;
  223. uint32_t tcpi_rttvar;
  224. uint32_t tcpi_snd_ssthresh;
  225. uint32_t tcpi_snd_cwnd;
  226. uint32_t tcpi_advmss;
  227. uint32_t tcpi_reordering;
  228. uint32_t tcpi_rcv_rtt;
  229. uint32_t tcpi_rcv_space;
  230. uint32_t tcpi_total_retrans;
  231. };
  232. /* For TCP_MD5SIG socket option. */
  233. #define TCP_MD5SIG_MAXKEYLEN 80
  234. struct tcp_md5sig
  235. {
  236. struct sockaddr_storage tcpm_addr; /* Address associated. */
  237. uint16_t __tcpm_pad1; /* Zero. */
  238. uint16_t tcpm_keylen; /* Key length. */
  239. uint32_t __tcpm_pad2; /* Zero. */
  240. uint8_t tcpm_key[TCP_MD5SIG_MAXKEYLEN]; /* Key (binary). */
  241. };
  242. /* For socket repair options. */
  243. struct tcp_repair_opt
  244. {
  245. uint32_t opt_code;
  246. uint32_t opt_val;
  247. };
  248. /* Queue to repair, for TCP_REPAIR_QUEUE. */
  249. enum
  250. {
  251. TCP_NO_QUEUE,
  252. TCP_RECV_QUEUE,
  253. TCP_SEND_QUEUE,
  254. TCP_QUEUES_NR,
  255. };
  256. /* For cookie transactions socket options. */
  257. #define TCP_COOKIE_MIN 8 /* 64-bits */
  258. #define TCP_COOKIE_MAX 16 /* 128-bits */
  259. #define TCP_COOKIE_PAIR_SIZE (2*TCP_COOKIE_MAX)
  260. /* Flags for both getsockopt and setsockopt */
  261. #define TCP_COOKIE_IN_ALWAYS (1 << 0) /* Discard SYN without cookie */
  262. #define TCP_COOKIE_OUT_NEVER (1 << 1) /* Prohibit outgoing cookies,
  263. * supercedes everything. */
  264. /* Flags for getsockopt */
  265. #define TCP_S_DATA_IN (1 << 2) /* Was data received? */
  266. #define TCP_S_DATA_OUT (1 << 3) /* Was data sent? */
  267. #define TCP_MSS_DEFAULT 536U /* IPv4 (RFC1122, RFC2581) */
  268. #define TCP_MSS_DESIRED 1220U /* IPv6 (tunneled), EDNS0 (RFC3226) */
  269. struct tcp_cookie_transactions
  270. {
  271. uint16_t tcpct_flags;
  272. uint8_t __tcpct_pad1;
  273. uint8_t tcpct_cookie_desired;
  274. uint16_t tcpct_s_data_desired;
  275. uint16_t tcpct_used;
  276. uint8_t tcpct_value[TCP_MSS_DEFAULT];
  277. };
  278. /* For use with TCP_REPAIR_WINDOW. */
  279. struct tcp_repair_window
  280. {
  281. uint32_t snd_wl1;
  282. uint32_t snd_wnd;
  283. uint32_t max_window;
  284. uint32_t rcv_wnd;
  285. uint32_t rcv_wup;
  286. };
  287. #endif /* Misc. */
  288. #endif /* netinet/tcp.h */