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