blob: b9ff30ba6a6a8b5ebb038b7a65a868b1da31c48f [file] [log] [blame]
Florin Coras371ca502018-02-21 12:07:41 -08001/*
Florin Corasc9940fc2019-02-05 20:55:11 -08002 * Copyright (c) 2018-2019 Cisco and/or its affiliates.
Florin Coras371ca502018-02-21 12:07:41 -08003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/session/application_interface.h>
17#include <vppinfra/lock.h>
Florin Corasd77eee62018-03-07 08:49:27 -080018#include <vnet/tls/tls.h>
Florin Coras3fe610a2024-05-31 12:04:41 -070019#include <vnet/tls/tls_inlines.h>
Florin Coras371ca502018-02-21 12:07:41 -080020
21static tls_main_t tls_main;
Florin Coras3fe610a2024-05-31 12:04:41 -070022tls_engine_vft_t *tls_vfts;
Florin Coras58d36f02018-03-09 13:05:53 -080023
24void tls_disconnect (u32 ctx_handle, u32 thread_index);
25
Florin Coras06a6a302019-04-17 14:19:12 -070026void
Florin Corasc01d5782018-10-17 14:53:11 -070027tls_disconnect_transport (tls_ctx_t * ctx)
28{
29 vnet_disconnect_args_t a = {
30 .handle = ctx->tls_session_handle,
Florin Coras3a2cf842024-07-11 05:15:47 -070031 .app_index = ctx->ts_app_index,
Florin Corasc01d5782018-10-17 14:53:11 -070032 };
33
34 if (vnet_disconnect_session (&a))
35 clib_warning ("disconnect returned");
36}
37
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +020038crypto_engine_type_t
Florin Coras58d36f02018-03-09 13:05:53 -080039tls_get_available_engine (void)
40{
41 int i;
42 for (i = 0; i < vec_len (tls_vfts); i++)
43 {
44 if (tls_vfts[i].ctx_alloc)
45 return i;
46 }
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +020047 return CRYPTO_ENGINE_NONE;
Florin Coras58d36f02018-03-09 13:05:53 -080048}
Florin Coras371ca502018-02-21 12:07:41 -080049
Florin Coras3fe610a2024-05-31 12:04:41 -070050static crypto_engine_type_t
51tls_get_engine_type (crypto_engine_type_t requested,
52 crypto_engine_type_t preferred)
53{
54 if (requested != CRYPTO_ENGINE_NONE)
55 {
56 if (tls_vfts[requested].ctx_alloc)
57 return requested;
58 return CRYPTO_ENGINE_NONE;
59 }
60 if (!tls_vfts[preferred].ctx_alloc)
61 return tls_get_available_engine ();
62 return preferred;
63}
64
Florin Corasd77eee62018-03-07 08:49:27 -080065int
Florin Coras288eaab2019-02-03 15:26:14 -080066tls_add_vpp_q_rx_evt (session_t * s)
Florin Coras371ca502018-02-21 12:07:41 -080067{
Florin Coras288eaab2019-02-03 15:26:14 -080068 if (svm_fifo_set_event (s->rx_fifo))
Florin Corasfe97da32019-03-06 10:09:04 -080069 session_send_io_evt_to_thread (s->rx_fifo, SESSION_IO_EVT_RX);
Florin Corasef915342018-09-29 10:23:06 -070070 return 0;
71}
72
73int
Florin Coras288eaab2019-02-03 15:26:14 -080074tls_add_vpp_q_builtin_rx_evt (session_t * s)
Florin Corasef915342018-09-29 10:23:06 -070075{
Florin Coras0242d302022-12-22 15:03:44 -080076 session_enqueue_notify (s);
Florin Corasef915342018-09-29 10:23:06 -070077 return 0;
78}
79
80int
Florin Coras288eaab2019-02-03 15:26:14 -080081tls_add_vpp_q_tx_evt (session_t * s)
Florin Corasef915342018-09-29 10:23:06 -070082{
Florin Coras288eaab2019-02-03 15:26:14 -080083 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasfe97da32019-03-06 10:09:04 -080084 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
Florin Corasef915342018-09-29 10:23:06 -070085 return 0;
86}
87
Florin Coras371ca502018-02-21 12:07:41 -080088static inline int
Florin Coras0242d302022-12-22 15:03:44 -080089tls_add_app_q_evt (app_worker_t *app_wrk, session_t *app_session)
Florin Coras371ca502018-02-21 12:07:41 -080090{
Florin Coras0242d302022-12-22 15:03:44 -080091 app_worker_add_event (app_wrk, app_session, SESSION_IO_EVT_RX);
92 return 0;
Florin Coras371ca502018-02-21 12:07:41 -080093}
94
95u32
Florin Coras371ca502018-02-21 12:07:41 -080096tls_listener_ctx_alloc (void)
97{
98 tls_main_t *tm = &tls_main;
99 tls_ctx_t *ctx;
100
101 pool_get (tm->listener_ctx_pool, ctx);
Dave Barachb7b92992018-10-17 10:38:51 -0400102 clib_memset (ctx, 0, sizeof (*ctx));
Florin Coras371ca502018-02-21 12:07:41 -0800103 return ctx - tm->listener_ctx_pool;
104}
105
106void
Florin Corase3e2f072018-03-04 07:24:30 -0800107tls_listener_ctx_free (tls_ctx_t * ctx)
Florin Coras371ca502018-02-21 12:07:41 -0800108{
Florin Corasc01d5782018-10-17 14:53:11 -0700109 if (CLIB_DEBUG)
110 memset (ctx, 0xfb, sizeof (*ctx));
Florin Corase3e2f072018-03-04 07:24:30 -0800111 pool_put (tls_main.listener_ctx_pool, ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800112}
113
114tls_ctx_t *
115tls_listener_ctx_get (u32 ctx_index)
116{
117 return pool_elt_at_index (tls_main.listener_ctx_pool, ctx_index);
118}
119
120u32
121tls_listener_ctx_index (tls_ctx_t * ctx)
122{
123 return (ctx - tls_main.listener_ctx_pool);
124}
125
126u32
127tls_ctx_half_open_alloc (void)
128{
129 tls_main_t *tm = &tls_main;
Florin Coras371ca502018-02-21 12:07:41 -0800130 tls_ctx_t *ctx;
Florin Coras371ca502018-02-21 12:07:41 -0800131
Florin Corasa474bc82023-12-04 20:29:52 -0800132 if (vec_len (tm->postponed_ho_free))
133 tls_flush_postponed_ho_cleanups ();
134
Florin Coras009303d2022-03-29 17:49:37 -0700135 pool_get_aligned_safe (tm->half_open_ctx_pool, ctx, CLIB_CACHE_LINE_BYTES);
136
137 clib_memset (ctx, 0, sizeof (*ctx));
138 ctx->c_c_index = ctx - tm->half_open_ctx_pool;
Florin Coras309f7aa2022-03-18 08:33:08 -0700139 ctx->c_thread_index = transport_cl_thread ();
Florin Coras009303d2022-03-29 17:49:37 -0700140
141 return ctx->c_c_index;
Florin Coras371ca502018-02-21 12:07:41 -0800142}
143
144void
145tls_ctx_half_open_free (u32 ho_index)
146{
Florin Coras371ca502018-02-21 12:07:41 -0800147 pool_put_index (tls_main.half_open_ctx_pool, ho_index);
Florin Coras371ca502018-02-21 12:07:41 -0800148}
149
150tls_ctx_t *
151tls_ctx_half_open_get (u32 ctx_index)
152{
153 tls_main_t *tm = &tls_main;
Florin Coras371ca502018-02-21 12:07:41 -0800154 return pool_elt_at_index (tm->half_open_ctx_pool, ctx_index);
155}
156
157void
Florin Corasa474bc82023-12-04 20:29:52 -0800158tls_add_postponed_ho_cleanups (u32 ho_index)
159{
160 tls_main_t *tm = &tls_main;
161 vec_add1 (tm->postponed_ho_free, ho_index);
162}
163
164static void
165tls_ctx_ho_try_free (u32 ho_index)
166{
167 tls_ctx_t *ctx;
168
169 ctx = tls_ctx_half_open_get (ho_index);
170 /* Probably tcp connected just before tcp establish timeout and
171 * worker that owns established session has not yet received
172 * @ref tls_session_connected_cb */
173 if (!(ctx->flags & TLS_CONN_F_HO_DONE))
174 {
175 ctx->tls_session_handle = SESSION_INVALID_HANDLE;
176 tls_add_postponed_ho_cleanups (ho_index);
177 return;
178 }
Florin Coras4a98b932024-01-31 13:45:39 -0800179 if (!(ctx->flags & TLS_CONN_F_NO_APP_SESSION))
Florin Corasa474bc82023-12-04 20:29:52 -0800180 session_half_open_delete_notify (&ctx->connection);
181 tls_ctx_half_open_free (ho_index);
182}
183
184void
185tls_flush_postponed_ho_cleanups ()
186{
187 tls_main_t *tm = &tls_main;
188 u32 *ho_indexp, *tmp;
189
190 tmp = tm->postponed_ho_free;
191 tm->postponed_ho_free = tm->ho_free_list;
192 tm->ho_free_list = tmp;
193
194 vec_foreach (ho_indexp, tm->ho_free_list)
195 tls_ctx_ho_try_free (*ho_indexp);
196
197 vec_reset_length (tm->ho_free_list);
198}
199
200void
Florin Coras288eaab2019-02-03 15:26:14 -0800201tls_notify_app_enqueue (tls_ctx_t * ctx, session_t * app_session)
Florin Coras58d36f02018-03-09 13:05:53 -0800202{
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800203 app_worker_t *app_wrk;
204 app_wrk = app_worker_get_if_valid (app_session->app_wrk_index);
205 if (PREDICT_TRUE (app_wrk != 0))
206 tls_add_app_q_evt (app_wrk, app_session);
Florin Coras58d36f02018-03-09 13:05:53 -0800207}
208
209int
Florin Coras371ca502018-02-21 12:07:41 -0800210tls_notify_app_accept (tls_ctx_t * ctx)
211{
Florin Coras288eaab2019-02-03 15:26:14 -0800212 session_t *app_listener, *app_session;
Florin Coras15531972018-08-12 23:50:53 -0700213 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800214 tls_ctx_t *lctx;
215 int rv;
216
Florin Coras371ca502018-02-21 12:07:41 -0800217 lctx = tls_listener_ctx_get (ctx->listener_ctx_index);
Florin Corasa27a46e2019-02-18 13:02:28 -0800218 app_listener = listen_session_get_from_handle (lctx->app_session_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800219
Florin Corase5659022024-02-27 17:10:25 -0800220 app_session = session_alloc (ctx->c_thread_index);
221 app_session->session_state = SESSION_STATE_ACCEPTING;
Florin Coras371ca502018-02-21 12:07:41 -0800222 app_session->session_type = app_listener->session_type;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200223 app_session->listener_handle = listen_session_get_handle (app_listener);
Florin Corase5659022024-02-27 17:10:25 -0800224 app_session->app_wrk_index = ctx->parent_app_wrk_index;
225 app_session->connection_index = ctx->tls_ctx_handle;
226 ctx->c_s_index = app_session->session_index;
Florin Coras15531972018-08-12 23:50:53 -0700227
Florin Corasa27a46e2019-02-18 13:02:28 -0800228 if ((rv = app_worker_init_accepted (app_session)))
Florin Coras371ca502018-02-21 12:07:41 -0800229 {
230 TLS_DBG (1, "failed to allocate fifos");
Florin Corasa27a46e2019-02-18 13:02:28 -0800231 session_free (app_session);
Florin Coras4a98b932024-01-31 13:45:39 -0800232 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Coras371ca502018-02-21 12:07:41 -0800233 return rv;
234 }
Florin Coras371ca502018-02-21 12:07:41 -0800235 ctx->app_session_handle = session_handle (app_session);
Florin Corasdf57ea02019-02-18 20:14:20 -0800236 ctx->parent_app_wrk_index = app_session->app_wrk_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800237 app_wrk = app_worker_get (app_session->app_wrk_index);
238 return app_worker_accept_notify (app_wrk, app_session);
Florin Coras371ca502018-02-21 12:07:41 -0800239}
240
Florin Coras58d36f02018-03-09 13:05:53 -0800241int
Florin Coras00e01d32019-10-21 16:07:46 -0700242tls_notify_app_connected (tls_ctx_t * ctx, session_error_t err)
Florin Coras371ca502018-02-21 12:07:41 -0800243{
Florin Coras179711d2022-04-10 13:34:47 -0700244 u32 parent_app_api_ctx;
Florin Coras288eaab2019-02-03 15:26:14 -0800245 session_t *app_session;
Florin Coras15531972018-08-12 23:50:53 -0700246 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800247
Florin Corasdf57ea02019-02-18 20:14:20 -0800248 app_wrk = app_worker_get_if_valid (ctx->parent_app_wrk_index);
Florin Corasef915342018-09-29 10:23:06 -0700249 if (!app_wrk)
250 {
Florin Coras4a98b932024-01-31 13:45:39 -0800251 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Corasef915342018-09-29 10:23:06 -0700252 return -1;
253 }
254
Florin Coras00e01d32019-10-21 16:07:46 -0700255 if (err)
Florin Coras4b47ee22020-11-19 13:38:26 -0800256 {
Florin Coras4a98b932024-01-31 13:45:39 -0800257 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Corase3c6a542021-05-19 19:28:40 -0700258 goto send_reply;
Florin Coras4b47ee22020-11-19 13:38:26 -0800259 }
Florin Coras8f89dd02018-03-05 16:53:07 -0800260
Florin Corase5659022024-02-27 17:10:25 -0800261 app_session = session_alloc (ctx->c_thread_index);
262 app_session->session_state = SESSION_STATE_CREATED;
263 app_session->connection_index = ctx->tls_ctx_handle;
264
Florin Coras4b47ee22020-11-19 13:38:26 -0800265 if (ctx->tls_type == TRANSPORT_PROTO_DTLS)
266 {
Florin Corasbab6c522021-05-13 08:36:56 -0700267 /* Cleanup half-open session as we don't get notification from udp */
268 session_half_open_delete_notify (&ctx->connection);
Florin Corase5659022024-02-27 17:10:25 -0800269 app_session->session_type =
Florin Coras4b47ee22020-11-19 13:38:26 -0800270 session_type_from_proto_and_ip (TRANSPORT_PROTO_DTLS, ctx->tcp_is_ip4);
Florin Coras4b47ee22020-11-19 13:38:26 -0800271 }
272 else
273 {
Florin Corase5659022024-02-27 17:10:25 -0800274 app_session->session_type =
275 session_type_from_proto_and_ip (TRANSPORT_PROTO_TLS, ctx->tcp_is_ip4);
Florin Coras4b47ee22020-11-19 13:38:26 -0800276 }
277
Florin Corasdf57ea02019-02-18 20:14:20 -0800278 app_session->app_wrk_index = ctx->parent_app_wrk_index;
Florin Coras2ceb8182023-08-31 17:33:47 -0700279 app_session->opaque = ctx->parent_app_api_context;
Florin Corase5659022024-02-27 17:10:25 -0800280 ctx->c_s_index = app_session->session_index;
Florin Coras15531972018-08-12 23:50:53 -0700281
Florin Coras00e01d32019-10-21 16:07:46 -0700282 if ((err = app_worker_init_connected (app_wrk, app_session)))
Florin Corasa3d55df2023-10-06 19:06:35 -0700283 {
284 app_worker_connect_notify (app_wrk, 0, err, ctx->parent_app_api_context);
Florin Coras4a98b932024-01-31 13:45:39 -0800285 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Corasa3d55df2023-10-06 19:06:35 -0700286 session_free (app_session);
287 return -1;
288 }
Florin Coras371ca502018-02-21 12:07:41 -0800289
jiangxiaoming5b7ea912020-09-23 10:29:21 +0800290 app_session->session_state = SESSION_STATE_READY;
Florin Coras179711d2022-04-10 13:34:47 -0700291 parent_app_api_ctx = ctx->parent_app_api_context;
292 ctx->app_session_handle = session_handle (app_session);
293
294 if (app_worker_connect_notify (app_wrk, app_session, SESSION_E_NONE,
295 parent_app_api_ctx))
Florin Coras371ca502018-02-21 12:07:41 -0800296 {
297 TLS_DBG (1, "failed to notify app");
Florin Coras6e7ebb02022-05-04 12:02:56 -0700298 session_free (session_get (ctx->c_s_index, ctx->c_thread_index));
Florin Coras4a98b932024-01-31 13:45:39 -0800299 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Coraseb97e5f2018-10-15 21:35:42 -0700300 return -1;
Florin Coras371ca502018-02-21 12:07:41 -0800301 }
302
303 return 0;
304
Florin Corase3c6a542021-05-19 19:28:40 -0700305send_reply:
Florin Coras00e01d32019-10-21 16:07:46 -0700306 return app_worker_connect_notify (app_wrk, 0, err,
307 ctx->parent_app_api_context);
Florin Coras371ca502018-02-21 12:07:41 -0800308}
309
Florin Coras371ca502018-02-21 12:07:41 -0800310void
Florin Coras8c5e5f62022-02-24 16:35:26 -0800311tls_notify_app_io_error (tls_ctx_t *ctx)
312{
Florin Corasd0e8bd72023-10-17 11:59:49 -0700313 ASSERT (ctx->flags & TLS_CONN_F_HS_DONE);
Florin Coras8c5e5f62022-02-24 16:35:26 -0800314
315 session_transport_reset_notify (&ctx->connection);
316 session_transport_closed_notify (&ctx->connection);
317 tls_disconnect_transport (ctx);
318}
319
320void
Florin Corasea158d62024-02-26 18:11:43 -0800321tls_session_reset_callback (session_t *ts)
Florin Coras371ca502018-02-21 12:07:41 -0800322{
Florin Corasf03c4942019-08-07 21:39:27 -0700323 tls_ctx_t *ctx;
324
Florin Corasea158d62024-02-26 18:11:43 -0800325 ctx = tls_ctx_get_w_thread (ts->opaque, ts->thread_index);
Florin Coras4a98b932024-01-31 13:45:39 -0800326 ctx->flags |= TLS_CONN_F_PASSIVE_CLOSE;
Florin Corasea158d62024-02-26 18:11:43 -0800327 tls_ctx_transport_reset (ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800328}
329
Florin Coras2c876f92021-05-10 21:12:27 -0700330static void
331tls_session_cleanup_ho (session_t *s)
332{
Florin Coras2c876f92021-05-10 21:12:27 -0700333 /* session opaque stores the opaque passed on connect */
Florin Corasa474bc82023-12-04 20:29:52 -0800334 tls_ctx_ho_try_free (s->opaque);
Florin Coras2c876f92021-05-10 21:12:27 -0700335}
336
Florin Coras371ca502018-02-21 12:07:41 -0800337int
Florin Corasfa76a762018-11-29 12:40:10 -0800338tls_add_segment_callback (u32 client_index, u64 segment_handle)
Florin Coras371ca502018-02-21 12:07:41 -0800339{
340 /* No-op for builtin */
341 return 0;
342}
343
344int
Florin Corasfa76a762018-11-29 12:40:10 -0800345tls_del_segment_callback (u32 client_index, u64 segment_handle)
Florin Coras371ca502018-02-21 12:07:41 -0800346{
347 return 0;
348}
349
350void
Florin Coras288eaab2019-02-03 15:26:14 -0800351tls_session_disconnect_callback (session_t * tls_session)
Florin Coras371ca502018-02-21 12:07:41 -0800352{
Florin Coras371ca502018-02-21 12:07:41 -0800353 tls_ctx_t *ctx;
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800354
355 TLS_DBG (1, "TCP disconnecting handle %x session %u", tls_session->opaque,
356 tls_session->session_index);
Florin Coras371ca502018-02-21 12:07:41 -0800357
Florin Coras6faac162019-10-11 08:00:43 -0700358 ASSERT (tls_session->thread_index == vlib_get_thread_index ()
359 || vlib_thread_is_main_w_barrier ());
360
361 ctx = tls_ctx_get_w_thread (tls_session->opaque, tls_session->thread_index);
Florin Coras4a98b932024-01-31 13:45:39 -0800362 ctx->flags |= TLS_CONN_F_PASSIVE_CLOSE;
Florin Coras06a6a302019-04-17 14:19:12 -0700363 tls_ctx_transport_close (ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800364}
365
366int
Florin Coras7c2a3352024-03-26 19:47:27 -0700367tls_session_accept_callback (session_t *ts)
Florin Coras371ca502018-02-21 12:07:41 -0800368{
Florin Corase5659022024-02-27 17:10:25 -0800369 session_t *tls_listener;
Florin Coras371ca502018-02-21 12:07:41 -0800370 tls_ctx_t *lctx, *ctx;
Florin Coras58d36f02018-03-09 13:05:53 -0800371 u32 ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800372
Florin Coras7c2a3352024-03-26 19:47:27 -0700373 tls_listener = listen_session_get_from_handle (ts->listener_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800374 lctx = tls_listener_ctx_get (tls_listener->opaque);
Florin Coras58d36f02018-03-09 13:05:53 -0800375
376 ctx_handle = tls_ctx_alloc (lctx->tls_ctx_engine);
377 ctx = tls_ctx_get (ctx_handle);
Florin Coras60732df2024-02-23 18:01:45 -0800378 clib_memcpy (ctx, lctx, sizeof (*lctx));
Florin Coras7c2a3352024-03-26 19:47:27 -0700379 ctx->c_s_index = SESSION_INVALID_INDEX;
380 ctx->c_thread_index = ts->thread_index;
Florin Coras58d36f02018-03-09 13:05:53 -0800381 ctx->tls_ctx_handle = ctx_handle;
Florin Coras7c2a3352024-03-26 19:47:27 -0700382 ts->opaque = ctx_handle;
383 ctx->tls_session_handle = session_handle (ts);
Florin Coras371ca502018-02-21 12:07:41 -0800384 ctx->listener_ctx_index = tls_listener->opaque;
Florin Corasd09236d2019-08-08 17:38:26 -0700385 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200386 ctx->ckpair_index = lctx->ckpair_index;
Florin Coras371ca502018-02-21 12:07:41 -0800387
Florin Coras58d36f02018-03-09 13:05:53 -0800388 TLS_DBG (1, "Accept on listener %u new connection [%u]%x",
389 tls_listener->opaque, vlib_get_thread_index (), ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800390
Florin Coras14bfd3d2023-11-08 19:57:38 -0800391 if (tls_ctx_init_server (ctx))
Xiaoming Jiang47781642023-05-26 00:32:25 +0000392 {
Florin Coras14bfd3d2023-11-08 19:57:38 -0800393 /* Do not free ctx yet, in case we have pending rx events */
Florin Coras4a98b932024-01-31 13:45:39 -0800394 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Coras14bfd3d2023-11-08 19:57:38 -0800395 tls_disconnect_transport (ctx);
Xiaoming Jiang47781642023-05-26 00:32:25 +0000396 }
397
Florin Coras0bfce6b2024-04-05 14:50:10 -0700398 if (ts->session_state < SESSION_STATE_READY)
399 ts->session_state = SESSION_STATE_READY;
400
Florin Coras14bfd3d2023-11-08 19:57:38 -0800401 return 0;
Florin Coras371ca502018-02-21 12:07:41 -0800402}
403
404int
Florin Corascfc2a0e2023-11-08 14:14:17 -0800405tls_app_rx_callback (session_t *ts)
Florin Coras371ca502018-02-21 12:07:41 -0800406{
Florin Coras371ca502018-02-21 12:07:41 -0800407 tls_ctx_t *ctx;
Florin Coras371ca502018-02-21 12:07:41 -0800408
Florin Coras4b47ee22020-11-19 13:38:26 -0800409 /* DTLS session migrating, wait for next notification */
Florin Corascfc2a0e2023-11-08 14:14:17 -0800410 if (PREDICT_FALSE (ts->flags & SESSION_F_IS_MIGRATING))
Florin Coras4b47ee22020-11-19 13:38:26 -0800411 return 0;
412
Florin Corascfc2a0e2023-11-08 14:14:17 -0800413 /* Read rescheduled but underlying transport deleted now */
414 if (PREDICT_FALSE ((ts->session_state == SESSION_STATE_TRANSPORT_DELETED)))
415 return 0;
416
417 ctx = tls_ctx_get (ts->opaque);
Florin Coras4a98b932024-01-31 13:45:39 -0800418 if (PREDICT_FALSE ((ctx->flags & TLS_CONN_F_NO_APP_SESSION) ||
419 (ctx->flags & TLS_CONN_F_APP_CLOSED)))
Saravanan Murugesan3683d1b2022-02-28 10:55:26 +0530420 {
421 TLS_DBG (1, "Local App closed");
422 return 0;
423 }
Florin Corascfc2a0e2023-11-08 14:14:17 -0800424 tls_ctx_read (ctx, ts);
Florin Coras58d36f02018-03-09 13:05:53 -0800425 return 0;
Florin Coras371ca502018-02-21 12:07:41 -0800426}
427
428int
Florin Coras9f86d222020-03-23 15:34:22 +0000429tls_app_tx_callback (session_t * tls_session)
430{
431 tls_ctx_t *ctx;
432
433 ctx = tls_ctx_get (tls_session->opaque);
434 transport_connection_reschedule (&ctx->connection);
435
436 return 0;
437}
438
439int
Florin Coras4b47ee22020-11-19 13:38:26 -0800440tls_session_connected_cb (u32 tls_app_index, u32 ho_ctx_index,
441 session_t *tls_session, session_error_t err)
Florin Coras371ca502018-02-21 12:07:41 -0800442{
Florin Coras371ca502018-02-21 12:07:41 -0800443 tls_ctx_t *ho_ctx, *ctx;
Florin Coras58d36f02018-03-09 13:05:53 -0800444 u32 ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800445
446 ho_ctx = tls_ctx_half_open_get (ho_ctx_index);
Florin Coras371ca502018-02-21 12:07:41 -0800447
Florin Coras58d36f02018-03-09 13:05:53 -0800448 ctx_handle = tls_ctx_alloc (ho_ctx->tls_ctx_engine);
449 ctx = tls_ctx_get (ctx_handle);
Dave Barach178cf492018-11-13 16:34:13 -0500450 clib_memcpy_fast (ctx, ho_ctx, sizeof (*ctx));
Florin Coras0ded4892024-02-13 15:37:20 -0500451
Florin Coras2c876f92021-05-10 21:12:27 -0700452 /* Half-open freed on tcp half-open cleanup notification */
Florin Coras0ded4892024-02-13 15:37:20 -0500453 __atomic_fetch_or (&ho_ctx->flags, TLS_CONN_F_HO_DONE, __ATOMIC_RELEASE);
Florin Coras371ca502018-02-21 12:07:41 -0800454
Florin Corasdcde9272018-03-02 09:23:42 -0800455 ctx->c_thread_index = vlib_get_thread_index ();
Florin Coras58d36f02018-03-09 13:05:53 -0800456 ctx->tls_ctx_handle = ctx_handle;
Florin Corasd09236d2019-08-08 17:38:26 -0700457 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasdcde9272018-03-02 09:23:42 -0800458
Florin Coras58d36f02018-03-09 13:05:53 -0800459 TLS_DBG (1, "TCP connect for %u returned %u. New connection [%u]%x",
Florin Coras4b47ee22020-11-19 13:38:26 -0800460 ho_ctx_index, err, vlib_get_thread_index (),
Florin Coras58d36f02018-03-09 13:05:53 -0800461 (ctx) ? ctx_handle : ~0);
Florin Coras371ca502018-02-21 12:07:41 -0800462
463 ctx->tls_session_handle = session_handle (tls_session);
Florin Coras58d36f02018-03-09 13:05:53 -0800464 tls_session->opaque = ctx_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800465
Florin Coras14bfd3d2023-11-08 19:57:38 -0800466 if (tls_ctx_init_client (ctx))
Xiaoming Jiang47781642023-05-26 00:32:25 +0000467 {
Florin Coras14bfd3d2023-11-08 19:57:38 -0800468 tls_notify_app_connected (ctx, SESSION_E_TLS_HANDSHAKE);
469 tls_disconnect_transport (ctx);
Xiaoming Jiang47781642023-05-26 00:32:25 +0000470 }
471
Florin Corasa8d266a2024-03-22 14:47:16 -0700472 if (tls_session->session_state < SESSION_STATE_READY)
473 tls_session->session_state = SESSION_STATE_READY;
474
Florin Coras14bfd3d2023-11-08 19:57:38 -0800475 return 0;
Florin Coras371ca502018-02-21 12:07:41 -0800476}
477
Florin Coras4b47ee22020-11-19 13:38:26 -0800478int
479dtls_session_connected_cb (u32 app_wrk_index, u32 ctx_handle, session_t *us,
480 session_error_t err)
481{
482 tls_ctx_t *ctx;
483
Florin Corasfb50bc32021-05-18 00:28:59 -0700484 ctx = tls_ctx_get_w_thread (ctx_handle, transport_cl_thread ());
Florin Coras4b47ee22020-11-19 13:38:26 -0800485
486 ctx->tls_session_handle = session_handle (us);
487 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
488 us->opaque = ctx_handle;
489
490 /* We don't preallocate the app session because the udp session might
491 * actually migrate to a different worker at the end of the handshake */
492
493 return tls_ctx_init_client (ctx);
494}
495
496int
497tls_session_connected_callback (u32 tls_app_index, u32 ho_ctx_index,
498 session_t *tls_session, session_error_t err)
499{
Florin Coras71ee3752021-05-28 18:29:08 -0700500 if (err)
501 {
502 app_worker_t *app_wrk;
503 tls_ctx_t *ho_ctx;
504 u32 api_context;
505
506 ho_ctx = tls_ctx_half_open_get (ho_ctx_index);
Florin Corasa474bc82023-12-04 20:29:52 -0800507 ho_ctx->flags |= TLS_CONN_F_HO_DONE;
Florin Coras71ee3752021-05-28 18:29:08 -0700508 app_wrk = app_worker_get_if_valid (ho_ctx->parent_app_wrk_index);
509 if (app_wrk)
510 {
511 api_context = ho_ctx->parent_app_api_context;
512 app_worker_connect_notify (app_wrk, 0, err, api_context);
513 }
Florin Coras71ee3752021-05-28 18:29:08 -0700514
515 return 0;
516 }
517
Florin Coras4b47ee22020-11-19 13:38:26 -0800518 if (session_get_transport_proto (tls_session) == TRANSPORT_PROTO_TCP)
519 return tls_session_connected_cb (tls_app_index, ho_ctx_index, tls_session,
520 err);
521 else
522 return dtls_session_connected_cb (tls_app_index, ho_ctx_index, tls_session,
523 err);
524}
525
Florin Corasef2b3352019-08-07 11:14:56 -0700526static void
527tls_app_session_cleanup (session_t * s, session_cleanup_ntf_t ntf)
528{
529 tls_ctx_t *ctx;
530
531 if (ntf == SESSION_CLEANUP_TRANSPORT)
Florin Coras36d49392020-04-24 23:00:11 +0000532 {
533 /* Allow cleanup of tcp session */
534 if (s->session_state == SESSION_STATE_TRANSPORT_DELETED)
535 session_close (s);
536 return;
537 }
Florin Corasef2b3352019-08-07 11:14:56 -0700538
539 ctx = tls_ctx_get (s->opaque);
Florin Coras4a98b932024-01-31 13:45:39 -0800540 if (!(ctx->flags & TLS_CONN_F_NO_APP_SESSION))
Florin Corasef2b3352019-08-07 11:14:56 -0700541 session_transport_delete_notify (&ctx->connection);
542 tls_ctx_free (ctx);
543}
544
Florin Coras4b47ee22020-11-19 13:38:26 -0800545static void
Florin Coras88dd3cf2021-05-01 19:01:42 -0700546dtls_migrate_ctx (void *arg)
Florin Coras4b47ee22020-11-19 13:38:26 -0800547{
548 tls_ctx_t *ctx = (tls_ctx_t *) arg;
549 u32 ctx_handle, thread_index;
550 session_t *us;
551
552 thread_index = session_thread_from_handle (ctx->tls_session_handle);
553 ASSERT (thread_index == vlib_get_thread_index ());
554
555 ctx_handle = tls_ctx_attach (ctx->tls_ctx_engine, thread_index, ctx);
556 ctx = tls_ctx_get_w_thread (ctx_handle, thread_index);
557 ctx->tls_ctx_handle = ctx_handle;
558
559 us = session_get_from_handle (ctx->tls_session_handle);
560 us->opaque = ctx_handle;
561 us->flags &= ~SESSION_F_IS_MIGRATING;
Florin Corasbab6c522021-05-13 08:36:56 -0700562
563 /* Probably the app detached while the session was migrating. Cleanup */
564 if (session_half_open_migrated_notify (&ctx->connection))
565 {
Florin Coras4a98b932024-01-31 13:45:39 -0800566 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Corasbab6c522021-05-13 08:36:56 -0700567 tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
568 return;
569 }
570
Florin Coras4b47ee22020-11-19 13:38:26 -0800571 if (svm_fifo_max_dequeue (us->tx_fifo))
572 session_send_io_evt_to_thread (us->tx_fifo, SESSION_IO_EVT_TX);
573}
574
575static void
576dtls_session_migrate_callback (session_t *us, session_handle_t new_sh)
577{
578 u32 new_thread = session_thread_from_handle (new_sh);
Florin Coras88dd3cf2021-05-01 19:01:42 -0700579 tls_ctx_t *ctx, *cloned_ctx;
Florin Coras4b47ee22020-11-19 13:38:26 -0800580
581 /* Migrate dtls context to new thread */
582 ctx = tls_ctx_get_w_thread (us->opaque, us->thread_index);
583 ctx->tls_session_handle = new_sh;
Florin Coras88dd3cf2021-05-01 19:01:42 -0700584 cloned_ctx = tls_ctx_detach (ctx);
Florin Coras4a98b932024-01-31 13:45:39 -0800585 ctx->flags |= TLS_CONN_F_MIGRATED;
Florin Corasbab6c522021-05-13 08:36:56 -0700586 session_half_open_migrate_notify (&ctx->connection);
Florin Coras4b47ee22020-11-19 13:38:26 -0800587
Florin Coras88dd3cf2021-05-01 19:01:42 -0700588 session_send_rpc_evt_to_thread (new_thread, dtls_migrate_ctx,
589 (void *) cloned_ctx);
590
591 tls_ctx_free (ctx);
Florin Coras4b47ee22020-11-19 13:38:26 -0800592}
593
Florin Corascfc2a0e2023-11-08 14:14:17 -0800594static void
595tls_session_transport_closed_callback (session_t *ts)
596{
597 tls_ctx_t *ctx;
598
599 ctx = tls_ctx_get_w_thread (ts->opaque, ts->thread_index);
Florin Coras4a98b932024-01-31 13:45:39 -0800600 if (!(ctx->flags & TLS_CONN_F_NO_APP_SESSION))
Florin Coras86b02c72023-12-13 14:26:19 -0800601 session_transport_closed_notify (&ctx->connection);
Florin Corascfc2a0e2023-11-08 14:14:17 -0800602}
603
Florin Coras371ca502018-02-21 12:07:41 -0800604static session_cb_vft_t tls_app_cb_vft = {
605 .session_accept_callback = tls_session_accept_callback,
606 .session_disconnect_callback = tls_session_disconnect_callback,
607 .session_connected_callback = tls_session_connected_callback,
608 .session_reset_callback = tls_session_reset_callback,
Florin Corascfc2a0e2023-11-08 14:14:17 -0800609 .session_transport_closed_callback = tls_session_transport_closed_callback,
Florin Coras2c876f92021-05-10 21:12:27 -0700610 .half_open_cleanup_callback = tls_session_cleanup_ho,
Florin Coras371ca502018-02-21 12:07:41 -0800611 .add_segment_callback = tls_add_segment_callback,
612 .del_segment_callback = tls_del_segment_callback,
613 .builtin_app_rx_callback = tls_app_rx_callback,
Florin Coras9f86d222020-03-23 15:34:22 +0000614 .builtin_app_tx_callback = tls_app_tx_callback,
Florin Coras4b47ee22020-11-19 13:38:26 -0800615 .session_migrate_callback = dtls_session_migrate_callback,
Florin Corasef2b3352019-08-07 11:14:56 -0700616 .session_cleanup_callback = tls_app_session_cleanup,
Florin Coras371ca502018-02-21 12:07:41 -0800617};
Florin Coras371ca502018-02-21 12:07:41 -0800618
619int
Florin Coras5665ced2018-10-25 18:03:45 -0700620tls_connect (transport_endpoint_cfg_t * tep)
Florin Coras371ca502018-02-21 12:07:41 -0800621{
Florin Coras15531972018-08-12 23:50:53 -0700622 vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
Florin Corasa54b62d2021-04-21 09:05:56 -0700623 transport_endpt_crypto_cfg_t *ccfg;
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200624 crypto_engine_type_t engine_type;
Florin Coras4b47ee22020-11-19 13:38:26 -0800625 session_endpoint_cfg_t *sep;
Florin Coras371ca502018-02-21 12:07:41 -0800626 tls_main_t *tm = &tls_main;
Florin Coras15531972018-08-12 23:50:53 -0700627 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800628 application_t *app;
629 tls_ctx_t *ctx;
630 u32 ctx_index;
Matus Fabian10c016c2024-10-16 15:29:36 +0200631 transport_endpt_ext_cfg_t *ext_cfg;
Florin Corasc1a42652019-02-08 18:27:29 -0800632 int rv;
Florin Coras371ca502018-02-21 12:07:41 -0800633
Florin Coras5665ced2018-10-25 18:03:45 -0700634 sep = (session_endpoint_cfg_t *) tep;
Matus Fabian10c016c2024-10-16 15:29:36 +0200635 ext_cfg = session_endpoint_get_ext_cfg (sep, TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
636 if (!ext_cfg)
Florin Corase5f18332021-04-22 11:48:35 -0700637 return SESSION_E_NOEXTCFG;
Florin Corasa54b62d2021-04-21 09:05:56 -0700638
Florin Coras15531972018-08-12 23:50:53 -0700639 app_wrk = app_worker_get (sep->app_wrk_index);
640 app = application_get (app_wrk->app_index);
Florin Corasa54b62d2021-04-21 09:05:56 -0700641
Matus Fabian10c016c2024-10-16 15:29:36 +0200642 ccfg = &ext_cfg->crypto;
Florin Corasa54b62d2021-04-21 09:05:56 -0700643 engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200644 if (engine_type == CRYPTO_ENGINE_NONE)
Florin Coras58d36f02018-03-09 13:05:53 -0800645 {
646 clib_warning ("No tls engine_type available");
Florin Corase5f18332021-04-22 11:48:35 -0700647 return SESSION_E_NOCRYPTOENG;
Florin Coras58d36f02018-03-09 13:05:53 -0800648 }
Florin Coras371ca502018-02-21 12:07:41 -0800649
650 ctx_index = tls_ctx_half_open_alloc ();
651 ctx = tls_ctx_half_open_get (ctx_index);
Florin Corasdf57ea02019-02-18 20:14:20 -0800652 ctx->parent_app_wrk_index = sep->app_wrk_index;
Florin Coras371ca502018-02-21 12:07:41 -0800653 ctx->parent_app_api_context = sep->opaque;
Florin Coras3a2cf842024-07-11 05:15:47 -0700654 ctx->ts_app_index = tm->app_index;
Florin Coras371ca502018-02-21 12:07:41 -0800655 ctx->tcp_is_ip4 = sep->is_ip4;
Florin Coras4b47ee22020-11-19 13:38:26 -0800656 ctx->tls_type = sep->transport_proto;
Florin Corasa54b62d2021-04-21 09:05:56 -0700657 ctx->ckpair_index = ccfg->ckpair_index;
Florin Coras2c876f92021-05-10 21:12:27 -0700658 ctx->c_proto = TRANSPORT_PROTO_TLS;
659 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasa54b62d2021-04-21 09:05:56 -0700660 if (ccfg->hostname[0])
Florin Coras8f89dd02018-03-05 16:53:07 -0800661 {
Florin Corasa54b62d2021-04-21 09:05:56 -0700662 ctx->srv_hostname = format (0, "%s", ccfg->hostname);
Florin Coras8f89dd02018-03-05 16:53:07 -0800663 vec_terminate_c_string (ctx->srv_hostname);
664 }
Florin Coras371ca502018-02-21 12:07:41 -0800665
Florin Coras58d36f02018-03-09 13:05:53 -0800666 ctx->tls_ctx_engine = engine_type;
Florin Coras371ca502018-02-21 12:07:41 -0800667
Dave Barach178cf492018-11-13 16:34:13 -0500668 clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));
Florin Coras15531972018-08-12 23:50:53 -0700669 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
670 cargs->app_index = tm->app_index;
671 cargs->api_context = ctx_index;
Florin Coras8a140612019-02-18 22:39:39 -0800672 cargs->sep_ext.ns_index = app->ns_index;
Florin Corasc1a42652019-02-08 18:27:29 -0800673 if ((rv = vnet_connect (cargs)))
Florin Coras3109d1c2024-01-22 18:27:43 -0800674 {
675 tls_ctx_half_open_free (ctx_index);
676 return rv;
677 }
Florin Coras371ca502018-02-21 12:07:41 -0800678
Florin Coras2c876f92021-05-10 21:12:27 -0700679 /* Track half-open tcp session in case we need to clean it up */
680 ctx->tls_session_handle = cargs->sh;
681
Florin Coras58d36f02018-03-09 13:05:53 -0800682 TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type);
Florin Coras2c876f92021-05-10 21:12:27 -0700683 return ctx_index;
Florin Coras371ca502018-02-21 12:07:41 -0800684}
685
686void
Florin Coras58d36f02018-03-09 13:05:53 -0800687tls_disconnect (u32 ctx_handle, u32 thread_index)
Florin Coras371ca502018-02-21 12:07:41 -0800688{
Florin Coras371ca502018-02-21 12:07:41 -0800689 tls_ctx_t *ctx;
690
Florin Coras58d36f02018-03-09 13:05:53 -0800691 TLS_DBG (1, "Disconnecting %x", ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800692
Florin Coras58d36f02018-03-09 13:05:53 -0800693 ctx = tls_ctx_get (ctx_handle);
Florin Corasd7f17a22024-01-31 16:18:17 -0800694 ctx->flags |= TLS_CONN_F_APP_CLOSED;
Florin Coras06a6a302019-04-17 14:19:12 -0700695 tls_ctx_app_close (ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800696}
697
698u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800699tls_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
Florin Coras371ca502018-02-21 12:07:41 -0800700{
Florin Corasc9940fc2019-02-05 20:55:11 -0800701 vnet_listen_args_t _bargs, *args = &_bargs;
Florin Corasa54b62d2021-04-21 09:05:56 -0700702 transport_endpt_crypto_cfg_t *ccfg;
Florin Corasab2f6db2018-08-31 14:31:41 -0700703 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800704 tls_main_t *tm = &tls_main;
Florin Corasa27a46e2019-02-18 13:02:28 -0800705 session_handle_t tls_al_handle;
Florin Coras5665ced2018-10-25 18:03:45 -0700706 session_endpoint_cfg_t *sep;
Florin Coras288eaab2019-02-03 15:26:14 -0800707 session_t *tls_listener;
708 session_t *app_listener;
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200709 crypto_engine_type_t engine_type;
Florin Coras74cac882018-09-07 09:13:15 -0700710 application_t *app;
Florin Corasa27a46e2019-02-18 13:02:28 -0800711 app_listener_t *al;
Florin Coras15531972018-08-12 23:50:53 -0700712 tls_ctx_t *lctx;
713 u32 lctx_index;
Matus Fabian10c016c2024-10-16 15:29:36 +0200714 transport_endpt_ext_cfg_t *ext_cfg;
Florin Corase5f18332021-04-22 11:48:35 -0700715 int rv;
Florin Coras371ca502018-02-21 12:07:41 -0800716
Florin Coras5665ced2018-10-25 18:03:45 -0700717 sep = (session_endpoint_cfg_t *) tep;
Matus Fabian10c016c2024-10-16 15:29:36 +0200718 ext_cfg = session_endpoint_get_ext_cfg (sep, TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
719 if (!ext_cfg)
Florin Corase5f18332021-04-22 11:48:35 -0700720 return SESSION_E_NOEXTCFG;
Florin Corasa54b62d2021-04-21 09:05:56 -0700721
Florin Coras15531972018-08-12 23:50:53 -0700722 app_wrk = app_worker_get (sep->app_wrk_index);
723 app = application_get (app_wrk->app_index);
Florin Corasa54b62d2021-04-21 09:05:56 -0700724
Matus Fabian10c016c2024-10-16 15:29:36 +0200725 ccfg = &ext_cfg->crypto;
Florin Corasa54b62d2021-04-21 09:05:56 -0700726 engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200727 if (engine_type == CRYPTO_ENGINE_NONE)
Florin Coras58d36f02018-03-09 13:05:53 -0800728 {
729 clib_warning ("No tls engine_type available");
Florin Corase5f18332021-04-22 11:48:35 -0700730 return SESSION_E_NOCRYPTOENG;
Florin Coras58d36f02018-03-09 13:05:53 -0800731 }
732
Dave Barachb7b92992018-10-17 10:38:51 -0400733 clib_memset (args, 0, sizeof (*args));
Florin Coras74cac882018-09-07 09:13:15 -0700734 args->app_index = tm->app_index;
735 args->sep_ext = *sep;
Florin Coras8a140612019-02-18 22:39:39 -0800736 args->sep_ext.ns_index = app->ns_index;
Florin Coras95cd8642019-12-30 21:53:19 -0800737 args->sep_ext.transport_proto = TRANSPORT_PROTO_TCP;
Florin Coras4b47ee22020-11-19 13:38:26 -0800738 if (sep->transport_proto == TRANSPORT_PROTO_DTLS)
739 {
740 args->sep_ext.transport_proto = TRANSPORT_PROTO_UDP;
741 args->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
742 }
Florin Corase5f18332021-04-22 11:48:35 -0700743 if ((rv = vnet_listen (args)))
744 return rv;
Florin Coras371ca502018-02-21 12:07:41 -0800745
Florin Coras74cac882018-09-07 09:13:15 -0700746 lctx_index = tls_listener_ctx_alloc ();
Florin Corasa27a46e2019-02-18 13:02:28 -0800747 tls_al_handle = args->handle;
748 al = app_listener_get_w_handle (tls_al_handle);
749 tls_listener = app_listener_get_session (al);
Florin Coras371ca502018-02-21 12:07:41 -0800750 tls_listener->opaque = lctx_index;
Florin Coras58d36f02018-03-09 13:05:53 -0800751
Florin Coras5c9083d2018-04-13 06:39:07 -0700752 app_listener = listen_session_get (app_listener_index);
Florin Coras58d36f02018-03-09 13:05:53 -0800753
754 lctx = tls_listener_ctx_get (lctx_index);
Florin Corasdf57ea02019-02-18 20:14:20 -0800755 lctx->parent_app_wrk_index = sep->app_wrk_index;
Florin Coras3a2cf842024-07-11 05:15:47 -0700756 lctx->ts_app_index = tm->app_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800757 lctx->tls_session_handle = tls_al_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800758 lctx->app_session_handle = listen_session_get_handle (app_listener);
759 lctx->tcp_is_ip4 = sep->is_ip4;
Florin Coras58d36f02018-03-09 13:05:53 -0800760 lctx->tls_ctx_engine = engine_type;
Florin Coras4b47ee22020-11-19 13:38:26 -0800761 lctx->tls_type = sep->transport_proto;
Florin Corasa54b62d2021-04-21 09:05:56 -0700762 lctx->ckpair_index = ccfg->ckpair_index;
Florin Coras7705b492022-02-16 17:12:01 -0800763 lctx->c_s_index = app_listener_index;
Filip Tehlar61e26be2021-12-20 14:40:59 +0000764 lctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Coras58d36f02018-03-09 13:05:53 -0800765
Florin Corasae3eaac2019-07-23 22:38:16 -0700766 if (tls_vfts[engine_type].ctx_start_listen (lctx))
767 {
768 vnet_unlisten_args_t a = {
769 .handle = lctx->tls_session_handle,
770 .app_index = tls_main.app_index,
771 .wrk_map_index = 0
772 };
773 if ((vnet_unlisten (&a)))
774 clib_warning ("unlisten returned");
775 tls_listener_ctx_free (lctx);
776 lctx_index = SESSION_INVALID_INDEX;
777 }
Ping Yudecda5b2018-08-13 06:20:00 -0400778
Florin Coras58d36f02018-03-09 13:05:53 -0800779 TLS_DBG (1, "Started listening %d, engine type %d", lctx_index,
780 engine_type);
Florin Coras371ca502018-02-21 12:07:41 -0800781 return lctx_index;
782}
783
784u32
Florin Corase3e2f072018-03-04 07:24:30 -0800785tls_stop_listen (u32 lctx_index)
Florin Coras371ca502018-02-21 12:07:41 -0800786{
Florin Corasaab06042020-02-26 02:56:14 +0000787 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200788 crypto_engine_type_t engine_type;
Florin Corasaab06042020-02-26 02:56:14 +0000789 transport_connection_t *lc;
Florin Corasb5e94e32018-09-14 14:46:39 -0700790 tls_ctx_t *lctx;
Florin Corasaab06042020-02-26 02:56:14 +0000791 session_t *ls;
Florin Corasc1a42652019-02-08 18:27:29 -0800792 int rv;
Ping Yudecda5b2018-08-13 06:20:00 -0400793
Florin Corase3e2f072018-03-04 07:24:30 -0800794 lctx = tls_listener_ctx_get (lctx_index);
Florin Corasaab06042020-02-26 02:56:14 +0000795
796 /* Cleanup listener from session lookup table */
797 ls = session_get_from_handle (lctx->tls_session_handle);
798 lc = session_get_transport (ls);
799
800 sep.fib_index = lc->fib_index;
801 sep.port = lc->lcl_port;
802 sep.is_ip4 = lc->is_ip4;
Florin Coras4b47ee22020-11-19 13:38:26 -0800803 sep.transport_proto = lctx->tls_type;
Florin Corasaab06042020-02-26 02:56:14 +0000804 clib_memcpy (&sep.ip, &lc->lcl_ip, sizeof (lc->lcl_ip));
805 session_lookup_del_session_endpoint2 (&sep);
806
Florin Corasc1a42652019-02-08 18:27:29 -0800807 vnet_unlisten_args_t a = {
Florin Corasb5e94e32018-09-14 14:46:39 -0700808 .handle = lctx->tls_session_handle,
809 .app_index = tls_main.app_index,
810 .wrk_map_index = 0 /* default wrk */
811 };
Florin Corasc1a42652019-02-08 18:27:29 -0800812 if ((rv = vnet_unlisten (&a)))
813 clib_warning ("unlisten returned %d", rv);
Florin Corasb5e94e32018-09-14 14:46:39 -0700814
Ping Yudecda5b2018-08-13 06:20:00 -0400815 engine_type = lctx->tls_ctx_engine;
816 tls_vfts[engine_type].ctx_stop_listen (lctx);
817
Florin Corase3e2f072018-03-04 07:24:30 -0800818 tls_listener_ctx_free (lctx);
Florin Coras371ca502018-02-21 12:07:41 -0800819 return 0;
820}
821
822transport_connection_t *
Florin Corase3e2f072018-03-04 07:24:30 -0800823tls_connection_get (u32 ctx_index, u32 thread_index)
824{
825 tls_ctx_t *ctx;
826 ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
827 return &ctx->connection;
828}
829
830transport_connection_t *
Florin Coras371ca502018-02-21 12:07:41 -0800831tls_listener_get (u32 listener_index)
832{
833 tls_ctx_t *ctx;
834 ctx = tls_listener_ctx_get (listener_index);
835 return &ctx->connection;
836}
837
Florin Coras2c876f92021-05-10 21:12:27 -0700838static transport_connection_t *
839tls_half_open_get (u32 ho_index)
840{
Florin Coras2c876f92021-05-10 21:12:27 -0700841 tls_ctx_t *ctx;
842 ctx = tls_ctx_half_open_get (ho_index);
Florin Coras2c876f92021-05-10 21:12:27 -0700843 return &ctx->connection;
844}
845
846static void
847tls_cleanup_ho (u32 ho_index)
848{
Florin Coras2c876f92021-05-10 21:12:27 -0700849 tls_ctx_t *ctx;
Florin Coras4aaedaa2023-11-10 09:48:39 -0800850 session_t *s;
Florin Corasbab6c522021-05-13 08:36:56 -0700851
Florin Coras2c876f92021-05-10 21:12:27 -0700852 ctx = tls_ctx_half_open_get (ho_index);
Florin Corasa474bc82023-12-04 20:29:52 -0800853 /* Already pending cleanup */
854 if (ctx->tls_session_handle == SESSION_INVALID_HANDLE)
855 {
856 ASSERT (ctx->flags & TLS_CONN_F_HO_DONE);
Florin Coras4a98b932024-01-31 13:45:39 -0800857 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Corasa474bc82023-12-04 20:29:52 -0800858 return;
859 }
860
Florin Coras4aaedaa2023-11-10 09:48:39 -0800861 s = session_get_from_handle (ctx->tls_session_handle);
862 /* If no pending cleanup notification, force cleanup now. Otherwise,
863 * wait for cleanup notification and set no app session on ctx */
864 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
865 {
866 session_cleanup_half_open (ctx->tls_session_handle);
867 tls_ctx_half_open_free (ho_index);
868 }
869 else
Florin Coras4a98b932024-01-31 13:45:39 -0800870 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Coras2c876f92021-05-10 21:12:27 -0700871}
872
Florin Corasf940f8a2019-03-06 10:44:38 -0800873int
Florin Coras9f86d222020-03-23 15:34:22 +0000874tls_custom_tx_callback (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -0800875{
Florin Coras00d7d862023-09-22 19:26:33 -0700876 session_t *as = (session_t *) session;
Florin Corasf940f8a2019-03-06 10:44:38 -0800877 tls_ctx_t *ctx;
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800878
Florin Coras00d7d862023-09-22 19:26:33 -0700879 if (PREDICT_FALSE (as->session_state >= SESSION_STATE_TRANSPORT_CLOSED ||
880 as->session_state <= SESSION_STATE_ACCEPTING))
881 {
882 sp->flags |= TRANSPORT_SND_F_DESCHED;
883 return 0;
884 }
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800885
Florin Coras00d7d862023-09-22 19:26:33 -0700886 ctx = tls_ctx_get (as->connection_index);
887 return tls_ctx_write (ctx, as, sp);
Florin Corasf940f8a2019-03-06 10:44:38 -0800888}
889
Florin Coras371ca502018-02-21 12:07:41 -0800890u8 *
891format_tls_ctx (u8 * s, va_list * args)
892{
jiangxiaoming5b7ea912020-09-23 10:29:21 +0800893 u32 tcp_si, tcp_ti, ctx_index, ctx_engine;
Florin Coras371ca502018-02-21 12:07:41 -0800894 tls_ctx_t *ctx = va_arg (*args, tls_ctx_t *);
Florin Coras4b47ee22020-11-19 13:38:26 -0800895 char *proto;
Florin Coras371ca502018-02-21 12:07:41 -0800896
Florin Coras4b47ee22020-11-19 13:38:26 -0800897 proto = ctx->tls_type == TRANSPORT_PROTO_TLS ? "TLS" : "DTLS";
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800898 session_parse_handle (ctx->tls_session_handle, &tcp_si, &tcp_ti);
899 tls_ctx_parse_handle (ctx->tls_ctx_handle, &ctx_index, &ctx_engine);
Florin Coras4b47ee22020-11-19 13:38:26 -0800900 s =
901 format (s, "[%d:%d][%s] app_wrk %u index %u engine %u ts %d:%d",
902 ctx->c_thread_index, ctx->c_s_index, proto,
903 ctx->parent_app_wrk_index, ctx_index, ctx_engine, tcp_ti, tcp_si);
Florin Coras371ca502018-02-21 12:07:41 -0800904
Florin Coras371ca502018-02-21 12:07:41 -0800905 return s;
906}
907
Florin Coras0d74dd12020-01-07 19:01:26 +0000908static u8 *
909format_tls_listener_ctx (u8 * s, va_list * args)
910{
911 session_t *tls_listener;
912 app_listener_t *al;
Florin Coras0d74dd12020-01-07 19:01:26 +0000913 tls_ctx_t *ctx;
Florin Coras4b47ee22020-11-19 13:38:26 -0800914 char *proto;
Florin Coras0d74dd12020-01-07 19:01:26 +0000915
916 ctx = va_arg (*args, tls_ctx_t *);
917
Florin Coras4b47ee22020-11-19 13:38:26 -0800918 proto = ctx->tls_type == TRANSPORT_PROTO_TLS ? "TLS" : "DTLS";
Florin Coras0d74dd12020-01-07 19:01:26 +0000919 al = app_listener_get_w_handle (ctx->tls_session_handle);
920 tls_listener = app_listener_get_session (al);
Florin Coras4b47ee22020-11-19 13:38:26 -0800921 s = format (s, "[%d:%d][%s] app_wrk %u engine %u ts %d:%d",
922 ctx->c_thread_index, ctx->c_s_index, proto,
jiangxiaoming5b7ea912020-09-23 10:29:21 +0800923 ctx->parent_app_wrk_index, ctx->tls_ctx_engine,
Florin Coras0d74dd12020-01-07 19:01:26 +0000924 tls_listener->thread_index, tls_listener->session_index);
925
926 return s;
927}
928
929static u8 *
930format_tls_ctx_state (u8 * s, va_list * args)
931{
932 tls_ctx_t *ctx;
Florin Corasd0e8bd72023-10-17 11:59:49 -0700933 session_t *as;
Florin Coras0d74dd12020-01-07 19:01:26 +0000934
935 ctx = va_arg (*args, tls_ctx_t *);
Florin Corasd0e8bd72023-10-17 11:59:49 -0700936 as = session_get (ctx->c_s_index, ctx->c_thread_index);
937 if (as->session_state == SESSION_STATE_LISTENING)
Florin Coras0d74dd12020-01-07 19:01:26 +0000938 s = format (s, "%s", "LISTEN");
Florin Coras0d74dd12020-01-07 19:01:26 +0000939 else
Florin Coras681ea6d2020-04-24 16:59:49 +0000940 {
Florin Corasd0e8bd72023-10-17 11:59:49 -0700941 if (as->session_state == SESSION_STATE_READY)
Florin Coras681ea6d2020-04-24 16:59:49 +0000942 s = format (s, "%s", "ESTABLISHED");
Florin Corasd0e8bd72023-10-17 11:59:49 -0700943 else if (as->session_state == SESSION_STATE_ACCEPTING)
944 s = format (s, "%s", "ACCEPTING");
945 else if (as->session_state == SESSION_STATE_CONNECTING)
946 s = format (s, "%s", "CONNECTING");
947 else if (as->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
948 s = format (s, "%s", "CLOSED");
949 else if (as->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
950 s = format (s, "%s", "CLOSING");
Florin Coras681ea6d2020-04-24 16:59:49 +0000951 else
Florin Corasd0e8bd72023-10-17 11:59:49 -0700952 s = format (s, "UNHANDLED %u", as->session_state);
Florin Coras681ea6d2020-04-24 16:59:49 +0000953 }
Florin Coras0d74dd12020-01-07 19:01:26 +0000954
955 return s;
956}
957
Florin Coras371ca502018-02-21 12:07:41 -0800958u8 *
959format_tls_connection (u8 * s, va_list * args)
960{
961 u32 ctx_index = va_arg (*args, u32);
962 u32 thread_index = va_arg (*args, u32);
963 u32 verbose = va_arg (*args, u32);
964 tls_ctx_t *ctx;
965
966 ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
967 if (!ctx)
968 return s;
969
Florin Coras7bf6ed62020-09-23 12:02:08 -0700970 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tls_ctx, ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800971 if (verbose)
972 {
Florin Coras7bf6ed62020-09-23 12:02:08 -0700973 s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tls_ctx_state,
974 ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800975 if (verbose > 1)
976 s = format (s, "\n");
977 }
978 return s;
979}
980
981u8 *
982format_tls_listener (u8 * s, va_list * args)
983{
984 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200985 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Coras0d74dd12020-01-07 19:01:26 +0000986 u32 verbose = va_arg (*args, u32);
Florin Coras371ca502018-02-21 12:07:41 -0800987 tls_ctx_t *ctx = tls_listener_ctx_get (tc_index);
Florin Coras371ca502018-02-21 12:07:41 -0800988
Florin Coras7bf6ed62020-09-23 12:02:08 -0700989 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tls_listener_ctx, ctx);
Florin Coras0d74dd12020-01-07 19:01:26 +0000990 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -0700991 s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tls_ctx_state, ctx);
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800992 return s;
Florin Coras371ca502018-02-21 12:07:41 -0800993}
994
995u8 *
996format_tls_half_open (u8 * s, va_list * args)
997{
Florin Coras2c876f92021-05-10 21:12:27 -0700998 u32 ho_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200999 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd69ef3a2023-10-02 10:41:54 -07001000 u32 __clib_unused verbose = va_arg (*args, u32);
Florin Coras2c876f92021-05-10 21:12:27 -07001001 session_t *tcp_ho;
1002 tls_ctx_t *ho_ctx;
1003
1004 ho_ctx = tls_ctx_half_open_get (ho_index);
1005
1006 tcp_ho = session_get_from_handle (ho_ctx->tls_session_handle);
1007 s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u ts %d:%d",
1008 ho_ctx->c_thread_index, ho_ctx->c_s_index, "TLS",
1009 ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
1010 tcp_ho->thread_index, tcp_ho->session_index);
1011
Florin Coras371ca502018-02-21 12:07:41 -08001012 return s;
1013}
1014
Yu Ping0b819152019-05-07 02:24:30 +08001015static void
1016tls_transport_endpoint_get (u32 ctx_handle, u32 thread_index,
1017 transport_endpoint_t * tep, u8 is_lcl)
1018{
1019 tls_ctx_t *ctx = tls_ctx_get_w_thread (ctx_handle, thread_index);
Florin Coras8074fc62024-02-20 11:24:54 -08001020 session_t *ts;
Yu Ping0b819152019-05-07 02:24:30 +08001021
Florin Coras8074fc62024-02-20 11:24:54 -08001022 ts = session_get_from_handle (ctx->tls_session_handle);
1023 if (ts && ts->session_state < SESSION_STATE_TRANSPORT_DELETED)
1024 session_get_endpoint (ts, tep, is_lcl);
Yu Ping0b819152019-05-07 02:24:30 +08001025}
1026
1027static void
1028tls_transport_listener_endpoint_get (u32 ctx_handle,
1029 transport_endpoint_t * tep, u8 is_lcl)
1030{
1031 session_t *tls_listener;
1032 app_listener_t *al;
1033 tls_ctx_t *ctx = tls_listener_ctx_get (ctx_handle);
1034
1035 al = app_listener_get_w_handle (ctx->tls_session_handle);
1036 tls_listener = app_listener_get_session (al);
1037 session_get_endpoint (tls_listener, tep, is_lcl);
1038}
1039
Florin Coras0a9ab822020-10-25 14:01:37 -07001040static clib_error_t *
1041tls_enable (vlib_main_t * vm, u8 is_en)
1042{
Florin Coras0a9ab822020-10-25 14:01:37 -07001043 vnet_app_detach_args_t _da, *da = &_da;
1044 vnet_app_attach_args_t _a, *a = &_a;
1045 u64 options[APP_OPTIONS_N_OPTIONS];
1046 tls_main_t *tm = &tls_main;
Florin Corasc1b03802023-09-30 15:21:08 -07001047 u32 fifo_size = 512 << 10;
Florin Coras0a9ab822020-10-25 14:01:37 -07001048
1049 if (!is_en)
1050 {
1051 da->app_index = tm->app_index;
1052 da->api_client_index = APP_INVALID_INDEX;
1053 vnet_application_detach (da);
1054 return 0;
1055 }
1056
Florin Coras0a9ab822020-10-25 14:01:37 -07001057 fifo_size = tm->fifo_size ? tm->fifo_size : fifo_size;
1058
1059 clib_memset (a, 0, sizeof (*a));
1060 clib_memset (options, 0, sizeof (options));
1061
1062 a->session_cb_vft = &tls_app_cb_vft;
1063 a->api_client_index = APP_INVALID_INDEX;
1064 a->options = options;
1065 a->name = format (0, "tls");
Florin Corasa3767bd2021-11-18 15:25:31 -08001066 a->options[APP_OPTIONS_SEGMENT_SIZE] = tm->first_seg_size;
1067 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = tm->add_seg_size;
Florin Coras0a9ab822020-10-25 14:01:37 -07001068 a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
1069 a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
1070 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1071 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1072 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1073
1074 if (vnet_application_attach (a))
1075 {
1076 clib_warning ("failed to attach tls app");
1077 return clib_error_return (0, "failed to attach tls app");
1078 }
1079
1080 tm->app_index = a->app_index;
1081 vec_free (a->name);
1082
1083 return 0;
1084}
1085
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001086static const transport_proto_vft_t tls_proto = {
Florin Coras0a9ab822020-10-25 14:01:37 -07001087 .enable = tls_enable,
Florin Coras1ee78302019-02-05 15:51:15 -08001088 .connect = tls_connect,
Florin Coras371ca502018-02-21 12:07:41 -08001089 .close = tls_disconnect,
Florin Coras1ee78302019-02-05 15:51:15 -08001090 .start_listen = tls_start_listen,
1091 .stop_listen = tls_stop_listen,
Florin Corase3e2f072018-03-04 07:24:30 -08001092 .get_connection = tls_connection_get,
Florin Coras371ca502018-02-21 12:07:41 -08001093 .get_listener = tls_listener_get,
Florin Coras2c876f92021-05-10 21:12:27 -07001094 .get_half_open = tls_half_open_get,
1095 .cleanup_ho = tls_cleanup_ho,
Florin Corasf940f8a2019-03-06 10:44:38 -08001096 .custom_tx = tls_custom_tx_callback,
Florin Coras371ca502018-02-21 12:07:41 -08001097 .format_connection = format_tls_connection,
1098 .format_half_open = format_tls_half_open,
1099 .format_listener = format_tls_listener,
Yu Ping0b819152019-05-07 02:24:30 +08001100 .get_transport_endpoint = tls_transport_endpoint_get,
1101 .get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001102 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +00001103 .name = "tls",
1104 .short_name = "J",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001105 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Coras2c876f92021-05-10 21:12:27 -07001106 .service_type = TRANSPORT_SERVICE_VC,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001107 },
Florin Coras371ca502018-02-21 12:07:41 -08001108};
Florin Coras4b47ee22020-11-19 13:38:26 -08001109
1110int
1111dtls_connect (transport_endpoint_cfg_t *tep)
1112{
1113 vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
Florin Corasa54b62d2021-04-21 09:05:56 -07001114 transport_endpt_crypto_cfg_t *ccfg;
Florin Coras4b47ee22020-11-19 13:38:26 -08001115 crypto_engine_type_t engine_type;
1116 session_endpoint_cfg_t *sep;
1117 tls_main_t *tm = &tls_main;
1118 app_worker_t *app_wrk;
1119 application_t *app;
1120 tls_ctx_t *ctx;
1121 u32 ctx_handle;
Matus Fabian10c016c2024-10-16 15:29:36 +02001122 transport_endpt_ext_cfg_t *ext_cfg;
Florin Coras4b47ee22020-11-19 13:38:26 -08001123 int rv;
1124
1125 sep = (session_endpoint_cfg_t *) tep;
Matus Fabian10c016c2024-10-16 15:29:36 +02001126 ext_cfg = session_endpoint_get_ext_cfg (sep, TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
1127 if (!ext_cfg)
Florin Corasa54b62d2021-04-21 09:05:56 -07001128 return -1;
1129
Florin Coras4b47ee22020-11-19 13:38:26 -08001130 app_wrk = app_worker_get (sep->app_wrk_index);
1131 app = application_get (app_wrk->app_index);
Florin Corasa54b62d2021-04-21 09:05:56 -07001132
Matus Fabian10c016c2024-10-16 15:29:36 +02001133 ccfg = &ext_cfg->crypto;
Florin Corasa54b62d2021-04-21 09:05:56 -07001134 engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
Florin Coras4b47ee22020-11-19 13:38:26 -08001135 if (engine_type == CRYPTO_ENGINE_NONE)
1136 {
1137 clib_warning ("No tls engine_type available");
1138 return -1;
1139 }
1140
Florin Corasfb50bc32021-05-18 00:28:59 -07001141 ctx_handle = tls_ctx_alloc_w_thread (engine_type, transport_cl_thread ());
1142 ctx = tls_ctx_get_w_thread (ctx_handle, transport_cl_thread ());
Florin Coras4b47ee22020-11-19 13:38:26 -08001143 ctx->parent_app_wrk_index = sep->app_wrk_index;
1144 ctx->parent_app_api_context = sep->opaque;
1145 ctx->tcp_is_ip4 = sep->is_ip4;
Florin Corasa54b62d2021-04-21 09:05:56 -07001146 ctx->ckpair_index = ccfg->ckpair_index;
Florin Coras4b47ee22020-11-19 13:38:26 -08001147 ctx->tls_type = sep->transport_proto;
1148 ctx->tls_ctx_handle = ctx_handle;
Florin Corasbab6c522021-05-13 08:36:56 -07001149 ctx->c_proto = TRANSPORT_PROTO_DTLS;
1150 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasa54b62d2021-04-21 09:05:56 -07001151 if (ccfg->hostname[0])
Florin Coras4b47ee22020-11-19 13:38:26 -08001152 {
Florin Corasa54b62d2021-04-21 09:05:56 -07001153 ctx->srv_hostname = format (0, "%s", ccfg->hostname);
Florin Coras4b47ee22020-11-19 13:38:26 -08001154 vec_terminate_c_string (ctx->srv_hostname);
1155 }
1156
Florin Coras4b47ee22020-11-19 13:38:26 -08001157 ctx->tls_ctx_engine = engine_type;
1158
1159 clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));
1160 cargs->sep.transport_proto = TRANSPORT_PROTO_UDP;
1161 cargs->app_index = tm->app_index;
1162 cargs->api_context = ctx_handle;
1163 cargs->sep_ext.ns_index = app->ns_index;
1164 cargs->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
1165 if ((rv = vnet_connect (cargs)))
1166 return rv;
1167
1168 TLS_DBG (1, "New DTLS connect request %x engine %d", ctx_handle,
1169 engine_type);
Florin Corasbab6c522021-05-13 08:36:56 -07001170 return ctx_handle;
1171}
1172
1173static transport_connection_t *
1174dtls_half_open_get (u32 ho_index)
1175{
1176 tls_ctx_t *ho_ctx;
Florin Corasfb50bc32021-05-18 00:28:59 -07001177 ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
Florin Corasbab6c522021-05-13 08:36:56 -07001178 return &ho_ctx->connection;
Florin Coras4b47ee22020-11-19 13:38:26 -08001179}
1180
1181static void
1182dtls_cleanup_callback (u32 ctx_index, u32 thread_index)
1183{
1184 /* No op */
1185}
1186
Florin Corasbab6c522021-05-13 08:36:56 -07001187static void
1188dtls_cleanup_ho (u32 ho_index)
1189{
1190 tls_ctx_t *ctx;
Florin Corasfb50bc32021-05-18 00:28:59 -07001191 ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
Florin Corasbab6c522021-05-13 08:36:56 -07001192 tls_ctx_free (ctx);
1193}
1194
1195u8 *
1196format_dtls_half_open (u8 *s, va_list *args)
1197{
1198 u32 ho_index = va_arg (*args, u32);
1199 u32 __clib_unused thread_index = va_arg (*args, u32);
1200 tls_ctx_t *ho_ctx;
1201 session_t *us;
1202
Florin Corasfb50bc32021-05-18 00:28:59 -07001203 ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
Florin Corasbab6c522021-05-13 08:36:56 -07001204
1205 us = session_get_from_handle (ho_ctx->tls_session_handle);
1206 s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u us %d:%d",
1207 ho_ctx->c_thread_index, ho_ctx->c_s_index, "DTLS",
1208 ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
1209 us->thread_index, us->session_index);
1210
1211 return s;
1212}
1213
Florin Coras4b47ee22020-11-19 13:38:26 -08001214static const transport_proto_vft_t dtls_proto = {
1215 .enable = 0,
1216 .connect = dtls_connect,
1217 .close = tls_disconnect,
1218 .start_listen = tls_start_listen,
1219 .stop_listen = tls_stop_listen,
1220 .get_connection = tls_connection_get,
1221 .get_listener = tls_listener_get,
Florin Corasbab6c522021-05-13 08:36:56 -07001222 .get_half_open = dtls_half_open_get,
Florin Coras4b47ee22020-11-19 13:38:26 -08001223 .custom_tx = tls_custom_tx_callback,
1224 .cleanup = dtls_cleanup_callback,
Florin Corasbab6c522021-05-13 08:36:56 -07001225 .cleanup_ho = dtls_cleanup_ho,
Florin Coras4b47ee22020-11-19 13:38:26 -08001226 .format_connection = format_tls_connection,
Florin Corasbab6c522021-05-13 08:36:56 -07001227 .format_half_open = format_dtls_half_open,
Florin Coras4b47ee22020-11-19 13:38:26 -08001228 .format_listener = format_tls_listener,
1229 .get_transport_endpoint = tls_transport_endpoint_get,
1230 .get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
1231 .transport_options = {
1232 .name = "dtls",
1233 .short_name = "D",
1234 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Corasbab6c522021-05-13 08:36:56 -07001235 .service_type = TRANSPORT_SERVICE_VC,
Florin Coras4b47ee22020-11-19 13:38:26 -08001236 },
1237};
Florin Coras371ca502018-02-21 12:07:41 -08001238
Florin Corasd77eee62018-03-07 08:49:27 -08001239void
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +02001240tls_register_engine (const tls_engine_vft_t * vft, crypto_engine_type_t type)
Florin Coras371ca502018-02-21 12:07:41 -08001241{
Florin Corasd77eee62018-03-07 08:49:27 -08001242 vec_validate (tls_vfts, type);
1243 tls_vfts[type] = *vft;
Florin Coras371ca502018-02-21 12:07:41 -08001244}
1245
Dave Barachcc5677b2018-03-28 19:08:45 -04001246static clib_error_t *
Florin Coras371ca502018-02-21 12:07:41 -08001247tls_init (vlib_main_t * vm)
1248{
Florin Coras58d36f02018-03-09 13:05:53 -08001249 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -08001250 tls_main_t *tm = &tls_main;
Florin Coras0a9ab822020-10-25 14:01:37 -07001251 u32 num_threads;
Florin Coras58d36f02018-03-09 13:05:53 -08001252
1253 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras371ca502018-02-21 12:07:41 -08001254
Florin Corasd77eee62018-03-07 08:49:27 -08001255 if (!tm->ca_cert_path)
1256 tm->ca_cert_path = TLS_CA_CERT_PATH;
1257
Florin Coras58d36f02018-03-09 13:05:53 -08001258 vec_validate (tm->rx_bufs, num_threads - 1);
1259 vec_validate (tm->tx_bufs, num_threads - 1);
1260
Varun Rapellyb8af24b2024-08-17 16:19:29 +00001261 /*
1262 * first_seg_size default value 32MB
1263 * add_seg_size default value 256 MB
1264 */
Florin Corasa3767bd2021-11-18 15:25:31 -08001265 tm->first_seg_size = 32 << 20;
1266 tm->add_seg_size = 256 << 20;
1267
Florin Coras371ca502018-02-21 12:07:41 -08001268 transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
1269 FIB_PROTOCOL_IP4, ~0);
1270 transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
1271 FIB_PROTOCOL_IP6, ~0);
Florin Coras4b47ee22020-11-19 13:38:26 -08001272
1273 transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto,
1274 FIB_PROTOCOL_IP4, ~0);
1275 transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto,
1276 FIB_PROTOCOL_IP6, ~0);
Florin Coras371ca502018-02-21 12:07:41 -08001277 return 0;
1278}
1279
1280VLIB_INIT_FUNCTION (tls_init);
1281
Florin Coras8f89dd02018-03-05 16:53:07 -08001282static clib_error_t *
1283tls_config_fn (vlib_main_t * vm, unformat_input_t * input)
1284{
1285 tls_main_t *tm = &tls_main;
Dave Wallaceb1a81aa2019-10-30 17:53:56 +00001286 uword tmp;
Florin Coras8f89dd02018-03-05 16:53:07 -08001287 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1288 {
1289 if (unformat (input, "use-test-cert-in-ca"))
1290 tm->use_test_cert_in_ca = 1;
1291 else if (unformat (input, "ca-cert-path %s", &tm->ca_cert_path))
1292 ;
Florin Corasddd98f32019-03-25 08:30:53 -07001293 else if (unformat (input, "first-segment-size %U", unformat_memory_size,
1294 &tm->first_seg_size))
1295 ;
Florin Corasa3767bd2021-11-18 15:25:31 -08001296 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1297 &tm->add_seg_size))
1298 ;
Dave Wallaceb1a81aa2019-10-30 17:53:56 +00001299 else if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp))
1300 {
1301 if (tmp >= 0x100000000ULL)
1302 {
1303 return clib_error_return
1304 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
1305 }
1306 tm->fifo_size = tmp;
1307 }
Florin Coras8f89dd02018-03-05 16:53:07 -08001308 else
1309 return clib_error_return (0, "unknown input `%U'",
1310 format_unformat_error, input);
1311 }
1312 return 0;
1313}
1314
Florin Coras1cbcdce2022-04-06 15:10:13 -07001315VLIB_CONFIG_FUNCTION (tls_config_fn, "tls");
Florin Corasd77eee62018-03-07 08:49:27 -08001316
1317tls_main_t *
1318vnet_tls_get_main (void)
1319{
1320 return &tls_main;
1321}
1322
Florin Coras371ca502018-02-21 12:07:41 -08001323/*
1324 * fd.io coding-style-patch-verification: ON
1325 *
1326 * Local Variables:
1327 * eval: (c-set-style "gnu")
1328 * End:
1329 */