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