blob: 9bc9323732ef6268a1ebd8dc3778058e65654d7c [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,
31 .app_index = tls_main.app_index,
32 };
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{
313 ASSERT (tls_ctx_handshake_is_over (ctx));
314
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;
Florin Corasc1a42652019-02-08 18:27:29 -0800631 int rv;
Florin Coras371ca502018-02-21 12:07:41 -0800632
Florin Coras5665ced2018-10-25 18:03:45 -0700633 sep = (session_endpoint_cfg_t *) tep;
Florin Corasa54b62d2021-04-21 09:05:56 -0700634 if (!sep->ext_cfg)
Florin Corase5f18332021-04-22 11:48:35 -0700635 return SESSION_E_NOEXTCFG;
Florin Corasa54b62d2021-04-21 09:05:56 -0700636
Florin Coras15531972018-08-12 23:50:53 -0700637 app_wrk = app_worker_get (sep->app_wrk_index);
638 app = application_get (app_wrk->app_index);
Florin Corasa54b62d2021-04-21 09:05:56 -0700639
640 ccfg = &sep->ext_cfg->crypto;
641 engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200642 if (engine_type == CRYPTO_ENGINE_NONE)
Florin Coras58d36f02018-03-09 13:05:53 -0800643 {
644 clib_warning ("No tls engine_type available");
Florin Corase5f18332021-04-22 11:48:35 -0700645 return SESSION_E_NOCRYPTOENG;
Florin Coras58d36f02018-03-09 13:05:53 -0800646 }
Florin Coras371ca502018-02-21 12:07:41 -0800647
648 ctx_index = tls_ctx_half_open_alloc ();
649 ctx = tls_ctx_half_open_get (ctx_index);
Florin Corasdf57ea02019-02-18 20:14:20 -0800650 ctx->parent_app_wrk_index = sep->app_wrk_index;
Florin Coras371ca502018-02-21 12:07:41 -0800651 ctx->parent_app_api_context = sep->opaque;
652 ctx->tcp_is_ip4 = sep->is_ip4;
Florin Coras4b47ee22020-11-19 13:38:26 -0800653 ctx->tls_type = sep->transport_proto;
Florin Corasa54b62d2021-04-21 09:05:56 -0700654 ctx->ckpair_index = ccfg->ckpair_index;
Florin Coras2c876f92021-05-10 21:12:27 -0700655 ctx->c_proto = TRANSPORT_PROTO_TLS;
656 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasa54b62d2021-04-21 09:05:56 -0700657 if (ccfg->hostname[0])
Florin Coras8f89dd02018-03-05 16:53:07 -0800658 {
Florin Corasa54b62d2021-04-21 09:05:56 -0700659 ctx->srv_hostname = format (0, "%s", ccfg->hostname);
Florin Coras8f89dd02018-03-05 16:53:07 -0800660 vec_terminate_c_string (ctx->srv_hostname);
661 }
Florin Coras371ca502018-02-21 12:07:41 -0800662
Florin Coras58d36f02018-03-09 13:05:53 -0800663 ctx->tls_ctx_engine = engine_type;
Florin Coras371ca502018-02-21 12:07:41 -0800664
Dave Barach178cf492018-11-13 16:34:13 -0500665 clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));
Florin Coras15531972018-08-12 23:50:53 -0700666 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
667 cargs->app_index = tm->app_index;
668 cargs->api_context = ctx_index;
Florin Coras8a140612019-02-18 22:39:39 -0800669 cargs->sep_ext.ns_index = app->ns_index;
Florin Corasc1a42652019-02-08 18:27:29 -0800670 if ((rv = vnet_connect (cargs)))
Florin Coras3109d1c2024-01-22 18:27:43 -0800671 {
672 tls_ctx_half_open_free (ctx_index);
673 return rv;
674 }
Florin Coras371ca502018-02-21 12:07:41 -0800675
Florin Coras2c876f92021-05-10 21:12:27 -0700676 /* Track half-open tcp session in case we need to clean it up */
677 ctx->tls_session_handle = cargs->sh;
678
Florin Coras58d36f02018-03-09 13:05:53 -0800679 TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type);
Florin Coras2c876f92021-05-10 21:12:27 -0700680 return ctx_index;
Florin Coras371ca502018-02-21 12:07:41 -0800681}
682
683void
Florin Coras58d36f02018-03-09 13:05:53 -0800684tls_disconnect (u32 ctx_handle, u32 thread_index)
Florin Coras371ca502018-02-21 12:07:41 -0800685{
Florin Coras371ca502018-02-21 12:07:41 -0800686 tls_ctx_t *ctx;
687
Florin Coras58d36f02018-03-09 13:05:53 -0800688 TLS_DBG (1, "Disconnecting %x", ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800689
Florin Coras58d36f02018-03-09 13:05:53 -0800690 ctx = tls_ctx_get (ctx_handle);
Florin Corasd7f17a22024-01-31 16:18:17 -0800691 ctx->flags |= TLS_CONN_F_APP_CLOSED;
Florin Coras06a6a302019-04-17 14:19:12 -0700692 tls_ctx_app_close (ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800693}
694
695u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800696tls_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
Florin Coras371ca502018-02-21 12:07:41 -0800697{
Florin Corasc9940fc2019-02-05 20:55:11 -0800698 vnet_listen_args_t _bargs, *args = &_bargs;
Florin Corasa54b62d2021-04-21 09:05:56 -0700699 transport_endpt_crypto_cfg_t *ccfg;
Florin Corasab2f6db2018-08-31 14:31:41 -0700700 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800701 tls_main_t *tm = &tls_main;
Florin Corasa27a46e2019-02-18 13:02:28 -0800702 session_handle_t tls_al_handle;
Florin Coras5665ced2018-10-25 18:03:45 -0700703 session_endpoint_cfg_t *sep;
Florin Coras288eaab2019-02-03 15:26:14 -0800704 session_t *tls_listener;
705 session_t *app_listener;
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200706 crypto_engine_type_t engine_type;
Florin Coras74cac882018-09-07 09:13:15 -0700707 application_t *app;
Florin Corasa27a46e2019-02-18 13:02:28 -0800708 app_listener_t *al;
Florin Coras15531972018-08-12 23:50:53 -0700709 tls_ctx_t *lctx;
710 u32 lctx_index;
Florin Corase5f18332021-04-22 11:48:35 -0700711 int rv;
Florin Coras371ca502018-02-21 12:07:41 -0800712
Florin Coras5665ced2018-10-25 18:03:45 -0700713 sep = (session_endpoint_cfg_t *) tep;
Florin Corasa54b62d2021-04-21 09:05:56 -0700714 if (!sep->ext_cfg)
Florin Corase5f18332021-04-22 11:48:35 -0700715 return SESSION_E_NOEXTCFG;
Florin Corasa54b62d2021-04-21 09:05:56 -0700716
Florin Coras15531972018-08-12 23:50:53 -0700717 app_wrk = app_worker_get (sep->app_wrk_index);
718 app = application_get (app_wrk->app_index);
Florin Corasa54b62d2021-04-21 09:05:56 -0700719
720 ccfg = &sep->ext_cfg->crypto;
721 engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200722 if (engine_type == CRYPTO_ENGINE_NONE)
Florin Coras58d36f02018-03-09 13:05:53 -0800723 {
724 clib_warning ("No tls engine_type available");
Florin Corase5f18332021-04-22 11:48:35 -0700725 return SESSION_E_NOCRYPTOENG;
Florin Coras58d36f02018-03-09 13:05:53 -0800726 }
727
Dave Barachb7b92992018-10-17 10:38:51 -0400728 clib_memset (args, 0, sizeof (*args));
Florin Coras74cac882018-09-07 09:13:15 -0700729 args->app_index = tm->app_index;
730 args->sep_ext = *sep;
Florin Coras8a140612019-02-18 22:39:39 -0800731 args->sep_ext.ns_index = app->ns_index;
Florin Coras95cd8642019-12-30 21:53:19 -0800732 args->sep_ext.transport_proto = TRANSPORT_PROTO_TCP;
Florin Coras4b47ee22020-11-19 13:38:26 -0800733 if (sep->transport_proto == TRANSPORT_PROTO_DTLS)
734 {
735 args->sep_ext.transport_proto = TRANSPORT_PROTO_UDP;
736 args->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
737 }
Florin Corase5f18332021-04-22 11:48:35 -0700738 if ((rv = vnet_listen (args)))
739 return rv;
Florin Coras371ca502018-02-21 12:07:41 -0800740
Florin Coras74cac882018-09-07 09:13:15 -0700741 lctx_index = tls_listener_ctx_alloc ();
Florin Corasa27a46e2019-02-18 13:02:28 -0800742 tls_al_handle = args->handle;
743 al = app_listener_get_w_handle (tls_al_handle);
744 tls_listener = app_listener_get_session (al);
Florin Coras371ca502018-02-21 12:07:41 -0800745 tls_listener->opaque = lctx_index;
Florin Coras58d36f02018-03-09 13:05:53 -0800746
Florin Coras5c9083d2018-04-13 06:39:07 -0700747 app_listener = listen_session_get (app_listener_index);
Florin Coras58d36f02018-03-09 13:05:53 -0800748
749 lctx = tls_listener_ctx_get (lctx_index);
Florin Corasdf57ea02019-02-18 20:14:20 -0800750 lctx->parent_app_wrk_index = sep->app_wrk_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800751 lctx->tls_session_handle = tls_al_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800752 lctx->app_session_handle = listen_session_get_handle (app_listener);
753 lctx->tcp_is_ip4 = sep->is_ip4;
Florin Coras58d36f02018-03-09 13:05:53 -0800754 lctx->tls_ctx_engine = engine_type;
Florin Coras4b47ee22020-11-19 13:38:26 -0800755 lctx->tls_type = sep->transport_proto;
Florin Corasa54b62d2021-04-21 09:05:56 -0700756 lctx->ckpair_index = ccfg->ckpair_index;
Florin Coras7705b492022-02-16 17:12:01 -0800757 lctx->c_s_index = app_listener_index;
Filip Tehlar61e26be2021-12-20 14:40:59 +0000758 lctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Coras58d36f02018-03-09 13:05:53 -0800759
Florin Corasae3eaac2019-07-23 22:38:16 -0700760 if (tls_vfts[engine_type].ctx_start_listen (lctx))
761 {
762 vnet_unlisten_args_t a = {
763 .handle = lctx->tls_session_handle,
764 .app_index = tls_main.app_index,
765 .wrk_map_index = 0
766 };
767 if ((vnet_unlisten (&a)))
768 clib_warning ("unlisten returned");
769 tls_listener_ctx_free (lctx);
770 lctx_index = SESSION_INVALID_INDEX;
771 }
Ping Yudecda5b2018-08-13 06:20:00 -0400772
Florin Coras58d36f02018-03-09 13:05:53 -0800773 TLS_DBG (1, "Started listening %d, engine type %d", lctx_index,
774 engine_type);
Florin Coras371ca502018-02-21 12:07:41 -0800775 return lctx_index;
776}
777
778u32
Florin Corase3e2f072018-03-04 07:24:30 -0800779tls_stop_listen (u32 lctx_index)
Florin Coras371ca502018-02-21 12:07:41 -0800780{
Florin Corasaab06042020-02-26 02:56:14 +0000781 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200782 crypto_engine_type_t engine_type;
Florin Corasaab06042020-02-26 02:56:14 +0000783 transport_connection_t *lc;
Florin Corasb5e94e32018-09-14 14:46:39 -0700784 tls_ctx_t *lctx;
Florin Corasaab06042020-02-26 02:56:14 +0000785 session_t *ls;
Florin Corasc1a42652019-02-08 18:27:29 -0800786 int rv;
Ping Yudecda5b2018-08-13 06:20:00 -0400787
Florin Corase3e2f072018-03-04 07:24:30 -0800788 lctx = tls_listener_ctx_get (lctx_index);
Florin Corasaab06042020-02-26 02:56:14 +0000789
790 /* Cleanup listener from session lookup table */
791 ls = session_get_from_handle (lctx->tls_session_handle);
792 lc = session_get_transport (ls);
793
794 sep.fib_index = lc->fib_index;
795 sep.port = lc->lcl_port;
796 sep.is_ip4 = lc->is_ip4;
Florin Coras4b47ee22020-11-19 13:38:26 -0800797 sep.transport_proto = lctx->tls_type;
Florin Corasaab06042020-02-26 02:56:14 +0000798 clib_memcpy (&sep.ip, &lc->lcl_ip, sizeof (lc->lcl_ip));
799 session_lookup_del_session_endpoint2 (&sep);
800
Florin Corasc1a42652019-02-08 18:27:29 -0800801 vnet_unlisten_args_t a = {
Florin Corasb5e94e32018-09-14 14:46:39 -0700802 .handle = lctx->tls_session_handle,
803 .app_index = tls_main.app_index,
804 .wrk_map_index = 0 /* default wrk */
805 };
Florin Corasc1a42652019-02-08 18:27:29 -0800806 if ((rv = vnet_unlisten (&a)))
807 clib_warning ("unlisten returned %d", rv);
Florin Corasb5e94e32018-09-14 14:46:39 -0700808
Ping Yudecda5b2018-08-13 06:20:00 -0400809 engine_type = lctx->tls_ctx_engine;
810 tls_vfts[engine_type].ctx_stop_listen (lctx);
811
Florin Corase3e2f072018-03-04 07:24:30 -0800812 tls_listener_ctx_free (lctx);
Florin Coras371ca502018-02-21 12:07:41 -0800813 return 0;
814}
815
816transport_connection_t *
Florin Corase3e2f072018-03-04 07:24:30 -0800817tls_connection_get (u32 ctx_index, u32 thread_index)
818{
819 tls_ctx_t *ctx;
820 ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
821 return &ctx->connection;
822}
823
824transport_connection_t *
Florin Coras371ca502018-02-21 12:07:41 -0800825tls_listener_get (u32 listener_index)
826{
827 tls_ctx_t *ctx;
828 ctx = tls_listener_ctx_get (listener_index);
829 return &ctx->connection;
830}
831
Florin Coras2c876f92021-05-10 21:12:27 -0700832static transport_connection_t *
833tls_half_open_get (u32 ho_index)
834{
Florin Coras2c876f92021-05-10 21:12:27 -0700835 tls_ctx_t *ctx;
836 ctx = tls_ctx_half_open_get (ho_index);
Florin Coras2c876f92021-05-10 21:12:27 -0700837 return &ctx->connection;
838}
839
840static void
841tls_cleanup_ho (u32 ho_index)
842{
Florin Coras2c876f92021-05-10 21:12:27 -0700843 tls_ctx_t *ctx;
Florin Coras4aaedaa2023-11-10 09:48:39 -0800844 session_t *s;
Florin Corasbab6c522021-05-13 08:36:56 -0700845
Florin Coras2c876f92021-05-10 21:12:27 -0700846 ctx = tls_ctx_half_open_get (ho_index);
Florin Corasa474bc82023-12-04 20:29:52 -0800847 /* Already pending cleanup */
848 if (ctx->tls_session_handle == SESSION_INVALID_HANDLE)
849 {
850 ASSERT (ctx->flags & TLS_CONN_F_HO_DONE);
Florin Coras4a98b932024-01-31 13:45:39 -0800851 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Corasa474bc82023-12-04 20:29:52 -0800852 return;
853 }
854
Florin Coras4aaedaa2023-11-10 09:48:39 -0800855 s = session_get_from_handle (ctx->tls_session_handle);
856 /* If no pending cleanup notification, force cleanup now. Otherwise,
857 * wait for cleanup notification and set no app session on ctx */
858 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
859 {
860 session_cleanup_half_open (ctx->tls_session_handle);
861 tls_ctx_half_open_free (ho_index);
862 }
863 else
Florin Coras4a98b932024-01-31 13:45:39 -0800864 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Coras2c876f92021-05-10 21:12:27 -0700865}
866
Florin Corasf940f8a2019-03-06 10:44:38 -0800867int
Florin Coras9f86d222020-03-23 15:34:22 +0000868tls_custom_tx_callback (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -0800869{
Florin Coras00d7d862023-09-22 19:26:33 -0700870 session_t *as = (session_t *) session;
Florin Corasf940f8a2019-03-06 10:44:38 -0800871 tls_ctx_t *ctx;
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800872
Florin Coras00d7d862023-09-22 19:26:33 -0700873 if (PREDICT_FALSE (as->session_state >= SESSION_STATE_TRANSPORT_CLOSED ||
874 as->session_state <= SESSION_STATE_ACCEPTING))
875 {
876 sp->flags |= TRANSPORT_SND_F_DESCHED;
877 return 0;
878 }
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800879
Florin Coras00d7d862023-09-22 19:26:33 -0700880 ctx = tls_ctx_get (as->connection_index);
881 return tls_ctx_write (ctx, as, sp);
Florin Corasf940f8a2019-03-06 10:44:38 -0800882}
883
Florin Coras371ca502018-02-21 12:07:41 -0800884u8 *
885format_tls_ctx (u8 * s, va_list * args)
886{
jiangxiaoming5b7ea912020-09-23 10:29:21 +0800887 u32 tcp_si, tcp_ti, ctx_index, ctx_engine;
Florin Coras371ca502018-02-21 12:07:41 -0800888 tls_ctx_t *ctx = va_arg (*args, tls_ctx_t *);
Florin Coras4b47ee22020-11-19 13:38:26 -0800889 char *proto;
Florin Coras371ca502018-02-21 12:07:41 -0800890
Florin Coras4b47ee22020-11-19 13:38:26 -0800891 proto = ctx->tls_type == TRANSPORT_PROTO_TLS ? "TLS" : "DTLS";
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800892 session_parse_handle (ctx->tls_session_handle, &tcp_si, &tcp_ti);
893 tls_ctx_parse_handle (ctx->tls_ctx_handle, &ctx_index, &ctx_engine);
Florin Coras4b47ee22020-11-19 13:38:26 -0800894 s =
895 format (s, "[%d:%d][%s] app_wrk %u index %u engine %u ts %d:%d",
896 ctx->c_thread_index, ctx->c_s_index, proto,
897 ctx->parent_app_wrk_index, ctx_index, ctx_engine, tcp_ti, tcp_si);
Florin Coras371ca502018-02-21 12:07:41 -0800898
Florin Coras371ca502018-02-21 12:07:41 -0800899 return s;
900}
901
Florin Coras0d74dd12020-01-07 19:01:26 +0000902static u8 *
903format_tls_listener_ctx (u8 * s, va_list * args)
904{
905 session_t *tls_listener;
906 app_listener_t *al;
Florin Coras0d74dd12020-01-07 19:01:26 +0000907 tls_ctx_t *ctx;
Florin Coras4b47ee22020-11-19 13:38:26 -0800908 char *proto;
Florin Coras0d74dd12020-01-07 19:01:26 +0000909
910 ctx = va_arg (*args, tls_ctx_t *);
911
Florin Coras4b47ee22020-11-19 13:38:26 -0800912 proto = ctx->tls_type == TRANSPORT_PROTO_TLS ? "TLS" : "DTLS";
Florin Coras0d74dd12020-01-07 19:01:26 +0000913 al = app_listener_get_w_handle (ctx->tls_session_handle);
914 tls_listener = app_listener_get_session (al);
Florin Coras4b47ee22020-11-19 13:38:26 -0800915 s = format (s, "[%d:%d][%s] app_wrk %u engine %u ts %d:%d",
916 ctx->c_thread_index, ctx->c_s_index, proto,
jiangxiaoming5b7ea912020-09-23 10:29:21 +0800917 ctx->parent_app_wrk_index, ctx->tls_ctx_engine,
Florin Coras0d74dd12020-01-07 19:01:26 +0000918 tls_listener->thread_index, tls_listener->session_index);
919
920 return s;
921}
922
923static u8 *
924format_tls_ctx_state (u8 * s, va_list * args)
925{
926 tls_ctx_t *ctx;
927 session_t *ts;
928
929 ctx = va_arg (*args, tls_ctx_t *);
jiangxiaoming5b7ea912020-09-23 10:29:21 +0800930 ts = session_get (ctx->c_s_index, ctx->c_thread_index);
Florin Coras0d74dd12020-01-07 19:01:26 +0000931 if (ts->session_state == SESSION_STATE_LISTENING)
932 s = format (s, "%s", "LISTEN");
Florin Coras0d74dd12020-01-07 19:01:26 +0000933 else
Florin Coras681ea6d2020-04-24 16:59:49 +0000934 {
935 if (ts->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
936 s = format (s, "%s", "CLOSED");
937 else if (ts->session_state == SESSION_STATE_APP_CLOSED)
938 s = format (s, "%s", "APP-CLOSED");
939 else if (ts->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
940 s = format (s, "%s", "CLOSING");
941 else if (tls_ctx_handshake_is_over (ctx))
942 s = format (s, "%s", "ESTABLISHED");
943 else
944 s = format (s, "%s", "HANDSHAKE");
945 }
Florin Coras0d74dd12020-01-07 19:01:26 +0000946
947 return s;
948}
949
Florin Coras371ca502018-02-21 12:07:41 -0800950u8 *
951format_tls_connection (u8 * s, va_list * args)
952{
953 u32 ctx_index = va_arg (*args, u32);
954 u32 thread_index = va_arg (*args, u32);
955 u32 verbose = va_arg (*args, u32);
956 tls_ctx_t *ctx;
957
958 ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
959 if (!ctx)
960 return s;
961
Florin Coras7bf6ed62020-09-23 12:02:08 -0700962 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tls_ctx, ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800963 if (verbose)
964 {
Florin Coras7bf6ed62020-09-23 12:02:08 -0700965 s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tls_ctx_state,
966 ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800967 if (verbose > 1)
968 s = format (s, "\n");
969 }
970 return s;
971}
972
973u8 *
974format_tls_listener (u8 * s, va_list * args)
975{
976 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200977 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Coras0d74dd12020-01-07 19:01:26 +0000978 u32 verbose = va_arg (*args, u32);
Florin Coras371ca502018-02-21 12:07:41 -0800979 tls_ctx_t *ctx = tls_listener_ctx_get (tc_index);
Florin Coras371ca502018-02-21 12:07:41 -0800980
Florin Coras7bf6ed62020-09-23 12:02:08 -0700981 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tls_listener_ctx, ctx);
Florin Coras0d74dd12020-01-07 19:01:26 +0000982 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -0700983 s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tls_ctx_state, ctx);
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800984 return s;
Florin Coras371ca502018-02-21 12:07:41 -0800985}
986
987u8 *
988format_tls_half_open (u8 * s, va_list * args)
989{
Florin Coras2c876f92021-05-10 21:12:27 -0700990 u32 ho_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200991 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd69ef3a2023-10-02 10:41:54 -0700992 u32 __clib_unused verbose = va_arg (*args, u32);
Florin Coras2c876f92021-05-10 21:12:27 -0700993 session_t *tcp_ho;
994 tls_ctx_t *ho_ctx;
995
996 ho_ctx = tls_ctx_half_open_get (ho_index);
997
998 tcp_ho = session_get_from_handle (ho_ctx->tls_session_handle);
999 s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u ts %d:%d",
1000 ho_ctx->c_thread_index, ho_ctx->c_s_index, "TLS",
1001 ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
1002 tcp_ho->thread_index, tcp_ho->session_index);
1003
Florin Coras371ca502018-02-21 12:07:41 -08001004 return s;
1005}
1006
Yu Ping0b819152019-05-07 02:24:30 +08001007static void
1008tls_transport_endpoint_get (u32 ctx_handle, u32 thread_index,
1009 transport_endpoint_t * tep, u8 is_lcl)
1010{
1011 tls_ctx_t *ctx = tls_ctx_get_w_thread (ctx_handle, thread_index);
Florin Coras8074fc62024-02-20 11:24:54 -08001012 session_t *ts;
Yu Ping0b819152019-05-07 02:24:30 +08001013
Florin Coras8074fc62024-02-20 11:24:54 -08001014 ts = session_get_from_handle (ctx->tls_session_handle);
1015 if (ts && ts->session_state < SESSION_STATE_TRANSPORT_DELETED)
1016 session_get_endpoint (ts, tep, is_lcl);
Yu Ping0b819152019-05-07 02:24:30 +08001017}
1018
1019static void
1020tls_transport_listener_endpoint_get (u32 ctx_handle,
1021 transport_endpoint_t * tep, u8 is_lcl)
1022{
1023 session_t *tls_listener;
1024 app_listener_t *al;
1025 tls_ctx_t *ctx = tls_listener_ctx_get (ctx_handle);
1026
1027 al = app_listener_get_w_handle (ctx->tls_session_handle);
1028 tls_listener = app_listener_get_session (al);
1029 session_get_endpoint (tls_listener, tep, is_lcl);
1030}
1031
Florin Coras0a9ab822020-10-25 14:01:37 -07001032static clib_error_t *
1033tls_enable (vlib_main_t * vm, u8 is_en)
1034{
Florin Coras0a9ab822020-10-25 14:01:37 -07001035 vnet_app_detach_args_t _da, *da = &_da;
1036 vnet_app_attach_args_t _a, *a = &_a;
1037 u64 options[APP_OPTIONS_N_OPTIONS];
1038 tls_main_t *tm = &tls_main;
Florin Corasc1b03802023-09-30 15:21:08 -07001039 u32 fifo_size = 512 << 10;
Florin Coras0a9ab822020-10-25 14:01:37 -07001040
1041 if (!is_en)
1042 {
1043 da->app_index = tm->app_index;
1044 da->api_client_index = APP_INVALID_INDEX;
1045 vnet_application_detach (da);
1046 return 0;
1047 }
1048
Florin Coras0a9ab822020-10-25 14:01:37 -07001049 fifo_size = tm->fifo_size ? tm->fifo_size : fifo_size;
1050
1051 clib_memset (a, 0, sizeof (*a));
1052 clib_memset (options, 0, sizeof (options));
1053
1054 a->session_cb_vft = &tls_app_cb_vft;
1055 a->api_client_index = APP_INVALID_INDEX;
1056 a->options = options;
1057 a->name = format (0, "tls");
Florin Corasa3767bd2021-11-18 15:25:31 -08001058 a->options[APP_OPTIONS_SEGMENT_SIZE] = tm->first_seg_size;
1059 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = tm->add_seg_size;
Florin Coras0a9ab822020-10-25 14:01:37 -07001060 a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
1061 a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
1062 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1063 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1064 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1065
1066 if (vnet_application_attach (a))
1067 {
1068 clib_warning ("failed to attach tls app");
1069 return clib_error_return (0, "failed to attach tls app");
1070 }
1071
1072 tm->app_index = a->app_index;
1073 vec_free (a->name);
1074
1075 return 0;
1076}
1077
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001078static const transport_proto_vft_t tls_proto = {
Florin Coras0a9ab822020-10-25 14:01:37 -07001079 .enable = tls_enable,
Florin Coras1ee78302019-02-05 15:51:15 -08001080 .connect = tls_connect,
Florin Coras371ca502018-02-21 12:07:41 -08001081 .close = tls_disconnect,
Florin Coras1ee78302019-02-05 15:51:15 -08001082 .start_listen = tls_start_listen,
1083 .stop_listen = tls_stop_listen,
Florin Corase3e2f072018-03-04 07:24:30 -08001084 .get_connection = tls_connection_get,
Florin Coras371ca502018-02-21 12:07:41 -08001085 .get_listener = tls_listener_get,
Florin Coras2c876f92021-05-10 21:12:27 -07001086 .get_half_open = tls_half_open_get,
1087 .cleanup_ho = tls_cleanup_ho,
Florin Corasf940f8a2019-03-06 10:44:38 -08001088 .custom_tx = tls_custom_tx_callback,
Florin Coras371ca502018-02-21 12:07:41 -08001089 .format_connection = format_tls_connection,
1090 .format_half_open = format_tls_half_open,
1091 .format_listener = format_tls_listener,
Yu Ping0b819152019-05-07 02:24:30 +08001092 .get_transport_endpoint = tls_transport_endpoint_get,
1093 .get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001094 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +00001095 .name = "tls",
1096 .short_name = "J",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001097 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Coras2c876f92021-05-10 21:12:27 -07001098 .service_type = TRANSPORT_SERVICE_VC,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001099 },
Florin Coras371ca502018-02-21 12:07:41 -08001100};
Florin Coras4b47ee22020-11-19 13:38:26 -08001101
1102int
1103dtls_connect (transport_endpoint_cfg_t *tep)
1104{
1105 vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
Florin Corasa54b62d2021-04-21 09:05:56 -07001106 transport_endpt_crypto_cfg_t *ccfg;
Florin Coras4b47ee22020-11-19 13:38:26 -08001107 crypto_engine_type_t engine_type;
1108 session_endpoint_cfg_t *sep;
1109 tls_main_t *tm = &tls_main;
1110 app_worker_t *app_wrk;
1111 application_t *app;
1112 tls_ctx_t *ctx;
1113 u32 ctx_handle;
1114 int rv;
1115
1116 sep = (session_endpoint_cfg_t *) tep;
Florin Corasa54b62d2021-04-21 09:05:56 -07001117 if (!sep->ext_cfg)
1118 return -1;
1119
Florin Coras4b47ee22020-11-19 13:38:26 -08001120 app_wrk = app_worker_get (sep->app_wrk_index);
1121 app = application_get (app_wrk->app_index);
Florin Corasa54b62d2021-04-21 09:05:56 -07001122
1123 ccfg = &sep->ext_cfg->crypto;
1124 engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
Florin Coras4b47ee22020-11-19 13:38:26 -08001125 if (engine_type == CRYPTO_ENGINE_NONE)
1126 {
1127 clib_warning ("No tls engine_type available");
1128 return -1;
1129 }
1130
Florin Corasfb50bc32021-05-18 00:28:59 -07001131 ctx_handle = tls_ctx_alloc_w_thread (engine_type, transport_cl_thread ());
1132 ctx = tls_ctx_get_w_thread (ctx_handle, transport_cl_thread ());
Florin Coras4b47ee22020-11-19 13:38:26 -08001133 ctx->parent_app_wrk_index = sep->app_wrk_index;
1134 ctx->parent_app_api_context = sep->opaque;
1135 ctx->tcp_is_ip4 = sep->is_ip4;
Florin Corasa54b62d2021-04-21 09:05:56 -07001136 ctx->ckpair_index = ccfg->ckpair_index;
Florin Coras4b47ee22020-11-19 13:38:26 -08001137 ctx->tls_type = sep->transport_proto;
1138 ctx->tls_ctx_handle = ctx_handle;
Florin Corasbab6c522021-05-13 08:36:56 -07001139 ctx->c_proto = TRANSPORT_PROTO_DTLS;
1140 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasa54b62d2021-04-21 09:05:56 -07001141 if (ccfg->hostname[0])
Florin Coras4b47ee22020-11-19 13:38:26 -08001142 {
Florin Corasa54b62d2021-04-21 09:05:56 -07001143 ctx->srv_hostname = format (0, "%s", ccfg->hostname);
Florin Coras4b47ee22020-11-19 13:38:26 -08001144 vec_terminate_c_string (ctx->srv_hostname);
1145 }
1146
Florin Coras4b47ee22020-11-19 13:38:26 -08001147 ctx->tls_ctx_engine = engine_type;
1148
1149 clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));
1150 cargs->sep.transport_proto = TRANSPORT_PROTO_UDP;
1151 cargs->app_index = tm->app_index;
1152 cargs->api_context = ctx_handle;
1153 cargs->sep_ext.ns_index = app->ns_index;
1154 cargs->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
1155 if ((rv = vnet_connect (cargs)))
1156 return rv;
1157
1158 TLS_DBG (1, "New DTLS connect request %x engine %d", ctx_handle,
1159 engine_type);
Florin Corasbab6c522021-05-13 08:36:56 -07001160 return ctx_handle;
1161}
1162
1163static transport_connection_t *
1164dtls_half_open_get (u32 ho_index)
1165{
1166 tls_ctx_t *ho_ctx;
Florin Corasfb50bc32021-05-18 00:28:59 -07001167 ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
Florin Corasbab6c522021-05-13 08:36:56 -07001168 return &ho_ctx->connection;
Florin Coras4b47ee22020-11-19 13:38:26 -08001169}
1170
1171static void
1172dtls_cleanup_callback (u32 ctx_index, u32 thread_index)
1173{
1174 /* No op */
1175}
1176
Florin Corasbab6c522021-05-13 08:36:56 -07001177static void
1178dtls_cleanup_ho (u32 ho_index)
1179{
1180 tls_ctx_t *ctx;
Florin Corasfb50bc32021-05-18 00:28:59 -07001181 ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
Florin Corasbab6c522021-05-13 08:36:56 -07001182 tls_ctx_free (ctx);
1183}
1184
1185u8 *
1186format_dtls_half_open (u8 *s, va_list *args)
1187{
1188 u32 ho_index = va_arg (*args, u32);
1189 u32 __clib_unused thread_index = va_arg (*args, u32);
1190 tls_ctx_t *ho_ctx;
1191 session_t *us;
1192
Florin Corasfb50bc32021-05-18 00:28:59 -07001193 ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
Florin Corasbab6c522021-05-13 08:36:56 -07001194
1195 us = session_get_from_handle (ho_ctx->tls_session_handle);
1196 s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u us %d:%d",
1197 ho_ctx->c_thread_index, ho_ctx->c_s_index, "DTLS",
1198 ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
1199 us->thread_index, us->session_index);
1200
1201 return s;
1202}
1203
Florin Coras4b47ee22020-11-19 13:38:26 -08001204static const transport_proto_vft_t dtls_proto = {
1205 .enable = 0,
1206 .connect = dtls_connect,
1207 .close = tls_disconnect,
1208 .start_listen = tls_start_listen,
1209 .stop_listen = tls_stop_listen,
1210 .get_connection = tls_connection_get,
1211 .get_listener = tls_listener_get,
Florin Corasbab6c522021-05-13 08:36:56 -07001212 .get_half_open = dtls_half_open_get,
Florin Coras4b47ee22020-11-19 13:38:26 -08001213 .custom_tx = tls_custom_tx_callback,
1214 .cleanup = dtls_cleanup_callback,
Florin Corasbab6c522021-05-13 08:36:56 -07001215 .cleanup_ho = dtls_cleanup_ho,
Florin Coras4b47ee22020-11-19 13:38:26 -08001216 .format_connection = format_tls_connection,
Florin Corasbab6c522021-05-13 08:36:56 -07001217 .format_half_open = format_dtls_half_open,
Florin Coras4b47ee22020-11-19 13:38:26 -08001218 .format_listener = format_tls_listener,
1219 .get_transport_endpoint = tls_transport_endpoint_get,
1220 .get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
1221 .transport_options = {
1222 .name = "dtls",
1223 .short_name = "D",
1224 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Corasbab6c522021-05-13 08:36:56 -07001225 .service_type = TRANSPORT_SERVICE_VC,
Florin Coras4b47ee22020-11-19 13:38:26 -08001226 },
1227};
Florin Coras371ca502018-02-21 12:07:41 -08001228
Florin Corasd77eee62018-03-07 08:49:27 -08001229void
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +02001230tls_register_engine (const tls_engine_vft_t * vft, crypto_engine_type_t type)
Florin Coras371ca502018-02-21 12:07:41 -08001231{
Florin Corasd77eee62018-03-07 08:49:27 -08001232 vec_validate (tls_vfts, type);
1233 tls_vfts[type] = *vft;
Florin Coras371ca502018-02-21 12:07:41 -08001234}
1235
Dave Barachcc5677b2018-03-28 19:08:45 -04001236static clib_error_t *
Florin Coras371ca502018-02-21 12:07:41 -08001237tls_init (vlib_main_t * vm)
1238{
Florin Coras58d36f02018-03-09 13:05:53 -08001239 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -08001240 tls_main_t *tm = &tls_main;
Florin Coras0a9ab822020-10-25 14:01:37 -07001241 u32 num_threads;
Florin Coras58d36f02018-03-09 13:05:53 -08001242
1243 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras371ca502018-02-21 12:07:41 -08001244
Florin Corasd77eee62018-03-07 08:49:27 -08001245 if (!tm->ca_cert_path)
1246 tm->ca_cert_path = TLS_CA_CERT_PATH;
1247
Florin Coras58d36f02018-03-09 13:05:53 -08001248 vec_validate (tm->rx_bufs, num_threads - 1);
1249 vec_validate (tm->tx_bufs, num_threads - 1);
1250
Florin Corasa3767bd2021-11-18 15:25:31 -08001251 tm->first_seg_size = 32 << 20;
1252 tm->add_seg_size = 256 << 20;
1253
Florin Coras371ca502018-02-21 12:07:41 -08001254 transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
1255 FIB_PROTOCOL_IP4, ~0);
1256 transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
1257 FIB_PROTOCOL_IP6, ~0);
Florin Coras4b47ee22020-11-19 13:38:26 -08001258
1259 transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto,
1260 FIB_PROTOCOL_IP4, ~0);
1261 transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto,
1262 FIB_PROTOCOL_IP6, ~0);
Florin Coras371ca502018-02-21 12:07:41 -08001263 return 0;
1264}
1265
1266VLIB_INIT_FUNCTION (tls_init);
1267
Florin Coras8f89dd02018-03-05 16:53:07 -08001268static clib_error_t *
1269tls_config_fn (vlib_main_t * vm, unformat_input_t * input)
1270{
1271 tls_main_t *tm = &tls_main;
Dave Wallaceb1a81aa2019-10-30 17:53:56 +00001272 uword tmp;
Florin Coras8f89dd02018-03-05 16:53:07 -08001273 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1274 {
1275 if (unformat (input, "use-test-cert-in-ca"))
1276 tm->use_test_cert_in_ca = 1;
1277 else if (unformat (input, "ca-cert-path %s", &tm->ca_cert_path))
1278 ;
Florin Corasddd98f32019-03-25 08:30:53 -07001279 else if (unformat (input, "first-segment-size %U", unformat_memory_size,
1280 &tm->first_seg_size))
1281 ;
Florin Corasa3767bd2021-11-18 15:25:31 -08001282 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1283 &tm->add_seg_size))
1284 ;
Dave Wallaceb1a81aa2019-10-30 17:53:56 +00001285 else if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp))
1286 {
1287 if (tmp >= 0x100000000ULL)
1288 {
1289 return clib_error_return
1290 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
1291 }
1292 tm->fifo_size = tmp;
1293 }
Florin Coras8f89dd02018-03-05 16:53:07 -08001294 else
1295 return clib_error_return (0, "unknown input `%U'",
1296 format_unformat_error, input);
1297 }
1298 return 0;
1299}
1300
Florin Coras1cbcdce2022-04-06 15:10:13 -07001301VLIB_CONFIG_FUNCTION (tls_config_fn, "tls");
Florin Corasd77eee62018-03-07 08:49:27 -08001302
1303tls_main_t *
1304vnet_tls_get_main (void)
1305{
1306 return &tls_main;
1307}
1308
Florin Coras371ca502018-02-21 12:07:41 -08001309/*
1310 * fd.io coding-style-patch-verification: ON
1311 *
1312 * Local Variables:
1313 * eval: (c-set-style "gnu")
1314 * End:
1315 */