diff options
Diffstat (limited to 'crypto/bio/bss_conn.c')
| -rw-r--r-- | crypto/bio/bss_conn.c | 236 |
1 files changed, 110 insertions, 126 deletions
diff --git a/crypto/bio/bss_conn.c b/crypto/bio/bss_conn.c index 9d00f18297ff..335704ee3181 100644 --- a/crypto/bio/bss_conn.c +++ b/crypto/bio/bss_conn.c @@ -1,5 +1,5 @@ /* - * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -23,9 +23,9 @@ typedef struct bio_connect_st { char *param_hostname; char *param_service; int connect_mode; -# ifndef OPENSSL_NO_KTLS +#ifndef OPENSSL_NO_KTLS unsigned char record_type; -# endif +#endif int tfo_first; BIO_ADDRINFO *addr_first; @@ -56,22 +56,22 @@ static int conn_new(BIO *h); static int conn_free(BIO *data); static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *); static int conn_sendmmsg(BIO *h, BIO_MSG *m, size_t s, size_t n, - uint64_t f, size_t *mp); + uint64_t f, size_t *mp); static int conn_recvmmsg(BIO *h, BIO_MSG *m, size_t s, size_t n, - uint64_t f, size_t *mp); + uint64_t f, size_t *mp); static int conn_state(BIO *b, BIO_CONNECT *c); static void conn_close_socket(BIO *data); static BIO_CONNECT *BIO_CONNECT_new(void); static void BIO_CONNECT_free(BIO_CONNECT *a); -#define BIO_CONN_S_BEFORE 1 -#define BIO_CONN_S_GET_ADDR 2 -#define BIO_CONN_S_CREATE_SOCKET 3 -#define BIO_CONN_S_CONNECT 4 -#define BIO_CONN_S_OK 5 -#define BIO_CONN_S_BLOCKED_CONNECT 6 -#define BIO_CONN_S_CONNECT_ERROR 7 +#define BIO_CONN_S_BEFORE 1 +#define BIO_CONN_S_GET_ADDR 2 +#define BIO_CONN_S_CREATE_SOCKET 3 +#define BIO_CONN_S_CONNECT 4 +#define BIO_CONN_S_OK 5 +#define BIO_CONN_S_BLOCKED_CONNECT 6 +#define BIO_CONN_S_CONNECT_ERROR 7 static const BIO_METHOD methods_connectp = { BIO_TYPE_CONNECT, @@ -121,47 +121,47 @@ static int conn_state(BIO *b, BIO_CONNECT *c) case BIO_CONN_S_BEFORE: if (c->param_hostname == NULL && c->param_service == NULL) { ERR_raise_data(ERR_LIB_BIO, - BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED, - "hostname=%s service=%s", - c->param_hostname, c->param_service); + BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED, + "hostname=%s service=%s", + c->param_hostname, c->param_service); goto exit_loop; } c->state = BIO_CONN_S_GET_ADDR; break; - case BIO_CONN_S_GET_ADDR: - { - int family = AF_UNSPEC; - switch (c->connect_family) { - case BIO_FAMILY_IPV6: - if (1) { /* This is a trick we use to avoid bit rot. - * at least the "else" part will always be - * compiled. - */ + case BIO_CONN_S_GET_ADDR: { + int family = AF_UNSPEC; + switch (c->connect_family) { + case BIO_FAMILY_IPV6: + if (1) { /* This is a trick we use to avoid bit rot. + * at least the "else" part will always be + * compiled. + */ #if OPENSSL_USE_IPV6 - family = AF_INET6; - } else { + family = AF_INET6; + } else { #endif - ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY); - goto exit_loop; - } - break; - case BIO_FAMILY_IPV4: - family = AF_INET; - break; - case BIO_FAMILY_IPANY: - family = AF_UNSPEC; - break; - default: - ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY); + ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY); goto exit_loop; } - if (BIO_lookup(c->param_hostname, c->param_service, - BIO_LOOKUP_CLIENT, - family, c->connect_sock_type, - &c->addr_first) == 0) - goto exit_loop; + break; + case BIO_FAMILY_IPV4: + family = AF_INET; + break; + case BIO_FAMILY_IPANY: + family = AF_UNSPEC; + break; + default: + ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY); + goto exit_loop; } + if (BIO_lookup(c->param_hostname, c->param_service, + BIO_LOOKUP_CLIENT, + family, c->connect_sock_type, + &c->addr_first) + == 0) + goto exit_loop; + } if (c->addr_first == NULL) { ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING); goto exit_loop; @@ -172,12 +172,12 @@ static int conn_state(BIO *b, BIO_CONNECT *c) case BIO_CONN_S_CREATE_SOCKET: ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter), - BIO_ADDRINFO_socktype(c->addr_iter), - BIO_ADDRINFO_protocol(c->addr_iter), 0); + BIO_ADDRINFO_socktype(c->addr_iter), + BIO_ADDRINFO_protocol(c->addr_iter), 0); if (ret == (int)INVALID_SOCKET) { ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), - "calling socket(%s, %s)", - c->param_hostname, c->param_service); + "calling socket(%s, %s)", + c->param_hostname, c->param_service); ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET); goto exit_loop; } @@ -202,7 +202,7 @@ static int conn_state(BIO *b, BIO_CONNECT *c) b->retry_reason = BIO_RR_CONNECT; ERR_pop_to_mark(); } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) - != NULL) { + != NULL) { /* * if there are more addresses to try, do that first */ @@ -213,8 +213,8 @@ static int conn_state(BIO *b, BIO_CONNECT *c) } else { ERR_clear_last_mark(); ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), - "calling connect(%s, %s)", - c->param_hostname, c->param_service); + "calling connect(%s, %s)", + c->param_hostname, c->param_service); c->state = BIO_CONN_S_CONNECT_ERROR; break; } @@ -243,8 +243,8 @@ static int conn_state(BIO *b, BIO_CONNECT *c) break; } ERR_raise_data(ERR_LIB_SYS, i, - "calling connect(%s, %s)", - c->param_hostname, c->param_service); + "calling connect(%s, %s)", + c->param_hostname, c->param_service); ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR); ret = 0; goto exit_loop; @@ -252,15 +252,6 @@ static int conn_state(BIO *b, BIO_CONNECT *c) if (!conn_create_dgram_bio(b, c)) break; c->state = BIO_CONN_S_OK; -# ifndef OPENSSL_NO_KTLS - /* - * The new socket is created successfully regardless of ktls_enable. - * ktls_enable doesn't change any functionality of the socket, except - * changing the setsockopt to enable the processing of ktls_start. - * Thus, it is not a problem to call it for non-TLS sockets. - */ - ktls_enable(b->num); -# endif } break; @@ -284,10 +275,10 @@ static int conn_state(BIO *b, BIO_CONNECT *c) } /* Loop does not exit */ - exit_loop: +exit_loop: if (cb != NULL) ret = cb((BIO *)b, c->state, ret); - end: +end: return ret; } @@ -384,11 +375,11 @@ static int conn_read(BIO *b, char *out, int outl) if (out != NULL) { clear_socket_error(); -# ifndef OPENSSL_NO_KTLS +#ifndef OPENSSL_NO_KTLS if (BIO_get_ktls_recv(b)) ret = ktls_read_record(b->num, out, outl); else -# endif +#endif ret = readsocket(b->num, out, outl); BIO_clear_retry_flags(b); if (ret <= 0) { @@ -421,7 +412,7 @@ static int conn_write(BIO *b, const char *in, int inl) } clear_socket_error(); -# ifndef OPENSSL_NO_KTLS +#ifndef OPENSSL_NO_KTLS if (BIO_should_ktls_ctrl_msg_flag(b)) { ret = ktls_send_ctrl_message(b->num, data->record_type, in, inl); if (ret >= 0) { @@ -429,16 +420,16 @@ static int conn_write(BIO *b, const char *in, int inl) BIO_clear_ktls_ctrl_msg_flag(b); } } else -# endif -# if defined(OSSL_TFO_SENDTO) - if (data->tfo_first) { +#endif +#if defined(OSSL_TFO_SENDTO) + if (data->tfo_first) { int peerlen = BIO_ADDRINFO_sockaddr_size(data->addr_iter); ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO, - BIO_ADDRINFO_sockaddr(data->addr_iter), peerlen); + BIO_ADDRINFO_sockaddr(data->addr_iter), peerlen); data->tfo_first = 0; } else -# endif +#endif ret = writesocket(b->num, in, inl); BIO_clear_retry_flags(b); if (ret <= 0) { @@ -456,9 +447,9 @@ static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) long ret = 1; BIO_CONNECT *data; const BIO_ADDR *dg_addr; -# ifndef OPENSSL_NO_KTLS +#ifndef OPENSSL_NO_KTLS ktls_crypto_info_t *crypto_info; -# endif +#endif data = (BIO_CONNECT *)b->ptr; @@ -469,6 +460,7 @@ static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) conn_close_socket(b); BIO_ADDRINFO_free(data->addr_first); data->addr_first = NULL; + data->addr_iter = NULL; b->flags = 0; break; case BIO_C_DO_STATE_MACHINE: @@ -489,11 +481,11 @@ static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter); } else if (num == 3) { switch (BIO_ADDRINFO_family(data->addr_iter)) { -# if OPENSSL_USE_IPV6 +#if OPENSSL_USE_IPV6 case AF_INET6: ret = BIO_FAMILY_IPV6; break; -# endif +#endif case AF_INET: ret = BIO_FAMILY_IPV4; break; @@ -526,9 +518,9 @@ static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) OPENSSL_free(data->param_hostname); data->param_hostname = NULL; ret = BIO_parse_hostserv(ptr, - &data->param_hostname, - &data->param_service, - BIO_PARSE_PRIO_HOST); + &data->param_hostname, + &data->param_service, + BIO_PARSE_PRIO_HOST); if (hold_service != data->param_service) OPENSSL_free(hold_service); } else if (num == 1) { /* BIO_set_conn_port */ @@ -602,21 +594,19 @@ static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) break; case BIO_CTRL_GET_RPOLL_DESCRIPTOR: - case BIO_CTRL_GET_WPOLL_DESCRIPTOR: - { - BIO_POLL_DESCRIPTOR *pd = ptr; + case BIO_CTRL_GET_WPOLL_DESCRIPTOR: { + BIO_POLL_DESCRIPTOR *pd = ptr; - if (data->state != BIO_CONN_S_OK) - conn_state(b, data); /* best effort */ + if (data->state != BIO_CONN_S_OK) + conn_state(b, data); /* best effort */ - if (data->state >= BIO_CONN_S_CREATE_SOCKET) { - pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD; - pd->value.fd = b->num; - } else { - ret = 0; - } + if (data->state >= BIO_CONN_S_CREATE_SOCKET) { + pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD; + pd->value.fd = b->num; + } else { + ret = 0; } - break; + } break; case BIO_C_SET_NBIO: if (num != 0) data->connect_mode |= BIO_SOCK_NONBLOCK; @@ -666,37 +656,33 @@ static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) break; case BIO_CTRL_FLUSH: break; - case BIO_CTRL_DUP: - { - dbio = (BIO *)ptr; - if (data->param_hostname) - BIO_set_conn_hostname(dbio, data->param_hostname); - if (data->param_service) - BIO_set_conn_port(dbio, data->param_service); - BIO_set_conn_ip_family(dbio, data->connect_family); - BIO_set_conn_mode(dbio, data->connect_mode); - /* - * FIXME: the cast of the function seems unlikely to be a good - * idea - */ - (void)BIO_set_info_callback(dbio, data->info_callback); - } - break; + case BIO_CTRL_DUP: { + dbio = (BIO *)ptr; + if (data->param_hostname) + BIO_set_conn_hostname(dbio, data->param_hostname); + if (data->param_service) + BIO_set_conn_port(dbio, data->param_service); + BIO_set_conn_ip_family(dbio, data->connect_family); + BIO_set_conn_mode(dbio, data->connect_mode); + /* + * FIXME: the cast of the function seems unlikely to be a good + * idea + */ + (void)BIO_set_info_callback(dbio, data->info_callback); + } break; case BIO_CTRL_SET_CALLBACK: ret = 0; /* use callback ctrl */ break; - case BIO_CTRL_GET_CALLBACK: - { - BIO_info_cb **fptr; + case BIO_CTRL_GET_CALLBACK: { + BIO_info_cb **fptr; - fptr = (BIO_info_cb **)ptr; - *fptr = data->info_callback; - } - break; + fptr = (BIO_info_cb **)ptr; + *fptr = data->info_callback; + } break; case BIO_CTRL_EOF: ret = (b->flags & BIO_FLAGS_IN_EOF) != 0; break; -# ifndef OPENSSL_NO_KTLS +#ifndef OPENSSL_NO_KTLS case BIO_CTRL_SET_KTLS: crypto_info = (ktls_crypto_info_t *)ptr; ret = ktls_start(b->num, crypto_info, num); @@ -721,7 +707,7 @@ static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) if (ret) BIO_set_ktls_zerocopy_sendfile_flag(b); break; -# endif +#endif default: ret = 0; break; @@ -737,11 +723,9 @@ static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) data = (BIO_CONNECT *)b->ptr; switch (cmd) { - case BIO_CTRL_SET_CALLBACK: - { - data->info_callback = fp; - } - break; + case BIO_CTRL_SET_CALLBACK: { + data->info_callback = fp; + } break; default: ret = 0; break; @@ -792,11 +776,11 @@ int conn_gets(BIO *bio, char *buf, int size) clear_socket_error(); while (size-- > 1) { -# ifndef OPENSSL_NO_KTLS +#ifndef OPENSSL_NO_KTLS if (BIO_get_ktls_recv(bio)) ret = ktls_read_record(bio->num, ptr, 1); else -# endif +#endif ret = readsocket(bio->num, ptr, 1); BIO_clear_retry_flags(bio); if (ret <= 0) { @@ -814,7 +798,7 @@ int conn_gets(BIO *bio, char *buf, int size) } static int conn_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs, - uint64_t flags, size_t *msgs_processed) + uint64_t flags, size_t *msgs_processed) { int ret; BIO_CONNECT *data; @@ -841,11 +825,11 @@ static int conn_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs, } return BIO_sendmmsg(data->dgram_bio, msg, stride, num_msgs, - flags, msgs_processed); + flags, msgs_processed); } static int conn_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs, - uint64_t flags, size_t *msgs_processed) + uint64_t flags, size_t *msgs_processed) { int ret; BIO_CONNECT *data; @@ -872,7 +856,7 @@ static int conn_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs, } return BIO_recvmmsg(data->dgram_bio, msg, stride, num_msgs, - flags, msgs_processed); + flags, msgs_processed); } BIO *BIO_new_connect(const char *str) |
