bio_tfo.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Contains definitions for simplifying the use of TCP Fast Open
  11. * (RFC7413) in OpenSSL socket BIOs.
  12. */
  13. /* If a supported OS is added here, update test/bio_tfo_test.c */
  14. #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
  15. # if defined(OPENSSL_SYS_MACOSX) || defined(__FreeBSD__)
  16. # include <sys/sysctl.h>
  17. # endif
  18. /*
  19. * OSSL_TFO_SYSCTL is used to determine if TFO is supported by
  20. * this kernel, and if supported, if it is enabled. This is more of
  21. * a problem on FreeBSD 10.3 ~ 11.4, where TCP_FASTOPEN was defined,
  22. * but not enabled by default in the kernel, and only for the server.
  23. * Linux does not have sysctlbyname(), and the closest equivalent
  24. * is to go into the /proc filesystem, but I'm not sure it's
  25. * worthwhile.
  26. *
  27. * On MacOS and Linux:
  28. * These operating systems use a single parameter to control TFO.
  29. * The OSSL_TFO_CLIENT_FLAG and OSSL_TFO_SERVER_FLAGS are used to
  30. * determine if TFO is enabled for the client and server respectively.
  31. *
  32. * OSSL_TFO_CLIENT_FLAG = 1 = client TFO enabled
  33. * OSSL_TFO_SERVER_FLAG = 2 = server TFO enabled
  34. *
  35. * Such that:
  36. * 0 = TFO disabled
  37. * 3 = server and client TFO enabled
  38. *
  39. * macOS 10.14 and later support TFO.
  40. * Linux kernel 3.6 added support for client TFO.
  41. * Linux kernel 3.7 added support for server TFO.
  42. * Linux kernel 3.13 enabled TFO by default.
  43. * Linux kernel 4.11 added the TCP_FASTOPEN_CONNECT option.
  44. *
  45. * On FreeBSD:
  46. * FreeBSD 10.3 ~ 11.4 uses a single sysctl for server enable.
  47. * FreeBSD 12.0 and later uses separate sysctls for server and
  48. * client enable.
  49. *
  50. * Some options are purposely NOT defined per-platform
  51. *
  52. * OSSL_TFO_SYSCTL
  53. * Defined as a sysctlbyname() option to determine if
  54. * TFO is enabled in the kernel (macOS, FreeBSD)
  55. *
  56. * OSSL_TFO_SERVER_SOCKOPT
  57. * Defined to indicate the socket option used to enable
  58. * TFO on a server socket (all)
  59. *
  60. * OSSL_TFO_SERVER_SOCKOPT_VALUE
  61. * Value to be used with OSSL_TFO_SERVER_SOCKOPT
  62. *
  63. * OSSL_TFO_CONNECTX
  64. * Use the connectx() function to make a client connection
  65. * (macOS)
  66. *
  67. * OSSL_TFO_CLIENT_SOCKOPT
  68. * Defined to indicate the socket option used to enable
  69. * TFO on a client socket (FreeBSD, Linux 4.14 and later)
  70. *
  71. * OSSL_TFO_SENDTO
  72. * Defined to indicate the sendto() message type to
  73. * be used to initiate a TFO connection (FreeBSD,
  74. * Linux pre-4.14)
  75. *
  76. * OSSL_TFO_DO_NOT_CONNECT
  77. * Defined to skip calling connect() when creating a
  78. * client socket (macOS, FreeBSD, Linux pre-4.14)
  79. */
  80. # if defined(OPENSSL_SYS_WINDOWS)
  81. /*
  82. * NO WINDOWS SUPPORT
  83. *
  84. * But this is what would be used on the server:
  85. *
  86. * define OSSL_TFO_SERVER_SOCKOPT TCP_FASTOPEN
  87. * define OSSL_TFO_SERVER_SOCKOPT_VALUE 1
  88. *
  89. * Still have to figure out client support
  90. */
  91. # undef TCP_FASTOPEN
  92. # endif
  93. /* NO VMS SUPPORT */
  94. # if defined(OPENSSL_SYS_VMS)
  95. # undef TCP_FASTOPEN
  96. # endif
  97. # if defined(OPENSSL_SYS_MACOSX)
  98. # define OSSL_TFO_SYSCTL "net.inet.tcp.fastopen"
  99. # define OSSL_TFO_SERVER_SOCKOPT TCP_FASTOPEN
  100. # define OSSL_TFO_SERVER_SOCKOPT_VALUE 1
  101. # define OSSL_TFO_CONNECTX 1
  102. # define OSSL_TFO_DO_NOT_CONNECT 1
  103. # define OSSL_TFO_CLIENT_FLAG 1
  104. # define OSSL_TFO_SERVER_FLAG 2
  105. # endif
  106. # if defined(__FreeBSD__)
  107. # if defined(TCP_FASTOPEN_PSK_LEN)
  108. /* As of 12.0 these are the SYSCTLs */
  109. # define OSSL_TFO_SYSCTL_SERVER "net.inet.tcp.fastopen.server_enable"
  110. # define OSSL_TFO_SYSCTL_CLIENT "net.inet.tcp.fastopen.client_enable"
  111. # define OSSL_TFO_SERVER_SOCKOPT TCP_FASTOPEN
  112. # define OSSL_TFO_SERVER_SOCKOPT_VALUE MAX_LISTEN
  113. # define OSSL_TFO_CLIENT_SOCKOPT TCP_FASTOPEN
  114. # define OSSL_TFO_DO_NOT_CONNECT 1
  115. # define OSSL_TFO_SENDTO 0
  116. /* These are the same because the sysctl are client/server-specific */
  117. # define OSSL_TFO_CLIENT_FLAG 1
  118. # define OSSL_TFO_SERVER_FLAG 1
  119. # else
  120. /* 10.3 through 11.4 SYSCTL - ONLY SERVER SUPPORT */
  121. # define OSSL_TFO_SYSCTL "net.inet.tcp.fastopen.enabled"
  122. # define OSSL_TFO_SERVER_SOCKOPT TCP_FASTOPEN
  123. # define OSSL_TFO_SERVER_SOCKOPT_VALUE MAX_LISTEN
  124. # define OSSL_TFO_SERVER_FLAG 1
  125. # endif
  126. # endif
  127. # if defined(OPENSSL_SYS_LINUX)
  128. /* OSSL_TFO_PROC not used, but of interest */
  129. # define OSSL_TFO_PROC "/proc/sys/net/ipv4/tcp_fastopen"
  130. # define OSSL_TFO_SERVER_SOCKOPT TCP_FASTOPEN
  131. # define OSSL_TFO_SERVER_SOCKOPT_VALUE MAX_LISTEN
  132. # if defined(TCP_FASTOPEN_CONNECT)
  133. # define OSSL_TFO_CLIENT_SOCKOPT TCP_FASTOPEN_CONNECT
  134. # else
  135. # define OSSL_TFO_SENDTO MSG_FASTOPEN
  136. # define OSSL_TFO_DO_NOT_CONNECT 1
  137. # endif
  138. # define OSSL_TFO_CLIENT_FLAG 1
  139. # define OSSL_TFO_SERVER_FLAG 2
  140. # endif
  141. #endif