blob: 9ca3a91af29d8daef01c0ef4cbad8b749442e710 [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{
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;
Florin Coras3a2cf842024-07-11 05:15:47 -0700652 ctx->ts_app_index = tm->app_index;
Florin Coras371ca502018-02-21 12:07:41 -0800653 ctx->tcp_is_ip4 = sep->is_ip4;
Florin Coras4b47ee22020-11-19 13:38:26 -0800654 ctx->tls_type = sep->transport_proto;
Florin Corasa54b62d2021-04-21 09:05:56 -0700655 ctx->ckpair_index = ccfg->ckpair_index;
Florin Coras2c876f92021-05-10 21:12:27 -0700656 ctx->c_proto = TRANSPORT_PROTO_TLS;
657 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasa54b62d2021-04-21 09:05:56 -0700658 if (ccfg->hostname[0])
Florin Coras8f89dd02018-03-05 16:53:07 -0800659 {
Florin Corasa54b62d2021-04-21 09:05:56 -0700660 ctx->srv_hostname = format (0, "%s", ccfg->hostname);
Florin Coras8f89dd02018-03-05 16:53:07 -0800661 vec_terminate_c_string (ctx->srv_hostname);
662 }
Florin Coras371ca502018-02-21 12:07:41 -0800663
Florin Coras58d36f02018-03-09 13:05:53 -0800664 ctx->tls_ctx_engine = engine_type;
Florin Coras371ca502018-02-21 12:07:41 -0800665
Dave Barach178cf492018-11-13 16:34:13 -0500666 clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));
Florin Coras15531972018-08-12 23:50:53 -0700667 cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
668 cargs->app_index = tm->app_index;
669 cargs->api_context = ctx_index;
Florin Coras8a140612019-02-18 22:39:39 -0800670 cargs->sep_ext.ns_index = app->ns_index;
Florin Corasc1a42652019-02-08 18:27:29 -0800671 if ((rv = vnet_connect (cargs)))
Florin Coras3109d1c2024-01-22 18:27:43 -0800672 {
673 tls_ctx_half_open_free (ctx_index);
674 return rv;
675 }
Florin Coras371ca502018-02-21 12:07:41 -0800676
Florin Coras2c876f92021-05-10 21:12:27 -0700677 /* Track half-open tcp session in case we need to clean it up */
678 ctx->tls_session_handle = cargs->sh;
679
Florin Coras58d36f02018-03-09 13:05:53 -0800680 TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type);
Florin Coras2c876f92021-05-10 21:12:27 -0700681 return ctx_index;
Florin Coras371ca502018-02-21 12:07:41 -0800682}
683
684void
Florin Coras58d36f02018-03-09 13:05:53 -0800685tls_disconnect (u32 ctx_handle, u32 thread_index)
Florin Coras371ca502018-02-21 12:07:41 -0800686{
Florin Coras371ca502018-02-21 12:07:41 -0800687 tls_ctx_t *ctx;
688
Florin Coras58d36f02018-03-09 13:05:53 -0800689 TLS_DBG (1, "Disconnecting %x", ctx_handle);
Florin Coras371ca502018-02-21 12:07:41 -0800690
Florin Coras58d36f02018-03-09 13:05:53 -0800691 ctx = tls_ctx_get (ctx_handle);
Florin Corasd7f17a22024-01-31 16:18:17 -0800692 ctx->flags |= TLS_CONN_F_APP_CLOSED;
Florin Coras06a6a302019-04-17 14:19:12 -0700693 tls_ctx_app_close (ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800694}
695
696u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800697tls_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
Florin Coras371ca502018-02-21 12:07:41 -0800698{
Florin Corasc9940fc2019-02-05 20:55:11 -0800699 vnet_listen_args_t _bargs, *args = &_bargs;
Florin Corasa54b62d2021-04-21 09:05:56 -0700700 transport_endpt_crypto_cfg_t *ccfg;
Florin Corasab2f6db2018-08-31 14:31:41 -0700701 app_worker_t *app_wrk;
Florin Coras371ca502018-02-21 12:07:41 -0800702 tls_main_t *tm = &tls_main;
Florin Corasa27a46e2019-02-18 13:02:28 -0800703 session_handle_t tls_al_handle;
Florin Coras5665ced2018-10-25 18:03:45 -0700704 session_endpoint_cfg_t *sep;
Florin Coras288eaab2019-02-03 15:26:14 -0800705 session_t *tls_listener;
706 session_t *app_listener;
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200707 crypto_engine_type_t engine_type;
Florin Coras74cac882018-09-07 09:13:15 -0700708 application_t *app;
Florin Corasa27a46e2019-02-18 13:02:28 -0800709 app_listener_t *al;
Florin Coras15531972018-08-12 23:50:53 -0700710 tls_ctx_t *lctx;
711 u32 lctx_index;
Florin Corase5f18332021-04-22 11:48:35 -0700712 int rv;
Florin Coras371ca502018-02-21 12:07:41 -0800713
Florin Coras5665ced2018-10-25 18:03:45 -0700714 sep = (session_endpoint_cfg_t *) tep;
Florin Corasa54b62d2021-04-21 09:05:56 -0700715 if (!sep->ext_cfg)
Florin Corase5f18332021-04-22 11:48:35 -0700716 return SESSION_E_NOEXTCFG;
Florin Corasa54b62d2021-04-21 09:05:56 -0700717
Florin Coras15531972018-08-12 23:50:53 -0700718 app_wrk = app_worker_get (sep->app_wrk_index);
719 app = application_get (app_wrk->app_index);
Florin Corasa54b62d2021-04-21 09:05:56 -0700720
721 ccfg = &sep->ext_cfg->crypto;
722 engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200723 if (engine_type == CRYPTO_ENGINE_NONE)
Florin Coras58d36f02018-03-09 13:05:53 -0800724 {
725 clib_warning ("No tls engine_type available");
Florin Corase5f18332021-04-22 11:48:35 -0700726 return SESSION_E_NOCRYPTOENG;
Florin Coras58d36f02018-03-09 13:05:53 -0800727 }
728
Dave Barachb7b92992018-10-17 10:38:51 -0400729 clib_memset (args, 0, sizeof (*args));
Florin Coras74cac882018-09-07 09:13:15 -0700730 args->app_index = tm->app_index;
731 args->sep_ext = *sep;
Florin Coras8a140612019-02-18 22:39:39 -0800732 args->sep_ext.ns_index = app->ns_index;
Florin Coras95cd8642019-12-30 21:53:19 -0800733 args->sep_ext.transport_proto = TRANSPORT_PROTO_TCP;
Florin Coras4b47ee22020-11-19 13:38:26 -0800734 if (sep->transport_proto == TRANSPORT_PROTO_DTLS)
735 {
736 args->sep_ext.transport_proto = TRANSPORT_PROTO_UDP;
737 args->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
738 }
Florin Corase5f18332021-04-22 11:48:35 -0700739 if ((rv = vnet_listen (args)))
740 return rv;
Florin Coras371ca502018-02-21 12:07:41 -0800741
Florin Coras74cac882018-09-07 09:13:15 -0700742 lctx_index = tls_listener_ctx_alloc ();
Florin Corasa27a46e2019-02-18 13:02:28 -0800743 tls_al_handle = args->handle;
744 al = app_listener_get_w_handle (tls_al_handle);
745 tls_listener = app_listener_get_session (al);
Florin Coras371ca502018-02-21 12:07:41 -0800746 tls_listener->opaque = lctx_index;
Florin Coras58d36f02018-03-09 13:05:53 -0800747
Florin Coras5c9083d2018-04-13 06:39:07 -0700748 app_listener = listen_session_get (app_listener_index);
Florin Coras58d36f02018-03-09 13:05:53 -0800749
750 lctx = tls_listener_ctx_get (lctx_index);
Florin Corasdf57ea02019-02-18 20:14:20 -0800751 lctx->parent_app_wrk_index = sep->app_wrk_index;
Florin Coras3a2cf842024-07-11 05:15:47 -0700752 lctx->ts_app_index = tm->app_index;
Florin Corasa27a46e2019-02-18 13:02:28 -0800753 lctx->tls_session_handle = tls_al_handle;
Florin Coras371ca502018-02-21 12:07:41 -0800754 lctx->app_session_handle = listen_session_get_handle (app_listener);
755 lctx->tcp_is_ip4 = sep->is_ip4;
Florin Coras58d36f02018-03-09 13:05:53 -0800756 lctx->tls_ctx_engine = engine_type;
Florin Coras4b47ee22020-11-19 13:38:26 -0800757 lctx->tls_type = sep->transport_proto;
Florin Corasa54b62d2021-04-21 09:05:56 -0700758 lctx->ckpair_index = ccfg->ckpair_index;
Florin Coras7705b492022-02-16 17:12:01 -0800759 lctx->c_s_index = app_listener_index;
Filip Tehlar61e26be2021-12-20 14:40:59 +0000760 lctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Coras58d36f02018-03-09 13:05:53 -0800761
Florin Corasae3eaac2019-07-23 22:38:16 -0700762 if (tls_vfts[engine_type].ctx_start_listen (lctx))
763 {
764 vnet_unlisten_args_t a = {
765 .handle = lctx->tls_session_handle,
766 .app_index = tls_main.app_index,
767 .wrk_map_index = 0
768 };
769 if ((vnet_unlisten (&a)))
770 clib_warning ("unlisten returned");
771 tls_listener_ctx_free (lctx);
772 lctx_index = SESSION_INVALID_INDEX;
773 }
Ping Yudecda5b2018-08-13 06:20:00 -0400774
Florin Coras58d36f02018-03-09 13:05:53 -0800775 TLS_DBG (1, "Started listening %d, engine type %d", lctx_index,
776 engine_type);
Florin Coras371ca502018-02-21 12:07:41 -0800777 return lctx_index;
778}
779
780u32
Florin Corase3e2f072018-03-04 07:24:30 -0800781tls_stop_listen (u32 lctx_index)
Florin Coras371ca502018-02-21 12:07:41 -0800782{
Florin Corasaab06042020-02-26 02:56:14 +0000783 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +0200784 crypto_engine_type_t engine_type;
Florin Corasaab06042020-02-26 02:56:14 +0000785 transport_connection_t *lc;
Florin Corasb5e94e32018-09-14 14:46:39 -0700786 tls_ctx_t *lctx;
Florin Corasaab06042020-02-26 02:56:14 +0000787 session_t *ls;
Florin Corasc1a42652019-02-08 18:27:29 -0800788 int rv;
Ping Yudecda5b2018-08-13 06:20:00 -0400789
Florin Corase3e2f072018-03-04 07:24:30 -0800790 lctx = tls_listener_ctx_get (lctx_index);
Florin Corasaab06042020-02-26 02:56:14 +0000791
792 /* Cleanup listener from session lookup table */
793 ls = session_get_from_handle (lctx->tls_session_handle);
794 lc = session_get_transport (ls);
795
796 sep.fib_index = lc->fib_index;
797 sep.port = lc->lcl_port;
798 sep.is_ip4 = lc->is_ip4;
Florin Coras4b47ee22020-11-19 13:38:26 -0800799 sep.transport_proto = lctx->tls_type;
Florin Corasaab06042020-02-26 02:56:14 +0000800 clib_memcpy (&sep.ip, &lc->lcl_ip, sizeof (lc->lcl_ip));
801 session_lookup_del_session_endpoint2 (&sep);
802
Florin Corasc1a42652019-02-08 18:27:29 -0800803 vnet_unlisten_args_t a = {
Florin Corasb5e94e32018-09-14 14:46:39 -0700804 .handle = lctx->tls_session_handle,
805 .app_index = tls_main.app_index,
806 .wrk_map_index = 0 /* default wrk */
807 };
Florin Corasc1a42652019-02-08 18:27:29 -0800808 if ((rv = vnet_unlisten (&a)))
809 clib_warning ("unlisten returned %d", rv);
Florin Corasb5e94e32018-09-14 14:46:39 -0700810
Ping Yudecda5b2018-08-13 06:20:00 -0400811 engine_type = lctx->tls_ctx_engine;
812 tls_vfts[engine_type].ctx_stop_listen (lctx);
813
Florin Corase3e2f072018-03-04 07:24:30 -0800814 tls_listener_ctx_free (lctx);
Florin Coras371ca502018-02-21 12:07:41 -0800815 return 0;
816}
817
818transport_connection_t *
Florin Corase3e2f072018-03-04 07:24:30 -0800819tls_connection_get (u32 ctx_index, u32 thread_index)
820{
821 tls_ctx_t *ctx;
822 ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
823 return &ctx->connection;
824}
825
826transport_connection_t *
Florin Coras371ca502018-02-21 12:07:41 -0800827tls_listener_get (u32 listener_index)
828{
829 tls_ctx_t *ctx;
830 ctx = tls_listener_ctx_get (listener_index);
831 return &ctx->connection;
832}
833
Florin Coras2c876f92021-05-10 21:12:27 -0700834static transport_connection_t *
835tls_half_open_get (u32 ho_index)
836{
Florin Coras2c876f92021-05-10 21:12:27 -0700837 tls_ctx_t *ctx;
838 ctx = tls_ctx_half_open_get (ho_index);
Florin Coras2c876f92021-05-10 21:12:27 -0700839 return &ctx->connection;
840}
841
842static void
843tls_cleanup_ho (u32 ho_index)
844{
Florin Coras2c876f92021-05-10 21:12:27 -0700845 tls_ctx_t *ctx;
Florin Coras4aaedaa2023-11-10 09:48:39 -0800846 session_t *s;
Florin Corasbab6c522021-05-13 08:36:56 -0700847
Florin Coras2c876f92021-05-10 21:12:27 -0700848 ctx = tls_ctx_half_open_get (ho_index);
Florin Corasa474bc82023-12-04 20:29:52 -0800849 /* Already pending cleanup */
850 if (ctx->tls_session_handle == SESSION_INVALID_HANDLE)
851 {
852 ASSERT (ctx->flags & TLS_CONN_F_HO_DONE);
Florin Coras4a98b932024-01-31 13:45:39 -0800853 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Corasa474bc82023-12-04 20:29:52 -0800854 return;
855 }
856
Florin Coras4aaedaa2023-11-10 09:48:39 -0800857 s = session_get_from_handle (ctx->tls_session_handle);
858 /* If no pending cleanup notification, force cleanup now. Otherwise,
859 * wait for cleanup notification and set no app session on ctx */
860 if (s->session_state != SESSION_STATE_TRANSPORT_DELETED)
861 {
862 session_cleanup_half_open (ctx->tls_session_handle);
863 tls_ctx_half_open_free (ho_index);
864 }
865 else
Florin Coras4a98b932024-01-31 13:45:39 -0800866 ctx->flags |= TLS_CONN_F_NO_APP_SESSION;
Florin Coras2c876f92021-05-10 21:12:27 -0700867}
868
Florin Corasf940f8a2019-03-06 10:44:38 -0800869int
Florin Coras9f86d222020-03-23 15:34:22 +0000870tls_custom_tx_callback (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -0800871{
Florin Coras00d7d862023-09-22 19:26:33 -0700872 session_t *as = (session_t *) session;
Florin Corasf940f8a2019-03-06 10:44:38 -0800873 tls_ctx_t *ctx;
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800874
Florin Coras00d7d862023-09-22 19:26:33 -0700875 if (PREDICT_FALSE (as->session_state >= SESSION_STATE_TRANSPORT_CLOSED ||
876 as->session_state <= SESSION_STATE_ACCEPTING))
877 {
878 sp->flags |= TRANSPORT_SND_F_DESCHED;
879 return 0;
880 }
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800881
Florin Coras00d7d862023-09-22 19:26:33 -0700882 ctx = tls_ctx_get (as->connection_index);
883 return tls_ctx_write (ctx, as, sp);
Florin Corasf940f8a2019-03-06 10:44:38 -0800884}
885
Florin Coras371ca502018-02-21 12:07:41 -0800886u8 *
887format_tls_ctx (u8 * s, va_list * args)
888{
jiangxiaoming5b7ea912020-09-23 10:29:21 +0800889 u32 tcp_si, tcp_ti, ctx_index, ctx_engine;
Florin Coras371ca502018-02-21 12:07:41 -0800890 tls_ctx_t *ctx = va_arg (*args, tls_ctx_t *);
Florin Coras4b47ee22020-11-19 13:38:26 -0800891 char *proto;
Florin Coras371ca502018-02-21 12:07:41 -0800892
Florin Coras4b47ee22020-11-19 13:38:26 -0800893 proto = ctx->tls_type == TRANSPORT_PROTO_TLS ? "TLS" : "DTLS";
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800894 session_parse_handle (ctx->tls_session_handle, &tcp_si, &tcp_ti);
895 tls_ctx_parse_handle (ctx->tls_ctx_handle, &ctx_index, &ctx_engine);
Florin Coras4b47ee22020-11-19 13:38:26 -0800896 s =
897 format (s, "[%d:%d][%s] app_wrk %u index %u engine %u ts %d:%d",
898 ctx->c_thread_index, ctx->c_s_index, proto,
899 ctx->parent_app_wrk_index, ctx_index, ctx_engine, tcp_ti, tcp_si);
Florin Coras371ca502018-02-21 12:07:41 -0800900
Florin Coras371ca502018-02-21 12:07:41 -0800901 return s;
902}
903
Florin Coras0d74dd12020-01-07 19:01:26 +0000904static u8 *
905format_tls_listener_ctx (u8 * s, va_list * args)
906{
907 session_t *tls_listener;
908 app_listener_t *al;
Florin Coras0d74dd12020-01-07 19:01:26 +0000909 tls_ctx_t *ctx;
Florin Coras4b47ee22020-11-19 13:38:26 -0800910 char *proto;
Florin Coras0d74dd12020-01-07 19:01:26 +0000911
912 ctx = va_arg (*args, tls_ctx_t *);
913
Florin Coras4b47ee22020-11-19 13:38:26 -0800914 proto = ctx->tls_type == TRANSPORT_PROTO_TLS ? "TLS" : "DTLS";
Florin Coras0d74dd12020-01-07 19:01:26 +0000915 al = app_listener_get_w_handle (ctx->tls_session_handle);
916 tls_listener = app_listener_get_session (al);
Florin Coras4b47ee22020-11-19 13:38:26 -0800917 s = format (s, "[%d:%d][%s] app_wrk %u engine %u ts %d:%d",
918 ctx->c_thread_index, ctx->c_s_index, proto,
jiangxiaoming5b7ea912020-09-23 10:29:21 +0800919 ctx->parent_app_wrk_index, ctx->tls_ctx_engine,
Florin Coras0d74dd12020-01-07 19:01:26 +0000920 tls_listener->thread_index, tls_listener->session_index);
921
922 return s;
923}
924
925static u8 *
926format_tls_ctx_state (u8 * s, va_list * args)
927{
928 tls_ctx_t *ctx;
929 session_t *ts;
930
931 ctx = va_arg (*args, tls_ctx_t *);
jiangxiaoming5b7ea912020-09-23 10:29:21 +0800932 ts = session_get (ctx->c_s_index, ctx->c_thread_index);
Florin Coras0d74dd12020-01-07 19:01:26 +0000933 if (ts->session_state == SESSION_STATE_LISTENING)
934 s = format (s, "%s", "LISTEN");
Florin Coras0d74dd12020-01-07 19:01:26 +0000935 else
Florin Coras681ea6d2020-04-24 16:59:49 +0000936 {
937 if (ts->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
938 s = format (s, "%s", "CLOSED");
939 else if (ts->session_state == SESSION_STATE_APP_CLOSED)
940 s = format (s, "%s", "APP-CLOSED");
941 else if (ts->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
942 s = format (s, "%s", "CLOSING");
943 else if (tls_ctx_handshake_is_over (ctx))
944 s = format (s, "%s", "ESTABLISHED");
945 else
946 s = format (s, "%s", "HANDSHAKE");
947 }
Florin Coras0d74dd12020-01-07 19:01:26 +0000948
949 return s;
950}
951
Florin Coras371ca502018-02-21 12:07:41 -0800952u8 *
953format_tls_connection (u8 * s, va_list * args)
954{
955 u32 ctx_index = va_arg (*args, u32);
956 u32 thread_index = va_arg (*args, u32);
957 u32 verbose = va_arg (*args, u32);
958 tls_ctx_t *ctx;
959
960 ctx = tls_ctx_get_w_thread (ctx_index, thread_index);
961 if (!ctx)
962 return s;
963
Florin Coras7bf6ed62020-09-23 12:02:08 -0700964 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tls_ctx, ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800965 if (verbose)
966 {
Florin Coras7bf6ed62020-09-23 12:02:08 -0700967 s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tls_ctx_state,
968 ctx);
Florin Coras371ca502018-02-21 12:07:41 -0800969 if (verbose > 1)
970 s = format (s, "\n");
971 }
972 return s;
973}
974
975u8 *
976format_tls_listener (u8 * s, va_list * args)
977{
978 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200979 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Coras0d74dd12020-01-07 19:01:26 +0000980 u32 verbose = va_arg (*args, u32);
Florin Coras371ca502018-02-21 12:07:41 -0800981 tls_ctx_t *ctx = tls_listener_ctx_get (tc_index);
Florin Coras371ca502018-02-21 12:07:41 -0800982
Florin Coras7bf6ed62020-09-23 12:02:08 -0700983 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_tls_listener_ctx, ctx);
Florin Coras0d74dd12020-01-07 19:01:26 +0000984 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -0700985 s = format (s, "%-" SESSION_CLI_STATE_LEN "U", format_tls_ctx_state, ctx);
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800986 return s;
Florin Coras371ca502018-02-21 12:07:41 -0800987}
988
989u8 *
990format_tls_half_open (u8 * s, va_list * args)
991{
Florin Coras2c876f92021-05-10 21:12:27 -0700992 u32 ho_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200993 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd69ef3a2023-10-02 10:41:54 -0700994 u32 __clib_unused verbose = va_arg (*args, u32);
Florin Coras2c876f92021-05-10 21:12:27 -0700995 session_t *tcp_ho;
996 tls_ctx_t *ho_ctx;
997
998 ho_ctx = tls_ctx_half_open_get (ho_index);
999
1000 tcp_ho = session_get_from_handle (ho_ctx->tls_session_handle);
1001 s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u ts %d:%d",
1002 ho_ctx->c_thread_index, ho_ctx->c_s_index, "TLS",
1003 ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
1004 tcp_ho->thread_index, tcp_ho->session_index);
1005
Florin Coras371ca502018-02-21 12:07:41 -08001006 return s;
1007}
1008
Yu Ping0b819152019-05-07 02:24:30 +08001009static void
1010tls_transport_endpoint_get (u32 ctx_handle, u32 thread_index,
1011 transport_endpoint_t * tep, u8 is_lcl)
1012{
1013 tls_ctx_t *ctx = tls_ctx_get_w_thread (ctx_handle, thread_index);
Florin Coras8074fc62024-02-20 11:24:54 -08001014 session_t *ts;
Yu Ping0b819152019-05-07 02:24:30 +08001015
Florin Coras8074fc62024-02-20 11:24:54 -08001016 ts = session_get_from_handle (ctx->tls_session_handle);
1017 if (ts && ts->session_state < SESSION_STATE_TRANSPORT_DELETED)
1018 session_get_endpoint (ts, tep, is_lcl);
Yu Ping0b819152019-05-07 02:24:30 +08001019}
1020
1021static void
1022tls_transport_listener_endpoint_get (u32 ctx_handle,
1023 transport_endpoint_t * tep, u8 is_lcl)
1024{
1025 session_t *tls_listener;
1026 app_listener_t *al;
1027 tls_ctx_t *ctx = tls_listener_ctx_get (ctx_handle);
1028
1029 al = app_listener_get_w_handle (ctx->tls_session_handle);
1030 tls_listener = app_listener_get_session (al);
1031 session_get_endpoint (tls_listener, tep, is_lcl);
1032}
1033
Florin Coras0a9ab822020-10-25 14:01:37 -07001034static clib_error_t *
1035tls_enable (vlib_main_t * vm, u8 is_en)
1036{
Florin Coras0a9ab822020-10-25 14:01:37 -07001037 vnet_app_detach_args_t _da, *da = &_da;
1038 vnet_app_attach_args_t _a, *a = &_a;
1039 u64 options[APP_OPTIONS_N_OPTIONS];
1040 tls_main_t *tm = &tls_main;
Florin Corasc1b03802023-09-30 15:21:08 -07001041 u32 fifo_size = 512 << 10;
Florin Coras0a9ab822020-10-25 14:01:37 -07001042
1043 if (!is_en)
1044 {
1045 da->app_index = tm->app_index;
1046 da->api_client_index = APP_INVALID_INDEX;
1047 vnet_application_detach (da);
1048 return 0;
1049 }
1050
Florin Coras0a9ab822020-10-25 14:01:37 -07001051 fifo_size = tm->fifo_size ? tm->fifo_size : fifo_size;
1052
1053 clib_memset (a, 0, sizeof (*a));
1054 clib_memset (options, 0, sizeof (options));
1055
1056 a->session_cb_vft = &tls_app_cb_vft;
1057 a->api_client_index = APP_INVALID_INDEX;
1058 a->options = options;
1059 a->name = format (0, "tls");
Florin Corasa3767bd2021-11-18 15:25:31 -08001060 a->options[APP_OPTIONS_SEGMENT_SIZE] = tm->first_seg_size;
1061 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = tm->add_seg_size;
Florin Coras0a9ab822020-10-25 14:01:37 -07001062 a->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
1063 a->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
1064 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
1065 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1066 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
1067
1068 if (vnet_application_attach (a))
1069 {
1070 clib_warning ("failed to attach tls app");
1071 return clib_error_return (0, "failed to attach tls app");
1072 }
1073
1074 tm->app_index = a->app_index;
1075 vec_free (a->name);
1076
1077 return 0;
1078}
1079
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001080static const transport_proto_vft_t tls_proto = {
Florin Coras0a9ab822020-10-25 14:01:37 -07001081 .enable = tls_enable,
Florin Coras1ee78302019-02-05 15:51:15 -08001082 .connect = tls_connect,
Florin Coras371ca502018-02-21 12:07:41 -08001083 .close = tls_disconnect,
Florin Coras1ee78302019-02-05 15:51:15 -08001084 .start_listen = tls_start_listen,
1085 .stop_listen = tls_stop_listen,
Florin Corase3e2f072018-03-04 07:24:30 -08001086 .get_connection = tls_connection_get,
Florin Coras371ca502018-02-21 12:07:41 -08001087 .get_listener = tls_listener_get,
Florin Coras2c876f92021-05-10 21:12:27 -07001088 .get_half_open = tls_half_open_get,
1089 .cleanup_ho = tls_cleanup_ho,
Florin Corasf940f8a2019-03-06 10:44:38 -08001090 .custom_tx = tls_custom_tx_callback,
Florin Coras371ca502018-02-21 12:07:41 -08001091 .format_connection = format_tls_connection,
1092 .format_half_open = format_tls_half_open,
1093 .format_listener = format_tls_listener,
Yu Ping0b819152019-05-07 02:24:30 +08001094 .get_transport_endpoint = tls_transport_endpoint_get,
1095 .get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001096 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +00001097 .name = "tls",
1098 .short_name = "J",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001099 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Coras2c876f92021-05-10 21:12:27 -07001100 .service_type = TRANSPORT_SERVICE_VC,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001101 },
Florin Coras371ca502018-02-21 12:07:41 -08001102};
Florin Coras4b47ee22020-11-19 13:38:26 -08001103
1104int
1105dtls_connect (transport_endpoint_cfg_t *tep)
1106{
1107 vnet_connect_args_t _cargs = { {}, }, *cargs = &_cargs;
Florin Corasa54b62d2021-04-21 09:05:56 -07001108 transport_endpt_crypto_cfg_t *ccfg;
Florin Coras4b47ee22020-11-19 13:38:26 -08001109 crypto_engine_type_t engine_type;
1110 session_endpoint_cfg_t *sep;
1111 tls_main_t *tm = &tls_main;
1112 app_worker_t *app_wrk;
1113 application_t *app;
1114 tls_ctx_t *ctx;
1115 u32 ctx_handle;
1116 int rv;
1117
1118 sep = (session_endpoint_cfg_t *) tep;
Florin Corasa54b62d2021-04-21 09:05:56 -07001119 if (!sep->ext_cfg)
1120 return -1;
1121
Florin Coras4b47ee22020-11-19 13:38:26 -08001122 app_wrk = app_worker_get (sep->app_wrk_index);
1123 app = application_get (app_wrk->app_index);
Florin Corasa54b62d2021-04-21 09:05:56 -07001124
1125 ccfg = &sep->ext_cfg->crypto;
1126 engine_type = tls_get_engine_type (ccfg->crypto_engine, app->tls_engine);
Florin Coras4b47ee22020-11-19 13:38:26 -08001127 if (engine_type == CRYPTO_ENGINE_NONE)
1128 {
1129 clib_warning ("No tls engine_type available");
1130 return -1;
1131 }
1132
Florin Corasfb50bc32021-05-18 00:28:59 -07001133 ctx_handle = tls_ctx_alloc_w_thread (engine_type, transport_cl_thread ());
1134 ctx = tls_ctx_get_w_thread (ctx_handle, transport_cl_thread ());
Florin Coras4b47ee22020-11-19 13:38:26 -08001135 ctx->parent_app_wrk_index = sep->app_wrk_index;
1136 ctx->parent_app_api_context = sep->opaque;
1137 ctx->tcp_is_ip4 = sep->is_ip4;
Florin Corasa54b62d2021-04-21 09:05:56 -07001138 ctx->ckpair_index = ccfg->ckpair_index;
Florin Coras4b47ee22020-11-19 13:38:26 -08001139 ctx->tls_type = sep->transport_proto;
1140 ctx->tls_ctx_handle = ctx_handle;
Florin Corasbab6c522021-05-13 08:36:56 -07001141 ctx->c_proto = TRANSPORT_PROTO_DTLS;
1142 ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasa54b62d2021-04-21 09:05:56 -07001143 if (ccfg->hostname[0])
Florin Coras4b47ee22020-11-19 13:38:26 -08001144 {
Florin Corasa54b62d2021-04-21 09:05:56 -07001145 ctx->srv_hostname = format (0, "%s", ccfg->hostname);
Florin Coras4b47ee22020-11-19 13:38:26 -08001146 vec_terminate_c_string (ctx->srv_hostname);
1147 }
1148
Florin Coras4b47ee22020-11-19 13:38:26 -08001149 ctx->tls_ctx_engine = engine_type;
1150
1151 clib_memcpy_fast (&cargs->sep, sep, sizeof (session_endpoint_t));
1152 cargs->sep.transport_proto = TRANSPORT_PROTO_UDP;
1153 cargs->app_index = tm->app_index;
1154 cargs->api_context = ctx_handle;
1155 cargs->sep_ext.ns_index = app->ns_index;
1156 cargs->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
1157 if ((rv = vnet_connect (cargs)))
1158 return rv;
1159
1160 TLS_DBG (1, "New DTLS connect request %x engine %d", ctx_handle,
1161 engine_type);
Florin Corasbab6c522021-05-13 08:36:56 -07001162 return ctx_handle;
1163}
1164
1165static transport_connection_t *
1166dtls_half_open_get (u32 ho_index)
1167{
1168 tls_ctx_t *ho_ctx;
Florin Corasfb50bc32021-05-18 00:28:59 -07001169 ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
Florin Corasbab6c522021-05-13 08:36:56 -07001170 return &ho_ctx->connection;
Florin Coras4b47ee22020-11-19 13:38:26 -08001171}
1172
1173static void
1174dtls_cleanup_callback (u32 ctx_index, u32 thread_index)
1175{
1176 /* No op */
1177}
1178
Florin Corasbab6c522021-05-13 08:36:56 -07001179static void
1180dtls_cleanup_ho (u32 ho_index)
1181{
1182 tls_ctx_t *ctx;
Florin Corasfb50bc32021-05-18 00:28:59 -07001183 ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
Florin Corasbab6c522021-05-13 08:36:56 -07001184 tls_ctx_free (ctx);
1185}
1186
1187u8 *
1188format_dtls_half_open (u8 *s, va_list *args)
1189{
1190 u32 ho_index = va_arg (*args, u32);
1191 u32 __clib_unused thread_index = va_arg (*args, u32);
1192 tls_ctx_t *ho_ctx;
1193 session_t *us;
1194
Florin Corasfb50bc32021-05-18 00:28:59 -07001195 ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
Florin Corasbab6c522021-05-13 08:36:56 -07001196
1197 us = session_get_from_handle (ho_ctx->tls_session_handle);
1198 s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u us %d:%d",
1199 ho_ctx->c_thread_index, ho_ctx->c_s_index, "DTLS",
1200 ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
1201 us->thread_index, us->session_index);
1202
1203 return s;
1204}
1205
Florin Coras4b47ee22020-11-19 13:38:26 -08001206static const transport_proto_vft_t dtls_proto = {
1207 .enable = 0,
1208 .connect = dtls_connect,
1209 .close = tls_disconnect,
1210 .start_listen = tls_start_listen,
1211 .stop_listen = tls_stop_listen,
1212 .get_connection = tls_connection_get,
1213 .get_listener = tls_listener_get,
Florin Corasbab6c522021-05-13 08:36:56 -07001214 .get_half_open = dtls_half_open_get,
Florin Coras4b47ee22020-11-19 13:38:26 -08001215 .custom_tx = tls_custom_tx_callback,
1216 .cleanup = dtls_cleanup_callback,
Florin Corasbab6c522021-05-13 08:36:56 -07001217 .cleanup_ho = dtls_cleanup_ho,
Florin Coras4b47ee22020-11-19 13:38:26 -08001218 .format_connection = format_tls_connection,
Florin Corasbab6c522021-05-13 08:36:56 -07001219 .format_half_open = format_dtls_half_open,
Florin Coras4b47ee22020-11-19 13:38:26 -08001220 .format_listener = format_tls_listener,
1221 .get_transport_endpoint = tls_transport_endpoint_get,
1222 .get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
1223 .transport_options = {
1224 .name = "dtls",
1225 .short_name = "D",
1226 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Corasbab6c522021-05-13 08:36:56 -07001227 .service_type = TRANSPORT_SERVICE_VC,
Florin Coras4b47ee22020-11-19 13:38:26 -08001228 },
1229};
Florin Coras371ca502018-02-21 12:07:41 -08001230
Florin Corasd77eee62018-03-07 08:49:27 -08001231void
Nathan Skrzypczak82fc5fd2019-09-13 10:20:15 +02001232tls_register_engine (const tls_engine_vft_t * vft, crypto_engine_type_t type)
Florin Coras371ca502018-02-21 12:07:41 -08001233{
Florin Corasd77eee62018-03-07 08:49:27 -08001234 vec_validate (tls_vfts, type);
1235 tls_vfts[type] = *vft;
Florin Coras371ca502018-02-21 12:07:41 -08001236}
1237
Dave Barachcc5677b2018-03-28 19:08:45 -04001238static clib_error_t *
Florin Coras371ca502018-02-21 12:07:41 -08001239tls_init (vlib_main_t * vm)
1240{
Florin Coras58d36f02018-03-09 13:05:53 -08001241 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras371ca502018-02-21 12:07:41 -08001242 tls_main_t *tm = &tls_main;
Florin Coras0a9ab822020-10-25 14:01:37 -07001243 u32 num_threads;
Florin Coras58d36f02018-03-09 13:05:53 -08001244
1245 num_threads = 1 /* main thread */ + vtm->n_threads;
Florin Coras371ca502018-02-21 12:07:41 -08001246
Florin Corasd77eee62018-03-07 08:49:27 -08001247 if (!tm->ca_cert_path)
1248 tm->ca_cert_path = TLS_CA_CERT_PATH;
1249
Florin Coras58d36f02018-03-09 13:05:53 -08001250 vec_validate (tm->rx_bufs, num_threads - 1);
1251 vec_validate (tm->tx_bufs, num_threads - 1);
1252
Florin Corasa3767bd2021-11-18 15:25:31 -08001253 tm->first_seg_size = 32 << 20;
1254 tm->add_seg_size = 256 << 20;
1255
Florin Coras371ca502018-02-21 12:07:41 -08001256 transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
1257 FIB_PROTOCOL_IP4, ~0);
1258 transport_register_protocol (TRANSPORT_PROTO_TLS, &tls_proto,
1259 FIB_PROTOCOL_IP6, ~0);
Florin Coras4b47ee22020-11-19 13:38:26 -08001260
1261 transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto,
1262 FIB_PROTOCOL_IP4, ~0);
1263 transport_register_protocol (TRANSPORT_PROTO_DTLS, &dtls_proto,
1264 FIB_PROTOCOL_IP6, ~0);
Florin Coras371ca502018-02-21 12:07:41 -08001265 return 0;
1266}
1267
1268VLIB_INIT_FUNCTION (tls_init);
1269
Florin Coras8f89dd02018-03-05 16:53:07 -08001270static clib_error_t *
1271tls_config_fn (vlib_main_t * vm, unformat_input_t * input)
1272{
1273 tls_main_t *tm = &tls_main;
Dave Wallaceb1a81aa2019-10-30 17:53:56 +00001274 uword tmp;
Florin Coras8f89dd02018-03-05 16:53:07 -08001275 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1276 {
1277 if (unformat (input, "use-test-cert-in-ca"))
1278 tm->use_test_cert_in_ca = 1;
1279 else if (unformat (input, "ca-cert-path %s", &tm->ca_cert_path))
1280 ;
Florin Corasddd98f32019-03-25 08:30:53 -07001281 else if (unformat (input, "first-segment-size %U", unformat_memory_size,
1282 &tm->first_seg_size))
1283 ;
Florin Corasa3767bd2021-11-18 15:25:31 -08001284 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1285 &tm->add_seg_size))
1286 ;
Dave Wallaceb1a81aa2019-10-30 17:53:56 +00001287 else if (unformat (input, "fifo-size %U", unformat_memory_size, &tmp))
1288 {
1289 if (tmp >= 0x100000000ULL)
1290 {
1291 return clib_error_return
1292 (0, "fifo-size %llu (0x%llx) too large", tmp, tmp);
1293 }
1294 tm->fifo_size = tmp;
1295 }
Florin Coras8f89dd02018-03-05 16:53:07 -08001296 else
1297 return clib_error_return (0, "unknown input `%U'",
1298 format_unformat_error, input);
1299 }
1300 return 0;
1301}
1302
Florin Coras1cbcdce2022-04-06 15:10:13 -07001303VLIB_CONFIG_FUNCTION (tls_config_fn, "tls");
Florin Corasd77eee62018-03-07 08:49:27 -08001304
1305tls_main_t *
1306vnet_tls_get_main (void)
1307{
1308 return &tls_main;
1309}
1310
Florin Coras371ca502018-02-21 12:07:41 -08001311/*
1312 * fd.io coding-style-patch-verification: ON
1313 *
1314 * Local Variables:
1315 * eval: (c-set-style "gnu")
1316 * End:
1317 */