if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
{
clib_rwlock_writer_lock (&tm->half_open_rwlock);
- pool_get (tm->half_open_ctx_pool, ctx);
- ctx_index = ctx - tm->half_open_ctx_pool;
+ pool_get_zero (tm->half_open_ctx_pool, ctx);
+ ctx->c_c_index = ctx - tm->half_open_ctx_pool;
+ ctx_index = ctx->c_c_index;
clib_rwlock_writer_unlock (&tm->half_open_rwlock);
}
else
{
/* reader lock assumption: only main thread will call pool_get */
clib_rwlock_reader_lock (&tm->half_open_rwlock);
- pool_get (tm->half_open_ctx_pool, ctx);
- ctx_index = ctx - tm->half_open_ctx_pool;
+ pool_get_zero (tm->half_open_ctx_pool, ctx);
+ ctx->c_c_index = ctx - tm->half_open_ctx_pool;
+ ctx_index = ctx->c_c_index;
clib_rwlock_reader_unlock (&tm->half_open_rwlock);
}
- clib_memset (ctx, 0, sizeof (*ctx));
return ctx_index;
}
}
static inline crypto_engine_type_t
-tls_get_engine_type (crypto_engine_type_t preferred)
+tls_get_engine_type (crypto_engine_type_t requested,
+ crypto_engine_type_t preferred)
{
+ if (requested != CRYPTO_ENGINE_NONE)
+ {
+ if (tls_vfts[requested].ctx_alloc)
+ return requested;
+ return CRYPTO_ENGINE_NONE;
+ }
if (!tls_vfts[preferred].ctx_alloc)
return tls_get_available_engine ();
return preferred;
}
}
+static void
+tls_session_cleanup_ho (session_t *s)
+{
+ tls_ctx_t *ctx;
+ u32 ho_index;
+
+ /* session opaque stores the opaque passed on connect */
+ ho_index = s->opaque;
+ ctx = tls_ctx_half_open_get (ho_index);
+ session_half_open_delete_notify (&ctx->connection);
+ tls_ctx_half_open_reader_unlock ();
+ tls_ctx_half_open_free (ho_index);
+}
+
int
tls_add_segment_callback (u32 client_index, u64 segment_handle)
{
app_worker_connect_notify (app_wrk, 0, err, api_context);
}
tls_ctx_half_open_reader_unlock ();
- tls_ctx_half_open_free (ho_ctx_index);
return rv;
}
ctx_handle = tls_ctx_alloc (ho_ctx->tls_ctx_engine);
ctx = tls_ctx_get (ctx_handle);
clib_memcpy_fast (ctx, ho_ctx, sizeof (*ctx));
+ /* Half-open freed on tcp half-open cleanup notification */
tls_ctx_half_open_reader_unlock ();
- tls_ctx_half_open_free (ho_ctx_index);
ctx->c_thread_index = vlib_get_thread_index ();
ctx->tls_ctx_handle = ctx_handle;
}
static void
-dtls_migrate_udp (void *arg)
+dtls_migrate_ctx (void *arg)
{
tls_ctx_t *ctx = (tls_ctx_t *) arg;
u32 ctx_handle, thread_index;
dtls_session_migrate_callback (session_t *us, session_handle_t new_sh)
{
u32 new_thread = session_thread_from_handle (new_sh);
- tls_ctx_t *ctx;
+ tls_ctx_t *ctx, *cloned_ctx;
/* Migrate dtls context to new thread */
ctx = tls_ctx_get_w_thread (us->opaque, us->thread_index);
ctx->tls_session_handle = new_sh;
- ctx = tls_ctx_detach (ctx);
+ cloned_ctx = tls_ctx_detach (ctx);
ctx->is_migrated = 1;
- session_send_rpc_evt_to_thread (new_thread, dtls_migrate_udp, (void *) ctx);
+ session_send_rpc_evt_to_thread (new_thread, dtls_migrate_ctx,
+ (void *) cloned_ctx);
+
+ tls_ctx_free (ctx);
}
static session_cb_vft_t tls_app_cb_vft = {
.session_disconnect_callback = tls_session_disconnect_callback,
.session_connected_callback = tls_session_connected_callback,
.session_reset_callback = tls_session_reset_callback,
+ .half_open_cleanup_callback = tls_session_cleanup_ho,
.add_segment_callback = tls_add_segment_callback,
.del_segment_callback = tls_del_segment_callback,
.builtin_app_rx_callback = tls_app_rx_callback,
tls_connect (transport_endpoint_cfg_t * tep)
{
vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
+ transport_endpt_crypto_cfg_t *ccfg;
crypto_engine_type_t engine_type;
session_endpoint_cfg_t *sep;
tls_main_t *tm = &tls_main;
int rv;
sep = (session_endpoint_cfg_t *) tep;
+ if (!sep->ext_cfg)
+ return SESSION_E_NOEXTCFG;
+
app_wrk = app_worker_get (sep->app_wrk_index);
app = application_get (app_wrk->app_index);
- engine_type = tls_get_engine_type (app->tls_engine);
+
+ ccfg = &sep->ext_cfg->crypto;
+ engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
if (engine_type == CRYPTO_ENGINE_NONE)
{
clib_warning ("No tls engine_type available");
- return -1;
+ return SESSION_E_NOCRYPTOENG;
}
ctx_index = tls_ctx_half_open_alloc ();
ctx->parent_app_wrk_index = sep->app_wrk_index;
ctx->parent_app_api_context = sep->opaque;
ctx->tcp_is_ip4 = sep->is_ip4;
- ctx->ckpair_index = sep->ckpair_index;
ctx->tls_type = sep->transport_proto;
- if (sep->hostname)
+ ctx->ckpair_index = ccfg->ckpair_index;
+ ctx->c_proto = TRANSPORT_PROTO_TLS;
+ ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
+ if (ccfg->hostname[0])
{
- ctx->srv_hostname = format (0, "%v", sep->hostname);
+ ctx->srv_hostname = format (0, "%s", ccfg->hostname);
vec_terminate_c_string (ctx->srv_hostname);
}
tls_ctx_half_open_reader_unlock ();
- app_worker_alloc_connects_segment_manager (app_wrk);
ctx->tls_ctx_engine = engine_type;
clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));
if ((rv = vnet_connect (cargs)))
return rv;
+ /* Track half-open tcp session in case we need to clean it up */
+ ctx->tls_session_handle = cargs->sh;
+
TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type);
- return 0;
+ return ctx_index;
}
void
tls_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
{
vnet_listen_args_t _bargs, *args = &_bargs;
+ transport_endpt_crypto_cfg_t *ccfg;
app_worker_t *app_wrk;
tls_main_t *tm = &tls_main;
session_handle_t tls_al_handle;
app_listener_t *al;
tls_ctx_t *lctx;
u32 lctx_index;
+ int rv;
sep = (session_endpoint_cfg_t *) tep;
+ if (!sep->ext_cfg)
+ return SESSION_E_NOEXTCFG;
+
app_wrk = app_worker_get (sep->app_wrk_index);
app = application_get (app_wrk->app_index);
- engine_type = tls_get_engine_type (app->tls_engine);
+
+ ccfg = &sep->ext_cfg->crypto;
+ engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
if (engine_type == CRYPTO_ENGINE_NONE)
{
clib_warning ("No tls engine_type available");
- return -1;
+ return SESSION_E_NOCRYPTOENG;
}
clib_memset (args, 0, sizeof (*args));
args->sep_ext.transport_proto = TRANSPORT_PROTO_UDP;
args->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
}
- if (vnet_listen (args))
- return -1;
+ if ((rv = vnet_listen (args)))
+ return rv;
lctx_index = tls_listener_ctx_alloc ();
tls_al_handle = args->handle;
lctx->app_session_handle = listen_session_get_handle (app_listener);
lctx->tcp_is_ip4 = sep->is_ip4;
lctx->tls_ctx_engine = engine_type;
- lctx->ckpair_index = sep->ckpair_index;
lctx->tls_type = sep->transport_proto;
+ lctx->ckpair_index = ccfg->ckpair_index;
if (tls_vfts[engine_type].ctx_start_listen (lctx))
{
return &ctx->connection;
}
+static transport_connection_t *
+tls_half_open_get (u32 ho_index)
+{
+ tls_main_t *tm = &tls_main;
+ tls_ctx_t *ctx;
+ ctx = tls_ctx_half_open_get (ho_index);
+ clib_rwlock_reader_unlock (&tm->half_open_rwlock);
+ return &ctx->connection;
+}
+
+static void
+tls_cleanup_ho (u32 ho_index)
+{
+ tls_main_t *tm = &tls_main;
+ tls_ctx_t *ctx;
+ ctx = tls_ctx_half_open_get (ho_index);
+ clib_rwlock_reader_unlock (&tm->half_open_rwlock);
+ session_cleanup_half_open (ctx->tls_session_handle);
+}
+
int
tls_custom_tx_callback (void *session, transport_send_params_t * sp)
{
u8 *
format_tls_half_open (u8 * s, va_list * args)
{
- u32 tc_index = va_arg (*args, u32);
+ u32 ho_index = va_arg (*args, u32);
u32 __clib_unused thread_index = va_arg (*args, u32);
- tls_ctx_t *ctx = tls_ctx_half_open_get (tc_index);
- s = format (s, "[TLS] half-open app %u", ctx->parent_app_wrk_index);
+ session_t *tcp_ho;
+ tls_ctx_t *ho_ctx;
+
+ ho_ctx = tls_ctx_half_open_get (ho_index);
+
+ tcp_ho = session_get_from_handle (ho_ctx->tls_session_handle);
+ s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u ts %d:%d",
+ ho_ctx->c_thread_index, ho_ctx->c_s_index, "TLS",
+ ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
+ tcp_ho->thread_index, tcp_ho->session_index);
+
tls_ctx_half_open_reader_unlock ();
return s;
}
.stop_listen = tls_stop_listen,
.get_connection = tls_connection_get,
.get_listener = tls_listener_get,
+ .get_half_open = tls_half_open_get,
+ .cleanup_ho = tls_cleanup_ho,
.custom_tx = tls_custom_tx_callback,
.format_connection = format_tls_connection,
.format_half_open = format_tls_half_open,
.name = "tls",
.short_name = "J",
.tx_type = TRANSPORT_TX_INTERNAL,
- .service_type = TRANSPORT_SERVICE_APP,
+ .service_type = TRANSPORT_SERVICE_VC,
},
};
dtls_connect (transport_endpoint_cfg_t *tep)
{
vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
+ transport_endpt_crypto_cfg_t *ccfg;
crypto_engine_type_t engine_type;
session_endpoint_cfg_t *sep;
tls_main_t *tm = &tls_main;
int rv;
sep = (session_endpoint_cfg_t *) tep;
+ if (!sep->ext_cfg)
+ return -1;
+
app_wrk = app_worker_get (sep->app_wrk_index);
app = application_get (app_wrk->app_index);
- engine_type = tls_get_engine_type (app->tls_engine);
+
+ ccfg = &sep->ext_cfg->crypto;
+ engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
if (engine_type == CRYPTO_ENGINE_NONE)
{
clib_warning ("No tls engine_type available");
ctx->parent_app_wrk_index = sep->app_wrk_index;
ctx->parent_app_api_context = sep->opaque;
ctx->tcp_is_ip4 = sep->is_ip4;
- ctx->ckpair_index = sep->ckpair_index;
+ ctx->ckpair_index = ccfg->ckpair_index;
ctx->tls_type = sep->transport_proto;
ctx->tls_ctx_handle = ctx_handle;
- if (sep->hostname)
+ if (ccfg->hostname[0])
{
- ctx->srv_hostname = format (0, "%v", sep->hostname);
+ ctx->srv_hostname = format (0, "%s", ccfg->hostname);
vec_terminate_c_string (ctx->srv_hostname);
}
- app_worker_alloc_connects_segment_manager (app_wrk);
ctx->tls_ctx_engine = engine_type;
clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));