2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include "bio_local.h"
14 #include "internal/bio_tfo.h"
15 #include "internal/ktls.h"
17 #ifndef OPENSSL_NO_SOCK
19 typedef struct bio_connect_st {
22 int connect_sock_type;
26 # ifndef OPENSSL_NO_KTLS
27 unsigned char record_type;
31 BIO_ADDRINFO *addr_first;
32 const BIO_ADDRINFO *addr_iter;
34 * int socket; this will be kept in bio->num so that it is compatible
35 * with the bss_sock bio
38 * called when the connection is initially made callback(BIO,state,ret);
39 * The callback should return 'ret'. state is for compatibility with the
42 BIO_info_cb *info_callback;
44 * Used when connect_sock_type is SOCK_DGRAM. Owned by us; we forward
45 * read/write(mmsg) calls to this if present.
50 static int conn_write(BIO *h, const char *buf, int num);
51 static int conn_read(BIO *h, char *buf, int size);
52 static int conn_puts(BIO *h, const char *str);
53 static int conn_gets(BIO *h, char *buf, int size);
54 static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
55 static int conn_new(BIO *h);
56 static int conn_free(BIO *data);
57 static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
58 static int conn_sendmmsg(BIO *h, BIO_MSG *m, size_t s, size_t n,
59 uint64_t f, size_t *mp);
60 static int conn_recvmmsg(BIO *h, BIO_MSG *m, size_t s, size_t n,
61 uint64_t f, size_t *mp);
63 static int conn_state(BIO *b, BIO_CONNECT *c);
64 static void conn_close_socket(BIO *data);
65 static BIO_CONNECT *BIO_CONNECT_new(void);
66 static void BIO_CONNECT_free(BIO_CONNECT *a);
68 #define BIO_CONN_S_BEFORE 1
69 #define BIO_CONN_S_GET_ADDR 2
70 #define BIO_CONN_S_CREATE_SOCKET 3
71 #define BIO_CONN_S_CONNECT 4
72 #define BIO_CONN_S_OK 5
73 #define BIO_CONN_S_BLOCKED_CONNECT 6
74 #define BIO_CONN_S_CONNECT_ERROR 7
76 static const BIO_METHOD methods_connectp = {
93 static int conn_create_dgram_bio(BIO *b, BIO_CONNECT *c)
95 if (c->connect_sock_type != SOCK_DGRAM)
98 #ifndef OPENSSL_NO_DGRAM
99 c->dgram_bio = BIO_new_dgram(b->num, 0);
100 if (c->dgram_bio == NULL)
107 c->state = BIO_CONN_S_CONNECT_ERROR;
111 static int conn_state(BIO *b, BIO_CONNECT *c)
114 BIO_info_cb *cb = NULL;
116 if (c->info_callback != NULL)
117 cb = c->info_callback;
121 case BIO_CONN_S_BEFORE:
122 if (c->param_hostname == NULL && c->param_service == NULL) {
123 ERR_raise_data(ERR_LIB_BIO,
124 BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED,
125 "hostname=%s service=%s",
126 c->param_hostname, c->param_service);
129 c->state = BIO_CONN_S_GET_ADDR;
132 case BIO_CONN_S_GET_ADDR:
134 int family = AF_UNSPEC;
135 switch (c->connect_family) {
136 case BIO_FAMILY_IPV6:
137 if (1) { /* This is a trick we use to avoid bit rot.
138 * at least the "else" part will always be
145 ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY);
149 case BIO_FAMILY_IPV4:
152 case BIO_FAMILY_IPANY:
156 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY);
159 if (BIO_lookup(c->param_hostname, c->param_service,
161 family, c->connect_sock_type,
162 &c->addr_first) == 0)
165 if (c->addr_first == NULL) {
166 ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING);
169 c->addr_iter = c->addr_first;
170 c->state = BIO_CONN_S_CREATE_SOCKET;
173 case BIO_CONN_S_CREATE_SOCKET:
174 ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
175 BIO_ADDRINFO_socktype(c->addr_iter),
176 BIO_ADDRINFO_protocol(c->addr_iter), 0);
177 if (ret == (int)INVALID_SOCKET) {
178 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
179 "calling socket(%s, %s)",
180 c->param_hostname, c->param_service);
181 ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
185 c->state = BIO_CONN_S_CONNECT;
188 case BIO_CONN_S_CONNECT:
189 BIO_clear_retry_flags(b);
191 ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
192 BIO_SOCK_KEEPALIVE | c->connect_mode);
195 if (BIO_sock_should_retry(ret)) {
196 BIO_set_retry_special(b);
197 c->state = BIO_CONN_S_BLOCKED_CONNECT;
198 b->retry_reason = BIO_RR_CONNECT;
200 } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
203 * if there are more addresses to try, do that first
205 BIO_closesocket(b->num);
206 c->state = BIO_CONN_S_CREATE_SOCKET;
210 ERR_clear_last_mark();
211 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
212 "calling connect(%s, %s)",
213 c->param_hostname, c->param_service);
214 c->state = BIO_CONN_S_CONNECT_ERROR;
219 ERR_clear_last_mark();
220 if (!conn_create_dgram_bio(b, c))
222 c->state = BIO_CONN_S_OK;
226 case BIO_CONN_S_BLOCKED_CONNECT:
227 /* wait for socket being writable, before querying BIO_sock_error */
228 if (BIO_socket_wait(b->num, 0, time(NULL)) == 0)
230 i = BIO_sock_error(b->num);
232 BIO_clear_retry_flags(b);
233 if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) {
235 * if there are more addresses to try, do that first
237 BIO_closesocket(b->num);
238 c->state = BIO_CONN_S_CREATE_SOCKET;
241 ERR_raise_data(ERR_LIB_SYS, i,
242 "calling connect(%s, %s)",
243 c->param_hostname, c->param_service);
244 ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR);
248 if (!conn_create_dgram_bio(b, c))
250 c->state = BIO_CONN_S_OK;
251 # ifndef OPENSSL_NO_KTLS
253 * The new socket is created successfully regardless of ktls_enable.
254 * ktls_enable doesn't change any functionality of the socket, except
255 * changing the setsockopt to enable the processing of ktls_start.
256 * Thus, it is not a problem to call it for non-TLS sockets.
263 case BIO_CONN_S_CONNECT_ERROR:
264 ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
277 if ((ret = cb((BIO *)b, c->state, ret)) == 0)
282 /* Loop does not exit */
285 ret = cb((BIO *)b, c->state, ret);
290 static BIO_CONNECT *BIO_CONNECT_new(void)
294 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
296 ret->state = BIO_CONN_S_BEFORE;
297 ret->connect_family = BIO_FAMILY_IPANY;
298 ret->connect_sock_type = SOCK_STREAM;
302 static void BIO_CONNECT_free(BIO_CONNECT *a)
306 OPENSSL_free(a->param_hostname);
307 OPENSSL_free(a->param_service);
308 BIO_ADDRINFO_free(a->addr_first);
312 const BIO_METHOD *BIO_s_connect(void)
314 return &methods_connectp;
317 static int conn_new(BIO *bi)
320 bi->num = (int)INVALID_SOCKET;
322 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
328 static void conn_close_socket(BIO *bio)
332 c = (BIO_CONNECT *)bio->ptr;
333 if (bio->num != (int)INVALID_SOCKET) {
334 /* Only do a shutdown if things were established */
335 if (c->state == BIO_CONN_S_OK)
336 shutdown(bio->num, 2);
337 BIO_closesocket(bio->num);
338 bio->num = (int)INVALID_SOCKET;
342 static int conn_free(BIO *a)
348 data = (BIO_CONNECT *)a->ptr;
350 BIO_free(data->dgram_bio);
353 conn_close_socket(a);
354 BIO_CONNECT_free(data);
362 static int conn_read(BIO *b, char *out, int outl)
367 data = (BIO_CONNECT *)b->ptr;
368 if (data->state != BIO_CONN_S_OK) {
369 ret = conn_state(b, data);
374 if (data->dgram_bio != NULL) {
375 BIO_clear_retry_flags(b);
376 ret = BIO_read(data->dgram_bio, out, outl);
377 BIO_set_flags(b, BIO_get_retry_flags(data->dgram_bio));
382 clear_socket_error();
383 # ifndef OPENSSL_NO_KTLS
384 if (BIO_get_ktls_recv(b))
385 ret = ktls_read_record(b->num, out, outl);
388 ret = readsocket(b->num, out, outl);
389 BIO_clear_retry_flags(b);
391 if (BIO_sock_should_retry(ret))
392 BIO_set_retry_read(b);
394 b->flags |= BIO_FLAGS_IN_EOF;
400 static int conn_write(BIO *b, const char *in, int inl)
405 data = (BIO_CONNECT *)b->ptr;
406 if (data->state != BIO_CONN_S_OK) {
407 ret = conn_state(b, data);
412 if (data->dgram_bio != NULL) {
413 BIO_clear_retry_flags(b);
414 ret = BIO_write(data->dgram_bio, in, inl);
415 BIO_set_flags(b, BIO_get_retry_flags(data->dgram_bio));
419 clear_socket_error();
420 # ifndef OPENSSL_NO_KTLS
421 if (BIO_should_ktls_ctrl_msg_flag(b)) {
422 ret = ktls_send_ctrl_message(b->num, data->record_type, in, inl);
425 BIO_clear_ktls_ctrl_msg_flag(b);
429 # if defined(OSSL_TFO_SENDTO)
430 if (data->tfo_first) {
431 int peerlen = BIO_ADDRINFO_sockaddr_size(data->addr_iter);
433 ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO,
434 BIO_ADDRINFO_sockaddr(data->addr_iter), peerlen);
438 ret = writesocket(b->num, in, inl);
439 BIO_clear_retry_flags(b);
441 if (BIO_sock_should_retry(ret))
442 BIO_set_retry_write(b);
447 static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
451 const char **pptr = NULL;
454 const BIO_ADDR *dg_addr;
455 # ifndef OPENSSL_NO_KTLS
456 ktls_crypto_info_t *crypto_info;
459 data = (BIO_CONNECT *)b->ptr;
464 data->state = BIO_CONN_S_BEFORE;
465 conn_close_socket(b);
466 BIO_ADDRINFO_free(data->addr_first);
467 data->addr_first = NULL;
470 case BIO_C_DO_STATE_MACHINE:
471 /* use this one to start the connection */
472 if (data->state != BIO_CONN_S_OK)
473 ret = (long)conn_state(b, data);
477 case BIO_C_GET_CONNECT:
479 pptr = (const char **)ptr;
481 *pptr = data->param_hostname;
482 } else if (num == 1) {
483 *pptr = data->param_service;
484 } else if (num == 2) {
485 *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
486 } else if (num == 3) {
487 switch (BIO_ADDRINFO_family(data->addr_iter)) {
488 # if OPENSSL_USE_IPV6
490 ret = BIO_FAMILY_IPV6;
494 ret = BIO_FAMILY_IPV4;
497 ret = data->connect_family;
503 } else if (num == 4) {
504 ret = data->connect_mode;
512 case BIO_C_SET_CONNECT:
515 if (num == 0) { /* BIO_set_conn_hostname */
516 char *hold_service = data->param_service;
517 /* We affect the hostname regardless. However, the input
518 * string might contain a host:service spec, so we must
519 * parse it, which might or might not affect the service
522 OPENSSL_free(data->param_hostname);
523 data->param_hostname = NULL;
524 ret = BIO_parse_hostserv(ptr,
525 &data->param_hostname,
526 &data->param_service,
527 BIO_PARSE_PRIO_HOST);
528 if (hold_service != data->param_service)
529 OPENSSL_free(hold_service);
530 } else if (num == 1) { /* BIO_set_conn_port */
531 OPENSSL_free(data->param_service);
532 if ((data->param_service = OPENSSL_strdup(ptr)) == NULL)
534 } else if (num == 2) { /* BIO_set_conn_address */
535 const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
536 char *host = BIO_ADDR_hostname_string(addr, 1);
537 char *service = BIO_ADDR_service_string(addr, 1);
539 ret = host != NULL && service != NULL;
541 OPENSSL_free(data->param_hostname);
542 data->param_hostname = host;
543 OPENSSL_free(data->param_service);
544 data->param_service = service;
545 BIO_ADDRINFO_free(data->addr_first);
546 data->addr_first = NULL;
547 data->addr_iter = NULL;
550 OPENSSL_free(service);
552 } else if (num == 3) { /* BIO_set_conn_ip_family */
553 data->connect_family = *(int *)ptr;
559 case BIO_C_SET_SOCK_TYPE:
560 if ((num != SOCK_STREAM && num != SOCK_DGRAM)
561 || data->state >= BIO_CONN_S_GET_ADDR) {
566 data->connect_sock_type = (int)num;
569 case BIO_C_GET_SOCK_TYPE:
570 ret = data->connect_sock_type;
572 case BIO_C_GET_DGRAM_BIO:
573 if (data->dgram_bio != NULL) {
574 *(BIO **)ptr = data->dgram_bio;
580 case BIO_CTRL_DGRAM_GET_PEER:
581 case BIO_CTRL_DGRAM_DETECT_PEER_ADDR:
582 if (data->state != BIO_CONN_S_OK)
583 conn_state(b, data); /* best effort */
585 if (data->state >= BIO_CONN_S_CREATE_SOCKET
586 && data->addr_iter != NULL
587 && (dg_addr = BIO_ADDRINFO_address(data->addr_iter)) != NULL) {
589 ret = BIO_ADDR_sockaddr_size(dg_addr);
590 if (num == 0 || num > ret)
593 memcpy(ptr, dg_addr, num);
600 case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
601 case BIO_CTRL_GET_WPOLL_DESCRIPTOR:
603 BIO_POLL_DESCRIPTOR *pd = ptr;
605 if (data->state != BIO_CONN_S_OK)
606 conn_state(b, data); /* best effort */
608 if (data->state >= BIO_CONN_S_CREATE_SOCKET) {
609 pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
610 pd->value.fd = b->num;
618 data->connect_mode |= BIO_SOCK_NONBLOCK;
620 data->connect_mode &= ~BIO_SOCK_NONBLOCK;
622 if (data->dgram_bio != NULL)
623 ret = BIO_set_nbio(data->dgram_bio, num);
626 #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
629 data->connect_mode |= BIO_SOCK_TFO;
632 data->connect_mode &= ~BIO_SOCK_TFO;
637 case BIO_C_SET_CONNECT_MODE:
638 data->connect_mode = (int)num;
639 if (num & BIO_SOCK_TFO)
653 case BIO_CTRL_GET_CLOSE:
656 case BIO_CTRL_SET_CLOSE:
657 b->shutdown = (int)num;
659 case BIO_CTRL_PENDING:
660 case BIO_CTRL_WPENDING:
668 if (data->param_hostname)
669 BIO_set_conn_hostname(dbio, data->param_hostname);
670 if (data->param_service)
671 BIO_set_conn_port(dbio, data->param_service);
672 BIO_set_conn_ip_family(dbio, data->connect_family);
673 BIO_set_conn_mode(dbio, data->connect_mode);
675 * FIXME: the cast of the function seems unlikely to be a good
678 (void)BIO_set_info_callback(dbio, data->info_callback);
681 case BIO_CTRL_SET_CALLBACK:
682 ret = 0; /* use callback ctrl */
684 case BIO_CTRL_GET_CALLBACK:
688 fptr = (BIO_info_cb **)ptr;
689 *fptr = data->info_callback;
693 ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
695 # ifndef OPENSSL_NO_KTLS
696 case BIO_CTRL_SET_KTLS:
697 crypto_info = (ktls_crypto_info_t *)ptr;
698 ret = ktls_start(b->num, crypto_info, num);
700 BIO_set_ktls_flag(b, num);
702 case BIO_CTRL_GET_KTLS_SEND:
703 return BIO_should_ktls_flag(b, 1) != 0;
704 case BIO_CTRL_GET_KTLS_RECV:
705 return BIO_should_ktls_flag(b, 0) != 0;
706 case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
707 BIO_set_ktls_ctrl_msg_flag(b);
708 data->record_type = num;
711 case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
712 BIO_clear_ktls_ctrl_msg_flag(b);
715 case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE:
716 ret = ktls_enable_tx_zerocopy_sendfile(b->num);
718 BIO_set_ktls_zerocopy_sendfile_flag(b);
728 static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
733 data = (BIO_CONNECT *)b->ptr;
736 case BIO_CTRL_SET_CALLBACK:
738 data->info_callback = fp;
748 static int conn_puts(BIO *bp, const char *str)
753 ret = conn_write(bp, str, n);
757 int conn_gets(BIO *bio, char *buf, int size)
764 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
768 ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
773 if (bio == NULL || bio->ptr == NULL) {
774 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
777 data = (BIO_CONNECT *)bio->ptr;
778 if (data->state != BIO_CONN_S_OK) {
779 ret = conn_state(bio, data);
784 if (data->dgram_bio != NULL) {
785 ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
789 clear_socket_error();
791 # ifndef OPENSSL_NO_KTLS
792 if (BIO_get_ktls_recv(bio))
793 ret = ktls_read_record(bio->num, ptr, 1);
796 ret = readsocket(bio->num, ptr, 1);
797 BIO_clear_retry_flags(bio);
799 if (BIO_sock_should_retry(ret))
800 BIO_set_retry_read(bio);
802 bio->flags |= BIO_FLAGS_IN_EOF;
809 return ret > 0 || (bio->flags & BIO_FLAGS_IN_EOF) != 0 ? ptr - buf : ret;
812 static int conn_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs,
813 uint64_t flags, size_t *msgs_processed)
820 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
824 data = (BIO_CONNECT *)bio->ptr;
825 if (data->state != BIO_CONN_S_OK) {
826 ret = conn_state(bio, data);
833 if (data->dgram_bio == NULL) {
835 ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
839 return BIO_sendmmsg(data->dgram_bio, msg, stride, num_msgs,
840 flags, msgs_processed);
843 static int conn_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs,
844 uint64_t flags, size_t *msgs_processed)
851 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
855 data = (BIO_CONNECT *)bio->ptr;
856 if (data->state != BIO_CONN_S_OK) {
857 ret = conn_state(bio, data);
864 if (data->dgram_bio == NULL) {
866 ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
870 return BIO_recvmmsg(data->dgram_bio, msg, stride, num_msgs,
871 flags, msgs_processed);
874 BIO *BIO_new_connect(const char *str)
878 ret = BIO_new(BIO_s_connect());
881 if (BIO_set_conn_hostname(ret, str))