bio_sock2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright 2016-2022 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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include "bio_local.h"
  13. #include "internal/ktls.h"
  14. #include "internal/bio_tfo.h"
  15. #include <openssl/err.h>
  16. #ifndef OPENSSL_NO_SOCK
  17. # ifdef SO_MAXCONN
  18. # define MAX_LISTEN SO_MAXCONN
  19. # elif defined(SOMAXCONN)
  20. # define MAX_LISTEN SOMAXCONN
  21. # else
  22. # define MAX_LISTEN 32
  23. # endif
  24. /*-
  25. * BIO_socket - create a socket
  26. * @domain: the socket domain (AF_INET, AF_INET6, AF_UNIX, ...)
  27. * @socktype: the socket type (SOCK_STEAM, SOCK_DGRAM)
  28. * @protocol: the protocol to use (IPPROTO_TCP, IPPROTO_UDP)
  29. * @options: BIO socket options (currently unused)
  30. *
  31. * Creates a socket. This should be called before calling any
  32. * of BIO_connect and BIO_listen.
  33. *
  34. * Returns the file descriptor on success or INVALID_SOCKET on failure. On
  35. * failure errno is set, and a status is added to the OpenSSL error stack.
  36. */
  37. int BIO_socket(int domain, int socktype, int protocol, int options)
  38. {
  39. int sock = -1;
  40. if (BIO_sock_init() != 1)
  41. return INVALID_SOCKET;
  42. sock = socket(domain, socktype, protocol);
  43. if (sock == -1) {
  44. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  45. "calling socket()");
  46. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
  47. return INVALID_SOCKET;
  48. }
  49. return sock;
  50. }
  51. /*-
  52. * BIO_connect - connect to an address
  53. * @sock: the socket to connect with
  54. * @addr: the address to connect to
  55. * @options: BIO socket options
  56. *
  57. * Connects to the address using the given socket and options.
  58. *
  59. * Options can be a combination of the following:
  60. * - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages.
  61. * - BIO_SOCK_NONBLOCK: Make the socket non-blocking.
  62. * - BIO_SOCK_NODELAY: don't delay small messages.
  63. * - BIO_SOCK_TFO: use TCP Fast Open
  64. *
  65. * options holds BIO socket options that can be used
  66. * You should call this for every address returned by BIO_lookup
  67. * until the connection is successful.
  68. *
  69. * Returns 1 on success or 0 on failure. On failure errno is set
  70. * and an error status is added to the OpenSSL error stack.
  71. */
  72. int BIO_connect(int sock, const BIO_ADDR *addr, int options)
  73. {
  74. const int on = 1;
  75. if (sock == -1) {
  76. ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
  77. return 0;
  78. }
  79. if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
  80. return 0;
  81. if (options & BIO_SOCK_KEEPALIVE) {
  82. if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  83. (const void *)&on, sizeof(on)) != 0) {
  84. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  85. "calling setsockopt()");
  86. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_KEEPALIVE);
  87. return 0;
  88. }
  89. }
  90. if (options & BIO_SOCK_NODELAY) {
  91. if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
  92. (const void *)&on, sizeof(on)) != 0) {
  93. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  94. "calling setsockopt()");
  95. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_NODELAY);
  96. return 0;
  97. }
  98. }
  99. if (options & BIO_SOCK_TFO) {
  100. # if defined(OSSL_TFO_CLIENT_FLAG)
  101. # if defined(OSSL_TFO_SYSCTL_CLIENT)
  102. int enabled = 0;
  103. size_t enabledlen = sizeof(enabled);
  104. /* Later FreeBSD */
  105. if (sysctlbyname(OSSL_TFO_SYSCTL_CLIENT, &enabled, &enabledlen, NULL, 0) < 0) {
  106. ERR_raise(ERR_LIB_BIO, BIO_R_TFO_NO_KERNEL_SUPPORT);
  107. return 0;
  108. }
  109. /* Need to check for client flag */
  110. if (!(enabled & OSSL_TFO_CLIENT_FLAG)) {
  111. ERR_raise(ERR_LIB_BIO, BIO_R_TFO_DISABLED);
  112. return 0;
  113. }
  114. # elif defined(OSSL_TFO_SYSCTL)
  115. int enabled = 0;
  116. size_t enabledlen = sizeof(enabled);
  117. /* macOS */
  118. if (sysctlbyname(OSSL_TFO_SYSCTL, &enabled, &enabledlen, NULL, 0) < 0) {
  119. ERR_raise(ERR_LIB_BIO, BIO_R_TFO_NO_KERNEL_SUPPORT);
  120. return 0;
  121. }
  122. /* Need to check for client flag */
  123. if (!(enabled & OSSL_TFO_CLIENT_FLAG)) {
  124. ERR_raise(ERR_LIB_BIO, BIO_R_TFO_DISABLED);
  125. return 0;
  126. }
  127. # endif
  128. # endif
  129. # if defined(OSSL_TFO_CONNECTX)
  130. sa_endpoints_t sae;
  131. memset(&sae, 0, sizeof(sae));
  132. sae.sae_dstaddr = BIO_ADDR_sockaddr(addr);
  133. sae.sae_dstaddrlen = BIO_ADDR_sockaddr_size(addr);
  134. if (connectx(sock, &sae, SAE_ASSOCID_ANY,
  135. CONNECT_DATA_IDEMPOTENT | CONNECT_RESUME_ON_READ_WRITE,
  136. NULL, 0, NULL, NULL) == -1) {
  137. if (!BIO_sock_should_retry(-1)) {
  138. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  139. "calling connectx()");
  140. ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
  141. }
  142. return 0;
  143. }
  144. # endif
  145. # if defined(OSSL_TFO_CLIENT_SOCKOPT)
  146. if (setsockopt(sock, IPPROTO_TCP, OSSL_TFO_CLIENT_SOCKOPT,
  147. (const void *)&on, sizeof(on)) != 0) {
  148. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  149. "calling setsockopt()");
  150. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_TFO);
  151. return 0;
  152. }
  153. # endif
  154. # if defined(OSSL_TFO_DO_NOT_CONNECT)
  155. return 1;
  156. # endif
  157. }
  158. if (connect(sock, BIO_ADDR_sockaddr(addr),
  159. BIO_ADDR_sockaddr_size(addr)) == -1) {
  160. if (!BIO_sock_should_retry(-1)) {
  161. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  162. "calling connect()");
  163. ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
  164. }
  165. return 0;
  166. }
  167. # ifndef OPENSSL_NO_KTLS
  168. /*
  169. * The new socket is created successfully regardless of ktls_enable.
  170. * ktls_enable doesn't change any functionality of the socket, except
  171. * changing the setsockopt to enable the processing of ktls_start.
  172. * Thus, it is not a problem to call it for non-TLS sockets.
  173. */
  174. ktls_enable(sock);
  175. # endif
  176. return 1;
  177. }
  178. /*-
  179. * BIO_bind - bind socket to address
  180. * @sock: the socket to set
  181. * @addr: local address to bind to
  182. * @options: BIO socket options
  183. *
  184. * Binds to the address using the given socket and options.
  185. *
  186. * Options can be a combination of the following:
  187. * - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination
  188. * for a recently closed port.
  189. *
  190. * When restarting the program it could be that the port is still in use. If
  191. * you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway.
  192. * It's recommended that you use this.
  193. */
  194. int BIO_bind(int sock, const BIO_ADDR *addr, int options)
  195. {
  196. # ifndef OPENSSL_SYS_WINDOWS
  197. int on = 1;
  198. # endif
  199. if (sock == -1) {
  200. ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
  201. return 0;
  202. }
  203. # ifndef OPENSSL_SYS_WINDOWS
  204. /*
  205. * SO_REUSEADDR has different behavior on Windows than on
  206. * other operating systems, don't set it there.
  207. */
  208. if (options & BIO_SOCK_REUSEADDR) {
  209. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  210. (const void *)&on, sizeof(on)) != 0) {
  211. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  212. "calling setsockopt()");
  213. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_REUSEADDR);
  214. return 0;
  215. }
  216. }
  217. # endif
  218. if (bind(sock, BIO_ADDR_sockaddr(addr), BIO_ADDR_sockaddr_size(addr)) != 0) {
  219. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error() /* may be 0 */,
  220. "calling bind()");
  221. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_BIND_SOCKET);
  222. return 0;
  223. }
  224. return 1;
  225. }
  226. /*-
  227. * BIO_listen - Creates a listen socket
  228. * @sock: the socket to listen with
  229. * @addr: local address to bind to
  230. * @options: BIO socket options
  231. *
  232. * Binds to the address using the given socket and options, then
  233. * starts listening for incoming connections.
  234. *
  235. * Options can be a combination of the following:
  236. * - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages.
  237. * - BIO_SOCK_NONBLOCK: Make the socket non-blocking.
  238. * - BIO_SOCK_NODELAY: don't delay small messages.
  239. * - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination
  240. * for a recently closed port.
  241. * - BIO_SOCK_V6_ONLY: When creating an IPv6 socket, make it listen only
  242. * for IPv6 addresses and not IPv4 addresses mapped to IPv6.
  243. * - BIO_SOCK_TFO: accept TCP fast open (set TCP_FASTOPEN)
  244. *
  245. * It's recommended that you set up both an IPv6 and IPv4 listen socket, and
  246. * then check both for new clients that connect to it. You want to set up
  247. * the socket as non-blocking in that case since else it could hang.
  248. *
  249. * Not all operating systems support IPv4 addresses on an IPv6 socket, and for
  250. * others it's an option. If you pass the BIO_LISTEN_V6_ONLY it will try to
  251. * create the IPv6 sockets to only listen for IPv6 connection.
  252. *
  253. * It could be that the first BIO_listen() call will listen to all the IPv6
  254. * and IPv4 addresses and that then trying to bind to the IPv4 address will
  255. * fail. We can't tell the difference between already listening ourself to
  256. * it and someone else listening to it when failing and errno is EADDRINUSE, so
  257. * it's recommended to not give an error in that case if the first call was
  258. * successful.
  259. *
  260. * When restarting the program it could be that the port is still in use. If
  261. * you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway.
  262. * It's recommended that you use this.
  263. */
  264. int BIO_listen(int sock, const BIO_ADDR *addr, int options)
  265. {
  266. int on = 1;
  267. int socktype;
  268. socklen_t socktype_len = sizeof(socktype);
  269. if (sock == -1) {
  270. ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
  271. return 0;
  272. }
  273. if (getsockopt(sock, SOL_SOCKET, SO_TYPE,
  274. (void *)&socktype, &socktype_len) != 0
  275. || socktype_len != sizeof(socktype)) {
  276. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  277. "calling getsockopt()");
  278. ERR_raise(ERR_LIB_BIO, BIO_R_GETTING_SOCKTYPE);
  279. return 0;
  280. }
  281. if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
  282. return 0;
  283. if (options & BIO_SOCK_KEEPALIVE) {
  284. if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  285. (const void *)&on, sizeof(on)) != 0) {
  286. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  287. "calling setsockopt()");
  288. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_KEEPALIVE);
  289. return 0;
  290. }
  291. }
  292. if (options & BIO_SOCK_NODELAY) {
  293. if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
  294. (const void *)&on, sizeof(on)) != 0) {
  295. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  296. "calling setsockopt()");
  297. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_NODELAY);
  298. return 0;
  299. }
  300. }
  301. /* On OpenBSD it is always IPv6 only with IPv6 sockets thus read-only */
  302. # if defined(IPV6_V6ONLY) && !defined(__OpenBSD__)
  303. if (BIO_ADDR_family(addr) == AF_INET6) {
  304. /*
  305. * Note: Windows default of IPV6_V6ONLY is ON, and Linux is OFF.
  306. * Therefore we always have to use setsockopt here.
  307. */
  308. on = options & BIO_SOCK_V6_ONLY ? 1 : 0;
  309. if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
  310. (const void *)&on, sizeof(on)) != 0) {
  311. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  312. "calling setsockopt()");
  313. ERR_raise(ERR_LIB_BIO, BIO_R_LISTEN_V6_ONLY);
  314. return 0;
  315. }
  316. }
  317. # endif
  318. if (!BIO_bind(sock, addr, options))
  319. return 0;
  320. if (socktype != SOCK_DGRAM && listen(sock, MAX_LISTEN) == -1) {
  321. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  322. "calling listen()");
  323. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_LISTEN_SOCKET);
  324. return 0;
  325. }
  326. # if defined(OSSL_TFO_SERVER_SOCKOPT)
  327. /*
  328. * Must do it explicitly after listen() for macOS, still
  329. * works fine on other OS's
  330. */
  331. if ((options & BIO_SOCK_TFO) && socktype != SOCK_DGRAM) {
  332. int q = OSSL_TFO_SERVER_SOCKOPT_VALUE;
  333. # if defined(OSSL_TFO_CLIENT_FLAG)
  334. # if defined(OSSL_TFO_SYSCTL_SERVER)
  335. int enabled = 0;
  336. size_t enabledlen = sizeof(enabled);
  337. /* Later FreeBSD */
  338. if (sysctlbyname(OSSL_TFO_SYSCTL_SERVER, &enabled, &enabledlen, NULL, 0) < 0) {
  339. ERR_raise(ERR_LIB_BIO, BIO_R_TFO_NO_KERNEL_SUPPORT);
  340. return 0;
  341. }
  342. /* Need to check for server flag */
  343. if (!(enabled & OSSL_TFO_SERVER_FLAG)) {
  344. ERR_raise(ERR_LIB_BIO, BIO_R_TFO_DISABLED);
  345. return 0;
  346. }
  347. # elif defined(OSSL_TFO_SYSCTL)
  348. int enabled = 0;
  349. size_t enabledlen = sizeof(enabled);
  350. /* Early FreeBSD, macOS */
  351. if (sysctlbyname(OSSL_TFO_SYSCTL, &enabled, &enabledlen, NULL, 0) < 0) {
  352. ERR_raise(ERR_LIB_BIO, BIO_R_TFO_NO_KERNEL_SUPPORT);
  353. return 0;
  354. }
  355. /* Need to check for server flag */
  356. if (!(enabled & OSSL_TFO_SERVER_FLAG)) {
  357. ERR_raise(ERR_LIB_BIO, BIO_R_TFO_DISABLED);
  358. return 0;
  359. }
  360. # endif
  361. # endif
  362. if (setsockopt(sock, IPPROTO_TCP, OSSL_TFO_SERVER_SOCKOPT,
  363. (void *)&q, sizeof(q)) < 0) {
  364. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  365. "calling setsockopt()");
  366. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_TFO);
  367. return 0;
  368. }
  369. }
  370. # endif
  371. return 1;
  372. }
  373. /*-
  374. * BIO_accept_ex - Accept new incoming connections
  375. * @sock: the listening socket
  376. * @addr: the BIO_ADDR to store the peer address in
  377. * @options: BIO socket options, applied on the accepted socket.
  378. *
  379. */
  380. int BIO_accept_ex(int accept_sock, BIO_ADDR *addr_, int options)
  381. {
  382. socklen_t len;
  383. int accepted_sock;
  384. BIO_ADDR locaddr;
  385. BIO_ADDR *addr = addr_ == NULL ? &locaddr : addr_;
  386. len = sizeof(*addr);
  387. accepted_sock = accept(accept_sock,
  388. BIO_ADDR_sockaddr_noconst(addr), &len);
  389. if (accepted_sock == -1) {
  390. if (!BIO_sock_should_retry(accepted_sock)) {
  391. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  392. "calling accept()");
  393. ERR_raise(ERR_LIB_BIO, BIO_R_ACCEPT_ERROR);
  394. }
  395. return INVALID_SOCKET;
  396. }
  397. if (!BIO_socket_nbio(accepted_sock, (options & BIO_SOCK_NONBLOCK) != 0)) {
  398. closesocket(accepted_sock);
  399. return INVALID_SOCKET;
  400. }
  401. return accepted_sock;
  402. }
  403. /*-
  404. * BIO_closesocket - Close a socket
  405. * @sock: the socket to close
  406. */
  407. int BIO_closesocket(int sock)
  408. {
  409. if (sock < 0 || closesocket(sock) < 0)
  410. return 0;
  411. return 1;
  412. }
  413. #endif